|
@@ -26,9 +26,10 @@ CW_KEY = f"{BULB_COMMAND}17"
|
|
_LOGGER = logging.getLogger(__name__)
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
from .device import ColorMode
|
|
from .device import ColorMode
|
|
|
|
+from .base_light import SwitchbotBaseLight
|
|
|
|
|
|
|
|
|
|
-class SwitchbotBulb(SwitchbotSequenceDevice):
|
|
+class SwitchbotBulb(SwitchbotBaseLight):
|
|
"""Representation of a Switchbot bulb."""
|
|
"""Representation of a Switchbot bulb."""
|
|
|
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
@@ -36,48 +37,11 @@ class SwitchbotBulb(SwitchbotSequenceDevice):
|
|
super().__init__(*args, **kwargs)
|
|
super().__init__(*args, **kwargs)
|
|
self._state: dict[str, Any] = {}
|
|
self._state: dict[str, Any] = {}
|
|
|
|
|
|
- @property
|
|
|
|
- def on(self) -> bool | None:
|
|
|
|
- """Return if bulb is on."""
|
|
|
|
- return self.is_on()
|
|
|
|
-
|
|
|
|
- @property
|
|
|
|
- def rgb(self) -> tuple[int, int, int] | None:
|
|
|
|
- """Return the current rgb value."""
|
|
|
|
- if "r" not in self._state or "g" not in self._state or "b" not in self._state:
|
|
|
|
- return None
|
|
|
|
- return self._state["r"], self._state["g"], self._state["b"]
|
|
|
|
-
|
|
|
|
- @property
|
|
|
|
- def color_temp(self) -> int | None:
|
|
|
|
- """Return the current color temp value."""
|
|
|
|
- return self._state.get("cw") or self.min_temp
|
|
|
|
-
|
|
|
|
- @property
|
|
|
|
- def brightness(self) -> int | None:
|
|
|
|
- """Return the current brightness value."""
|
|
|
|
- return self._get_adv_value("brightness") or 0
|
|
|
|
-
|
|
|
|
- @property
|
|
|
|
- def color_mode(self) -> ColorMode:
|
|
|
|
- """Return the current color mode."""
|
|
|
|
- return ColorMode(self._get_adv_value("color_mode") or 0)
|
|
|
|
-
|
|
|
|
@property
|
|
@property
|
|
def color_modes(self) -> set[ColorMode]:
|
|
def color_modes(self) -> set[ColorMode]:
|
|
"""Return the supported color modes."""
|
|
"""Return the supported color modes."""
|
|
return {ColorMode.RGB, ColorMode.COLOR_TEMP}
|
|
return {ColorMode.RGB, ColorMode.COLOR_TEMP}
|
|
|
|
|
|
- @property
|
|
|
|
- def min_temp(self) -> int:
|
|
|
|
- """Return minimum color temp."""
|
|
|
|
- return 2700
|
|
|
|
-
|
|
|
|
- @property
|
|
|
|
- def max_temp(self) -> int:
|
|
|
|
- """Return maximum color temp."""
|
|
|
|
- return 6500
|
|
|
|
-
|
|
|
|
async def update(self) -> None:
|
|
async def update(self) -> None:
|
|
"""Update state of device."""
|
|
"""Update state of device."""
|
|
result = await self._sendcommand(BULB_REQUEST)
|
|
result = await self._sendcommand(BULB_REQUEST)
|
|
@@ -87,20 +51,20 @@ class SwitchbotBulb(SwitchbotSequenceDevice):
|
|
"""Turn device on."""
|
|
"""Turn device on."""
|
|
result = await self._sendcommand(BULB_ON_KEY)
|
|
result = await self._sendcommand(BULB_ON_KEY)
|
|
self._update_state(result)
|
|
self._update_state(result)
|
|
- return result[1] == 0x80
|
|
+ return self._check_command_result(result, 1, {0x80})
|
|
|
|
|
|
async def turn_off(self) -> bool:
|
|
async def turn_off(self) -> bool:
|
|
"""Turn device off."""
|
|
"""Turn device off."""
|
|
result = await self._sendcommand(BULB_OFF_KEY)
|
|
result = await self._sendcommand(BULB_OFF_KEY)
|
|
self._update_state(result)
|
|
self._update_state(result)
|
|
- return result[1] == 0x00
|
|
+ return self._check_command_result(result, 1, {0x00})
|
|
|
|
|
|
async def set_brightness(self, brightness: int) -> bool:
|
|
async def set_brightness(self, brightness: int) -> bool:
|
|
"""Set brightness."""
|
|
"""Set brightness."""
|
|
assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
|
|
assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
|
|
result = await self._sendcommand(f"{BRIGHTNESS_KEY}{brightness:02X}")
|
|
result = await self._sendcommand(f"{BRIGHTNESS_KEY}{brightness:02X}")
|
|
self._update_state(result)
|
|
self._update_state(result)
|
|
- return result[1] == 0x80
|
|
+ return self._check_command_result(result, 1, {0x80})
|
|
|
|
|
|
async def set_color_temp(self, brightness: int, color_temp: int) -> bool:
|
|
async def set_color_temp(self, brightness: int, color_temp: int) -> bool:
|
|
"""Set color temp."""
|
|
"""Set color temp."""
|
|
@@ -110,7 +74,7 @@ class SwitchbotBulb(SwitchbotSequenceDevice):
|
|
f"{CW_BRIGHTNESS_KEY}{brightness:02X}{color_temp:04X}"
|
|
f"{CW_BRIGHTNESS_KEY}{brightness:02X}{color_temp:04X}"
|
|
)
|
|
)
|
|
self._update_state(result)
|
|
self._update_state(result)
|
|
- return result[1] == 0x80
|
|
+ return self._check_command_result(result, 1, {0x80})
|
|
|
|
|
|
async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
|
|
async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
|
|
"""Set rgb."""
|
|
"""Set rgb."""
|
|
@@ -122,11 +86,7 @@ class SwitchbotBulb(SwitchbotSequenceDevice):
|
|
f"{RGB_BRIGHTNESS_KEY}{brightness:02X}{r:02X}{g:02X}{b:02X}"
|
|
f"{RGB_BRIGHTNESS_KEY}{brightness:02X}{r:02X}{g:02X}{b:02X}"
|
|
)
|
|
)
|
|
self._update_state(result)
|
|
self._update_state(result)
|
|
- return result[1] == 0x80
|
|
+ return self._check_command_result(result, 1, {0x80})
|
|
-
|
|
|
|
- def is_on(self) -> bool | None:
|
|
|
|
- """Return bulb state from cache."""
|
|
|
|
- return self._get_adv_value("isOn")
|
|
|
|
|
|
|
|
def _update_state(self, result: bytes) -> None:
|
|
def _update_state(self, result: bytes) -> None:
|
|
"""Update device state."""
|
|
"""Update device state."""
|