bulb.py 3.6 KB

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