light_strip.py 2.9 KB

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