bulb.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. @property
  21. def color_modes(self) -> set[ColorMode]:
  22. """Return the supported color modes."""
  23. return {ColorMode.RGB, ColorMode.COLOR_TEMP}
  24. async def update(self) -> None:
  25. """Update state of device."""
  26. result = await self._send_command(BULB_REQUEST)
  27. self._update_state(result)
  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_adv_data = {
  73. "isOn": result[1] == 0x80,
  74. "color_mode": result[10],
  75. }
  76. _LOGGER.debug("%s: update state: %s = %s", self.name, result.hex(), self._state)
  77. self._fire_callbacks()