1
0

base_light.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from __future__ import annotations
  2. import logging
  3. from abc import abstractmethod
  4. from typing import Any
  5. from .device import ColorMode, SwitchbotDevice
  6. _LOGGER = logging.getLogger(__name__)
  7. import asyncio
  8. from ..models import SwitchBotAdvertisement
  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. def color_mode(self) -> ColorMode:
  35. """Return the current color mode."""
  36. return ColorMode(self._get_adv_value("color_mode") or 0)
  37. @property
  38. def min_temp(self) -> int:
  39. """Return minimum color temp."""
  40. return 2700
  41. @property
  42. def max_temp(self) -> int:
  43. """Return maximum color temp."""
  44. return 6500
  45. def is_on(self) -> bool | None:
  46. """Return bulb state from cache."""
  47. return self._get_adv_value("isOn")
  48. @abstractmethod
  49. async def turn_on(self) -> bool:
  50. """Turn device on."""
  51. @abstractmethod
  52. async def turn_off(self) -> bool:
  53. """Turn device off."""
  54. @abstractmethod
  55. async def set_brightness(self, brightness: int) -> bool:
  56. """Set brightness."""
  57. @abstractmethod
  58. async def set_color_temp(self, brightness: int, color_temp: int) -> bool:
  59. """Set color temp."""
  60. @abstractmethod
  61. async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
  62. """Set rgb."""
  63. class SwitchbotSequenceBaseLight(SwitchbotBaseLight):
  64. """Representation of a Switchbot light."""
  65. def update_from_advertisement(self, advertisement: SwitchBotAdvertisement) -> None:
  66. """Update device data from advertisement."""
  67. current_state = self._get_adv_value("sequence_number")
  68. super().update_from_advertisement(advertisement)
  69. new_state = self._get_adv_value("sequence_number")
  70. _LOGGER.debug(
  71. "%s: update advertisement: %s (seq before: %s) (seq after: %s)",
  72. self.name,
  73. advertisement,
  74. current_state,
  75. new_state,
  76. )
  77. if current_state != new_state:
  78. asyncio.ensure_future(self.update())