bulb.py 3.5 KB

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