1
0

light_strip.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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._sendcommand(STRIP_REQUEST)
  29. self._update_state(result)
  30. async def turn_on(self) -> bool:
  31. """Turn device on."""
  32. result = await self._sendcommand(STRIP_ON_KEY)
  33. self._update_state(result)
  34. return result[1] == 0x80
  35. async def turn_off(self) -> bool:
  36. """Turn device off."""
  37. result = await self._sendcommand(STRIP_OFF_KEY)
  38. self._update_state(result)
  39. return 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._sendcommand(f"{BRIGHTNESS_KEY}{brightness:02X}")
  44. self._update_state(result)
  45. return result[1] == 0x80
  46. async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
  47. """Set rgb."""
  48. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  49. assert 0 <= r <= 255, "r must be between 0 and 255"
  50. assert 0 <= g <= 255, "g must be between 0 and 255"
  51. assert 0 <= b <= 255, "b must be between 0 and 255"
  52. result = await self._sendcommand(
  53. f"{RGB_BRIGHTNESS_KEY}{brightness:02X}{r:02X}{g:02X}{b:02X}"
  54. )
  55. self._update_state(result)
  56. return result[1] == 0x80
  57. def _update_state(self, result: bytes) -> None:
  58. """Update device state."""
  59. if len(result) < 10:
  60. return
  61. self._state["r"] = result[3]
  62. self._state["g"] = result[4]
  63. self._state["b"] = result[5]
  64. _LOGGER.debug(
  65. "%s: Bulb update state: %s = %s", self.name, result.hex(), self._state
  66. )
  67. self._fire_callbacks()