light_strip.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from __future__ import annotations
  2. import asyncio
  3. import logging
  4. from typing import Any
  5. from switchbot.models import SwitchBotAdvertisement
  6. from .device import SwitchbotDevice
  7. REQ_HEADER = "570f"
  8. STRIP_COMMMAND_HEADER = "4901"
  9. STRIP_REQUEST = f"{REQ_HEADER}4A01"
  10. STRIP_COMMAND = f"{REQ_HEADER}{STRIP_COMMMAND_HEADER}"
  11. # Strip keys
  12. STRIP_ON_KEY = f"{STRIP_COMMAND}01"
  13. STRIP_OFF_KEY = f"{STRIP_COMMAND}02"
  14. RGB_BRIGHTNESS_KEY = f"{STRIP_COMMAND}12"
  15. BRIGHTNESS_KEY = f"{STRIP_COMMAND}14"
  16. _LOGGER = logging.getLogger(__name__)
  17. from .device import ColorMode
  18. class SwitchbotLightStrip(SwitchbotDevice):
  19. """Representation of a Switchbot light strip."""
  20. def __init__(self, *args: Any, **kwargs: Any) -> None:
  21. """Switchbot light strip constructor."""
  22. super().__init__(*args, **kwargs)
  23. self._state: dict[str, Any] = {}
  24. @property
  25. def on(self) -> bool | None:
  26. """Return if bulb is on."""
  27. return self.is_on()
  28. @property
  29. def rgb(self) -> tuple[int, int, int] | None:
  30. """Return the current rgb value."""
  31. if "r" not in self._state or "g" not in self._state or "b" not in self._state:
  32. return None
  33. return self._state["r"], self._state["g"], self._state["b"]
  34. @property
  35. def brightness(self) -> int | None:
  36. """Return the current brightness value."""
  37. return self._get_adv_value("brightness") or 0
  38. @property
  39. def color_mode(self) -> ColorMode:
  40. """Return the current color mode."""
  41. return ColorMode(self._get_adv_value("color_mode") or 0)
  42. async def update(self) -> None:
  43. """Update state of device."""
  44. result = await self._sendcommand(STRIP_REQUEST)
  45. self._update_state(result)
  46. async def turn_on(self) -> bool:
  47. """Turn device on."""
  48. result = await self._sendcommand(STRIP_ON_KEY)
  49. self._update_state(result)
  50. return result[1] == 0x80
  51. async def turn_off(self) -> bool:
  52. """Turn device off."""
  53. result = await self._sendcommand(STRIP_OFF_KEY)
  54. self._update_state(result)
  55. return result[1] == 0x00
  56. async def set_brightness(self, brightness: int) -> bool:
  57. """Set brightness."""
  58. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  59. result = await self._sendcommand(f"{BRIGHTNESS_KEY}{brightness:02X}")
  60. self._update_state(result)
  61. return result[1] == 0x80
  62. async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
  63. """Set rgb."""
  64. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  65. assert 0 <= r <= 255, "r must be between 0 and 255"
  66. assert 0 <= g <= 255, "g must be between 0 and 255"
  67. assert 0 <= b <= 255, "b must be between 0 and 255"
  68. result = await self._sendcommand(
  69. f"{RGB_BRIGHTNESS_KEY}{brightness:02X}{r:02X}{g:02X}{b:02X}"
  70. )
  71. self._update_state(result)
  72. return result[1] == 0x80
  73. def is_on(self) -> bool | None:
  74. """Return bulb state from cache."""
  75. return self._get_adv_value("isOn")
  76. def _update_state(self, result: bytes) -> None:
  77. """Update device state."""
  78. if len(result) < 10:
  79. return
  80. self._state["r"] = result[3]
  81. self._state["g"] = result[4]
  82. self._state["b"] = result[5]
  83. _LOGGER.debug(
  84. "%s: Bulb update state: %s = %s", self.name, result.hex(), self._state
  85. )
  86. self._fire_callbacks()
  87. def update_from_advertisement(self, advertisement: SwitchBotAdvertisement) -> None:
  88. """Update device data from advertisement."""
  89. current_state = self._get_adv_value("sequence_number")
  90. super().update_from_advertisement(advertisement)
  91. new_state = self._get_adv_value("sequence_number")
  92. _LOGGER.debug(
  93. "%s: Strip update advertisement: %s (seq before: %s) (seq after: %s)",
  94. self.name,
  95. advertisement,
  96. current_state,
  97. new_state,
  98. )
  99. if current_state != new_state:
  100. asyncio.ensure_future(self.update())