1
0

ceiling_light.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from __future__ import annotations
  2. from typing import Any
  3. from ..const.light import (
  4. DEFAULT_COLOR_TEMP,
  5. CeilingLightColorMode,
  6. ColorMode,
  7. )
  8. from .base_light import SwitchbotSequenceBaseLight
  9. from .device import update_after_operation
  10. # Private mapping from device-specific color modes to original ColorMode enum
  11. _CEILING_LIGHT_COLOR_MODE_MAP = {
  12. CeilingLightColorMode.COLOR_TEMP: ColorMode.COLOR_TEMP,
  13. CeilingLightColorMode.NIGHT: ColorMode.COLOR_TEMP,
  14. CeilingLightColorMode.MUSIC: ColorMode.EFFECT,
  15. CeilingLightColorMode.UNKNOWN: ColorMode.OFF,
  16. }
  17. CEILING_LIGHT_CONTROL_HEADER = "570F5401"
  18. class SwitchbotCeilingLight(SwitchbotSequenceBaseLight):
  19. """Representation of a Switchbot ceiling light."""
  20. _turn_on_command = f"{CEILING_LIGHT_CONTROL_HEADER}01FF01FFFF"
  21. _turn_off_command = f"{CEILING_LIGHT_CONTROL_HEADER}02FF01FFFF"
  22. _set_brightness_command = f"{CEILING_LIGHT_CONTROL_HEADER}01FF01{{}}"
  23. _set_color_temp_command = f"{CEILING_LIGHT_CONTROL_HEADER}01FF01{{}}"
  24. _get_basic_info_command = ["5702", "570f5581"]
  25. @property
  26. def color_modes(self) -> set[ColorMode]:
  27. """Return the supported color modes."""
  28. return {ColorMode.COLOR_TEMP}
  29. @property
  30. def color_mode(self) -> ColorMode:
  31. """Return the current color mode."""
  32. device_mode = CeilingLightColorMode(
  33. value if (value := self._get_adv_value("color_mode")) is not None else 10
  34. )
  35. return _CEILING_LIGHT_COLOR_MODE_MAP.get(device_mode, ColorMode.OFF)
  36. @update_after_operation
  37. async def set_brightness(self, brightness: int) -> bool:
  38. """Set brightness."""
  39. assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
  40. hex_brightness = f"{brightness:02X}"
  41. color_temp = self._state.get("cw", DEFAULT_COLOR_TEMP)
  42. hex_data = f"{hex_brightness}{color_temp:04X}"
  43. result = await self._send_command(self._set_brightness_command.format(hex_data))
  44. return self._check_command_result(result, 0, {1})
  45. async def get_basic_info(self) -> dict[str, Any] | None:
  46. """Get device basic settings."""
  47. if not (
  48. res := await self._get_multi_commands_results(self._get_basic_info_command)
  49. ):
  50. return None
  51. _version_info, _data = res
  52. self._state["cw"] = int.from_bytes(_data[3:5], "big")
  53. return {
  54. "isOn": bool(_data[1] & 0b10000000),
  55. "color_mode": _data[1] & 0b01000000,
  56. "brightness": _data[2] & 0b01111111,
  57. "cw": self._state["cw"],
  58. "firmware": _version_info[2] / 10.0,
  59. }