ceiling_light.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. _set_night_light_command = f"{CEILING_LIGHT_CONTROL_HEADER}01{{}}01{{}}"
  25. _get_basic_info_command = ["5702", "570f5581"]
  26. @property
  27. def color_modes(self) -> set[ColorMode]:
  28. """Return the supported color modes."""
  29. return {ColorMode.COLOR_TEMP}
  30. @property
  31. def color_mode(self) -> ColorMode:
  32. """Return the current color mode."""
  33. value = self._state.get("color_mode", self._get_adv_value("color_mode"))
  34. device_mode = CeilingLightColorMode(value if value is not None else 10)
  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. @update_after_operation
  46. async def set_night_light(self, is_on: bool, brightness: int | None = None) -> bool:
  47. """
  48. Toggle night light color mode on or off.
  49. Powers the light on and, unless `brightness` is given, resets
  50. brightness to 20% (night) or 100% (otherwise) — mirroring the
  51. official app's night light toggle.
  52. """
  53. color_mode = (
  54. CeilingLightColorMode.NIGHT if is_on else CeilingLightColorMode.COLOR_TEMP
  55. )
  56. hex_mode = f"{color_mode.value:02X}"
  57. if brightness is None:
  58. # The app's night light toggle always pairs NIGHT with 20%
  59. # brightness and COLOR_TEMP with 100%, carrying over the
  60. # last-set color temp.
  61. brightness = 20 if is_on else 100
  62. else:
  63. self._validate_brightness(brightness)
  64. color_temp = self._state.get("cw", DEFAULT_COLOR_TEMP)
  65. hex_data = f"{brightness:02X}{color_temp:04X}"
  66. result = await self._send_command(
  67. self._set_night_light_command.format(hex_mode, hex_data)
  68. )
  69. return self._check_command_result(result, 0, {1})
  70. def is_night_light_on(self) -> bool | None:
  71. """Return the cached night light color mode state."""
  72. value = self._state.get("color_mode")
  73. if value is None:
  74. return None
  75. return value == CeilingLightColorMode.NIGHT.value
  76. async def get_basic_info(self) -> dict[str, Any] | None:
  77. """Get device basic settings."""
  78. if not (
  79. res := await self._get_multi_commands_results(self._get_basic_info_command)
  80. ):
  81. return None
  82. _version_info, _data = res
  83. color_temp = int.from_bytes(_data[3:5], "big")
  84. if self.min_temp <= color_temp <= self.max_temp:
  85. self._state["cw"] = color_temp
  86. else:
  87. self._state.setdefault("cw", DEFAULT_COLOR_TEMP)
  88. # Cached in self._state (not the adv-data merge) because the
  89. # advertisement parser hardcodes color_mode, which would otherwise
  90. # clobber this value on the next advertisement.
  91. self._state["color_mode"] = (_data[1] & 0b01000000) >> 6
  92. return {
  93. "isOn": bool(_data[1] & 0b10000000),
  94. "color_mode": self._state["color_mode"],
  95. "brightness": _data[2] & 0b01111111,
  96. "cw": self._state["cw"],
  97. "firmware": _version_info[2] / 10.0,
  98. }