base_light.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from __future__ import annotations
  2. import logging
  3. from abc import abstractmethod
  4. from typing import Any
  5. from ..helpers import create_background_task
  6. from ..models import SwitchBotAdvertisement
  7. from .device import SwitchbotDevice
  8. _LOGGER = logging.getLogger(__name__)
  9. class SwitchbotBaseLight(SwitchbotDevice):
  10. """Representation of a Switchbot light."""
  11. def __init__(self, *args: Any, **kwargs: Any) -> None:
  12. """Switchbot bulb constructor."""
  13. super().__init__(*args, **kwargs)
  14. self._state: dict[str, Any] = {}
  15. @property
  16. def on(self) -> bool | None:
  17. """Return if bulb is on."""
  18. return self.is_on()
  19. @property
  20. def rgb(self) -> tuple[int, int, int] | None:
  21. """Return the current rgb value."""
  22. if "r" not in self._state or "g" not in self._state or "b" not in self._state:
  23. return None
  24. return self._state["r"], self._state["g"], self._state["b"]
  25. @property
  26. def color_temp(self) -> int | None:
  27. """Return the current color temp value."""
  28. return self._state.get("cw") or self.min_temp
  29. @property
  30. def brightness(self) -> int | None:
  31. """Return the current brightness value."""
  32. return self._get_adv_value("brightness") or 0
  33. @property
  34. @abstractmethod
  35. def color_mode(self) -> Any:
  36. """Return the current color mode."""
  37. raise NotImplementedError("Subclasses must implement color mode")
  38. @property
  39. def min_temp(self) -> int:
  40. """Return minimum color temp."""
  41. return 2700
  42. @property
  43. def max_temp(self) -> int:
  44. """Return maximum color temp."""
  45. return 6500
  46. @property
  47. def get_effect_list(self) -> list[str] | None:
  48. """Return the list of supported effects."""
  49. return None
  50. def is_on(self) -> bool | None:
  51. """Return bulb state from cache."""
  52. return self._get_adv_value("isOn")
  53. def get_effect(self):
  54. """Return the current effect."""
  55. return self._get_adv_value("effect")
  56. @abstractmethod
  57. async def turn_on(self) -> bool:
  58. """Turn device on."""
  59. @abstractmethod
  60. async def turn_off(self) -> bool:
  61. """Turn device off."""
  62. @abstractmethod
  63. async def set_brightness(self, brightness: int) -> bool:
  64. """Set brightness."""
  65. @abstractmethod
  66. async def set_color_temp(self, brightness: int, color_temp: int) -> bool:
  67. """Set color temp."""
  68. @abstractmethod
  69. async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
  70. """Set rgb."""
  71. async def _send_multiple_commands(self, keys: list[str]) -> bool:
  72. """
  73. Send multiple commands to device.
  74. Since we current have no way to tell which command the device
  75. needs we send both.
  76. """
  77. final_result = False
  78. for key in keys:
  79. result = await self._send_command(key)
  80. final_result |= self._check_command_result(result, 0, {1})
  81. return final_result
  82. class SwitchbotSequenceBaseLight(SwitchbotBaseLight):
  83. """Representation of a Switchbot light."""
  84. def update_from_advertisement(self, advertisement: SwitchBotAdvertisement) -> None:
  85. """Update device data from advertisement."""
  86. current_state = self._get_adv_value("sequence_number")
  87. super().update_from_advertisement(advertisement)
  88. new_state = self._get_adv_value("sequence_number")
  89. _LOGGER.debug(
  90. "%s: update advertisement: %s (seq before: %s) (seq after: %s)",
  91. self.name,
  92. advertisement,
  93. current_state,
  94. new_state,
  95. )
  96. if current_state != new_state:
  97. create_background_task(self.update())