bulb.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. await super().update()
  29. async def turn_on(self) -> bool:
  30. """Turn device on."""
  31. result = await self._send_command(BULB_ON_KEY)
  32. self._update_state(result)
  33. return self._check_command_result(result, 1, {0x80})
  34. async def turn_off(self) -> bool:
  35. """Turn device off."""
  36. result = await self._send_command(BULB_OFF_KEY)
  37. self._update_state(result)
  38. return self._check_command_result(result, 1, {0x00})
  39. async def set_brightness(self, brightness: int) -> bool:
  40. """Set brightness."""
  41. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  42. result = await self._send_command(f"{BRIGHTNESS_KEY}{brightness:02X}")
  43. self._update_state(result)
  44. return self._check_command_result(result, 1, {0x80})
  45. async def set_color_temp(self, brightness: int, color_temp: int) -> bool:
  46. """Set color temp."""
  47. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  48. assert 2700 <= color_temp <= 6500, "Color Temp must be between 0 and 100"
  49. result = await self._send_command(
  50. f"{CW_BRIGHTNESS_KEY}{brightness:02X}{color_temp:04X}"
  51. )
  52. self._update_state(result)
  53. return self._check_command_result(result, 1, {0x80})
  54. async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
  55. """Set rgb."""
  56. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  57. assert 0 <= r <= 255, "r must be between 0 and 255"
  58. assert 0 <= g <= 255, "g must be between 0 and 255"
  59. assert 0 <= b <= 255, "b must be between 0 and 255"
  60. result = await self._send_command(
  61. f"{RGB_BRIGHTNESS_KEY}{brightness:02X}{r:02X}{g:02X}{b:02X}"
  62. )
  63. self._update_state(result)
  64. return self._check_command_result(result, 1, {0x80})
  65. def _update_state(self, result: bytes | None) -> None:
  66. """Update device state."""
  67. if not result or len(result) < 10:
  68. return
  69. self._state["r"] = result[3]
  70. self._state["g"] = result[4]
  71. self._state["b"] = result[5]
  72. self._state["cw"] = int(result[6:8].hex(), 16)
  73. self._override_state(
  74. {
  75. "isOn": result[1] == 0x80,
  76. "color_mode": result[10],
  77. }
  78. )
  79. _LOGGER.debug("%s: update state: %s = %s", self.name, result.hex(), self._state)
  80. self._fire_callbacks()