ceiling_light.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from __future__ import annotations
  2. import logging
  3. from typing import Any
  4. from .base_light import SwitchbotBaseLight
  5. from .device import REQ_HEADER, ColorMode
  6. CEILING_LIGHT_COMMAND_HEADER = "5401"
  7. CEILING_LIGHT_REQUEST = f"{REQ_HEADER}5501"
  8. CEILING_LIGHT_COMMAND = f"{REQ_HEADER}{CEILING_LIGHT_COMMAND_HEADER}"
  9. CEILING_LIGHT_ON_KEY = f"{CEILING_LIGHT_COMMAND}01FF01FFFF"
  10. CEILING_LIGHT_OFF_KEY = f"{CEILING_LIGHT_COMMAND}02FF01FFFF"
  11. CW_BRIGHTNESS_KEY = f"{CEILING_LIGHT_COMMAND}010001"
  12. BRIGHTNESS_KEY = f"{CEILING_LIGHT_COMMAND}01FF01"
  13. _LOGGER = logging.getLogger(__name__)
  14. class SwitchbotCeilingLight(SwitchbotBaseLight):
  15. """Representation of a Switchbot bulb."""
  16. @property
  17. def color_modes(self) -> set[ColorMode]:
  18. """Return the supported color modes."""
  19. return {ColorMode.COLOR_TEMP}
  20. async def update(self) -> None:
  21. """Update state of device."""
  22. async def turn_on(self) -> bool:
  23. """Turn device on."""
  24. result = await self._send_command(CEILING_LIGHT_ON_KEY)
  25. ret = self._check_command_result(result, 0, {0x01})
  26. self._override_adv_data = {"isOn": True}
  27. self._fire_callbacks()
  28. return ret
  29. async def turn_off(self) -> bool:
  30. """Turn device off."""
  31. result = await self._send_command(CEILING_LIGHT_OFF_KEY)
  32. ret = self._check_command_result(result, 0, {0x01})
  33. self._override_adv_data = {"isOn": False}
  34. self._fire_callbacks()
  35. return ret
  36. async def set_brightness(self, brightness: int) -> bool:
  37. """Set brightness."""
  38. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  39. result = await self._send_command(f"{BRIGHTNESS_KEY}{brightness:02X}0FA1")
  40. ret = self._check_command_result(result, 0, {0x01})
  41. self._override_adv_data = {"brightness": brightness, "isOn": True}
  42. self._fire_callbacks()
  43. return ret
  44. async def set_color_temp(self, brightness: int, color_temp: int) -> bool:
  45. """Set color temp."""
  46. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  47. assert 2700 <= color_temp <= 6500, "Color Temp must be between 0 and 100"
  48. result = await self._send_command(
  49. f"{CW_BRIGHTNESS_KEY}{brightness:02X}{color_temp:04X}"
  50. )
  51. ret = self._check_command_result(result, 0, {0x01})
  52. self._state["cw"] = color_temp
  53. self._override_adv_data = {"brightness": brightness, "isOn": True}
  54. self._fire_callbacks()
  55. return ret
  56. async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
  57. """Set rgb."""
  58. # Not supported on this device