light_strip.py 3.1 KB

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