light_strip.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. def __init__(self, *args: Any, **kwargs: Any) -> None:
  18. """Switchbot light strip constructor."""
  19. super().__init__(*args, **kwargs)
  20. self._state: dict[str, Any] = {}
  21. @property
  22. def color_modes(self) -> set[ColorMode]:
  23. """Return the supported color modes."""
  24. return {ColorMode.RGB}
  25. async def update(self) -> None:
  26. """Update state of device."""
  27. result = await self._send_command(STRIP_REQUEST)
  28. self._update_state(result)
  29. async def turn_on(self) -> bool:
  30. """Turn device on."""
  31. result = await self._send_command(STRIP_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(STRIP_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. # not supported on this device
  48. async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
  49. """Set rgb."""
  50. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  51. assert 0 <= r <= 255, "r must be between 0 and 255"
  52. assert 0 <= g <= 255, "g must be between 0 and 255"
  53. assert 0 <= b <= 255, "b must be between 0 and 255"
  54. result = await self._send_command(
  55. f"{RGB_BRIGHTNESS_KEY}{brightness:02X}{r:02X}{g:02X}{b:02X}"
  56. )
  57. self._update_state(result)
  58. return self._check_command_result(result, 1, {0x80})
  59. def _update_state(self, result: bytes | None) -> None:
  60. """Update device state."""
  61. if not result or len(result) < 10:
  62. return
  63. self._state["r"] = result[3]
  64. self._state["g"] = result[4]
  65. self._state["b"] = result[5]
  66. self._override_adv_data = {
  67. "isOn": result[1] == 0x80,
  68. "color_mode": result[10],
  69. }
  70. _LOGGER.debug("%s: update state: %s = %s", self.name, result.hex(), self._state)
  71. self._fire_callbacks()