bulb.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from __future__ import annotations
  2. import asyncio
  3. from enum import Enum
  4. from typing import Any
  5. from switchbot.models import SwitchBotAdvertisement
  6. from .device import SwitchbotDevice
  7. REQ_HEADER = "570f"
  8. BULB_COMMMAND_HEADER = "4701"
  9. BULB_REQUEST = f"{REQ_HEADER}4801"
  10. BULB_COMMAND = f"{REQ_HEADER}{BULB_COMMMAND_HEADER}"
  11. # Bulb keys
  12. BULB_ON_KEY = f"{BULB_COMMAND}01"
  13. BULB_OFF_KEY = f"{BULB_COMMAND}02"
  14. RGB_BRIGHTNESS_KEY = f"{BULB_COMMAND}12"
  15. CW_BRIGHTNESS_KEY = f"{BULB_COMMAND}13"
  16. BRIGHTNESS_KEY = f"{BULB_COMMAND}14"
  17. RGB_KEY = f"{BULB_COMMAND}16"
  18. CW_KEY = f"{BULB_COMMAND}17"
  19. class ColorMode(Enum):
  20. OFF = 0
  21. COLOR_TEMP = 1
  22. RGB = 2
  23. EFFECT = 3
  24. class SwitchbotBulb(SwitchbotDevice):
  25. """Representation of a Switchbot bulb."""
  26. def __init__(self, *args: Any, **kwargs: Any) -> None:
  27. """Switchbot bulb constructor."""
  28. super().__init__(*args, **kwargs)
  29. self._state: dict[str, Any] = {}
  30. @property
  31. def on(self) -> bool:
  32. """Return if bulb is on."""
  33. return self.is_on()
  34. @property
  35. def rgb(self) -> tuple[int, int, int] | None:
  36. """Return the current rgb value."""
  37. if "r" not in self._state or "g" not in self._state or "b" not in self._state:
  38. return None
  39. return self._state["r"], self._state["g"], self._state["b"]
  40. @property
  41. def color_temp(self) -> int | None:
  42. """Return the current color temp value."""
  43. return self._state.get("cw")
  44. @property
  45. def brightness(self) -> int | None:
  46. """Return the current brightness value."""
  47. return self._get_adv_value("brightness")
  48. @property
  49. def color_mode(self) -> ColorMode:
  50. """Return the current color mode."""
  51. return ColorMode(self._get_adv_value("color_mode") or 0)
  52. async def update(self, interface: int | None = None) -> None:
  53. """Update state of device."""
  54. result = await self._sendcommand(BULB_REQUEST)
  55. self._update_state(result)
  56. async def turn_on(self) -> bool:
  57. """Turn device on."""
  58. result = await self._sendcommand(BULB_ON_KEY)
  59. self._update_state(result)
  60. return result[1] == 0x80
  61. async def turn_off(self) -> bool:
  62. """Turn device off."""
  63. result = await self._sendcommand(BULB_OFF_KEY)
  64. self._update_state(result)
  65. return result[1] == 0x00
  66. async def set_brightness(self, brightness: int) -> bool:
  67. """Set brightness."""
  68. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  69. result = await self._sendcommand(f"{BRIGHTNESS_KEY}{brightness:02X}")
  70. self._update_state(result)
  71. return result[1] == 0x80
  72. async def set_color_temp(self, brightness: int, color_temp: int) -> bool:
  73. """Set color temp."""
  74. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  75. assert 2700 <= color_temp <= 6500, "Color Temp must be between 0 and 100"
  76. result = await self._sendcommand(
  77. f"{CW_BRIGHTNESS_KEY}{brightness:02X}{color_temp:04X}"
  78. )
  79. self._update_state(result)
  80. return result[1] == 0x80
  81. async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
  82. """Set rgb."""
  83. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  84. assert 0 <= r <= 255, "r must be between 0 and 255"
  85. assert 0 <= g <= 255, "g must be between 0 and 255"
  86. assert 0 <= b <= 255, "b must be between 0 and 255"
  87. result = await self._sendcommand(
  88. f"{RGB_BRIGHTNESS_KEY}{brightness:02X}{r:02X}{g:02X}{b:02X}"
  89. )
  90. self._update_state(result)
  91. return result[1] == 0x80
  92. async def turn_off(self) -> bool:
  93. """Turn device off."""
  94. result = await self._sendcommand(BULB_OFF_KEY)
  95. self._update_state(result)
  96. return result[1] == 0x00
  97. def is_on(self) -> bool | None:
  98. """Return bulb state from cache."""
  99. return self._get_adv_value("isOn")
  100. def _update_state(self, result: bytes) -> None:
  101. """Update device state."""
  102. self._state["r"] = result[3]
  103. self._state["g"] = result[4]
  104. self._state["b"] = result[5]
  105. self._state["cw"] = int(result[6:7].hex(), 16)
  106. self._fire_callbacks()
  107. def update_from_advertisement(self, advertisement: SwitchBotAdvertisement) -> None:
  108. """Update device data from advertisement."""
  109. current_state = self._get_adv_value("sequence_number")
  110. super().update_from_advertisement()
  111. if current_state != self._get_adv_value("sequence_number"):
  112. asyncio.ensure_future(self.update())