bulb.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from __future__ import annotations
  2. import logging
  3. from .base_light import SwitchbotSequenceBaseLight
  4. from .device import REQ_HEADER, ColorMode
  5. BULB_COMMAND_HEADER = "4701"
  6. BULB_REQUEST = f"{REQ_HEADER}4801"
  7. BULB_COMMAND = f"{REQ_HEADER}{BULB_COMMAND_HEADER}"
  8. # Bulb keys
  9. BULB_ON_KEY = f"{BULB_COMMAND}01"
  10. BULB_OFF_KEY = f"{BULB_COMMAND}02"
  11. RGB_BRIGHTNESS_KEY = f"{BULB_COMMAND}12"
  12. CW_BRIGHTNESS_KEY = f"{BULB_COMMAND}13"
  13. BRIGHTNESS_KEY = f"{BULB_COMMAND}14"
  14. RGB_KEY = f"{BULB_COMMAND}16"
  15. CW_KEY = f"{BULB_COMMAND}17"
  16. _LOGGER = logging.getLogger(__name__)
  17. class SwitchbotBulb(SwitchbotSequenceBaseLight):
  18. """Representation of a Switchbot bulb."""
  19. @property
  20. def color_modes(self) -> set[ColorMode]:
  21. """Return the supported color modes."""
  22. return {ColorMode.RGB, ColorMode.COLOR_TEMP}
  23. async def update(self) -> None:
  24. """Update state of device."""
  25. result = await self._send_command(BULB_REQUEST)
  26. self._update_state(result)
  27. await super().update()
  28. async def turn_on(self) -> bool:
  29. """Turn device on."""
  30. result = await self._send_command(BULB_ON_KEY)
  31. self._update_state(result)
  32. return self._check_command_result(result, 1, {0x80})
  33. async def turn_off(self) -> bool:
  34. """Turn device off."""
  35. result = await self._send_command(BULB_OFF_KEY)
  36. self._update_state(result)
  37. return self._check_command_result(result, 1, {0x00})
  38. async def set_brightness(self, brightness: int) -> bool:
  39. """Set brightness."""
  40. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  41. result = await self._send_command(f"{BRIGHTNESS_KEY}{brightness:02X}")
  42. self._update_state(result)
  43. return self._check_command_result(result, 1, {0x80})
  44. async def set_color_temp(self, brightness: int, color_temp: int) -> bool:
  45. """Set color temp."""
  46. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  47. assert 2700 <= color_temp <= 6500, "Color Temp must be between 0 and 100"
  48. result = await self._send_command(
  49. f"{CW_BRIGHTNESS_KEY}{brightness:02X}{color_temp:04X}"
  50. )
  51. self._update_state(result)
  52. return self._check_command_result(result, 1, {0x80})
  53. async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
  54. """Set rgb."""
  55. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  56. assert 0 <= r <= 255, "r must be between 0 and 255"
  57. assert 0 <= g <= 255, "g must be between 0 and 255"
  58. assert 0 <= b <= 255, "b must be between 0 and 255"
  59. result = await self._send_command(
  60. f"{RGB_BRIGHTNESS_KEY}{brightness:02X}{r:02X}{g:02X}{b:02X}"
  61. )
  62. self._update_state(result)
  63. return self._check_command_result(result, 1, {0x80})
  64. def _update_state(self, result: bytes | None) -> None:
  65. """Update device state."""
  66. if not result or len(result) < 10:
  67. return
  68. self._state["r"] = result[3]
  69. self._state["g"] = result[4]
  70. self._state["b"] = result[5]
  71. self._state["cw"] = int(result[6:8].hex(), 16)
  72. self._override_state(
  73. {
  74. "isOn": result[1] == 0x80,
  75. "color_mode": result[10],
  76. }
  77. )
  78. _LOGGER.debug("%s: update state: %s = %s", self.name, result.hex(), self._state)
  79. self._fire_callbacks()