|
@@ -4,31 +4,72 @@ from typing import Any
|
|
|
|
|
|
from .device import SwitchbotDevice
|
|
from .device import SwitchbotDevice
|
|
|
|
|
|
-# Plug Mini keys
|
|
|
|
-PLUG_ON_KEY = "570f50010180"
|
|
|
|
-PLUG_OFF_KEY = "570f50010100"
|
|
|
|
|
|
+REQ_HEADER = "570f"
|
|
|
|
+BULB_COMMMAND_HEADER = "4701"
|
|
|
|
+BULB_REQUEST = f"{REQ_HEADER}4801"
|
|
|
|
+
|
|
|
|
+BULB_COMMAND = f"{REQ_HEADER}{BULB_COMMMAND_HEADER}"
|
|
|
|
+# Bulb keys
|
|
|
|
+BULB_ON_KEY = f"{BULB_COMMAND}01"
|
|
|
|
+BULB_OFF_KEY = f"{BULB_COMMAND}02"
|
|
|
|
+RGB_BRIGHTNESS_KEY = f"{BULB_COMMAND}12"
|
|
|
|
+CW_BRIGHTNESS_KEY = f"{BULB_COMMAND}13"
|
|
|
|
+BRIGHTNESS_KEY = f"{BULB_COMMAND}14"
|
|
|
|
+RGB_KEY = f"{BULB_COMMAND}16"
|
|
|
|
+CW_KEY = f"{BULB_COMMAND}17"
|
|
|
|
|
|
|
|
|
|
class SwitchbotBulb(SwitchbotDevice):
|
|
class SwitchbotBulb(SwitchbotDevice):
|
|
"""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:
|
|
- """Switchbot plug mini constructor."""
|
|
|
|
|
|
+ """Switchbot bulb constructor."""
|
|
super().__init__(*args, **kwargs)
|
|
super().__init__(*args, **kwargs)
|
|
self._settings: dict[str, Any] = {}
|
|
self._settings: dict[str, Any] = {}
|
|
|
|
|
|
async def update(self, interface: int | None = None) -> None:
|
|
async def update(self, interface: int | None = None) -> None:
|
|
"""Update state of device."""
|
|
"""Update state of device."""
|
|
- await self.get_device_data(retry=self._retry_count, interface=interface)
|
|
|
|
|
|
+ result = await self._sendcommand(BULB_REQUEST)
|
|
|
|
|
|
async def turn_on(self) -> bool:
|
|
async def turn_on(self) -> bool:
|
|
"""Turn device on."""
|
|
"""Turn device on."""
|
|
- result = await self._sendcommand(PLUG_ON_KEY, self._retry_count)
|
|
|
|
|
|
+ result = await self._sendcommand(BULB_ON_KEY)
|
|
|
|
+ return result[1] == 0x80
|
|
|
|
+
|
|
|
|
+ async def turn_off(self) -> bool:
|
|
|
|
+ """Turn device off."""
|
|
|
|
+ result = await self._sendcommand(BULB_OFF_KEY)
|
|
|
|
+ return result[1] == 0x00
|
|
|
|
+
|
|
|
|
+ async def set_brightness(self, brightness: int) -> bool:
|
|
|
|
+ """Set brightness."""
|
|
|
|
+ assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
|
|
|
|
+ result = await self._sendcommand(f"{BRIGHTNESS_KEY}{brightness:02X}")
|
|
|
|
+ return result[1] == 0x80
|
|
|
|
+
|
|
|
|
+ async def set_color_temp(self, brightness: int, color_temp: int) -> bool:
|
|
|
|
+ """Set color temp."""
|
|
|
|
+ assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
|
|
|
|
+ assert 2700 <= color_temp <= 6500, "Color Temp must be between 0 and 100"
|
|
|
|
+ result = await self._sendcommand(
|
|
|
|
+ f"{CW_BRIGHTNESS_KEY}{brightness:02X}{color_temp:04X}"
|
|
|
|
+ )
|
|
|
|
+ return result[1] == 0x80
|
|
|
|
+
|
|
|
|
+ async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
|
|
|
|
+ """Set rgb."""
|
|
|
|
+ assert 0 <= brightness <= 100, "Brightness must be between 0 and 100"
|
|
|
|
+ assert 0 <= r <= 255, "r must be between 0 and 255"
|
|
|
|
+ assert 0 <= g <= 255, "g must be between 0 and 255"
|
|
|
|
+ assert 0 <= b <= 255, "b must be between 0 and 255"
|
|
|
|
+ result = await self._sendcommand(
|
|
|
|
+ f"{RGB_BRIGHTNESS_KEY}{brightness:02X}{r:02X}{g:02X}{b:02X}"
|
|
|
|
+ )
|
|
return result[1] == 0x80
|
|
return 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(PLUG_OFF_KEY, self._retry_count)
|
|
|
|
|
|
+ result = await self._sendcommand(BULB_OFF_KEY)
|
|
return result[1] == 0x00
|
|
return result[1] == 0x00
|
|
|
|
|
|
def is_on(self) -> bool | None:
|
|
def is_on(self) -> bool | None:
|