light_strip.py 2.9 KB

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