Jelajahi Sumber

WIP: Add support for the bulbs

J. Nick Koston 2 tahun lalu
induk
melakukan
7b7c0b8187
2 mengubah file dengan 37 tambahan dan 6 penghapusan
  1. 35 0
      switchbot/devices/bulb.py
  2. 2 6
      switchbot/devices/plug.py

+ 35 - 0
switchbot/devices/bulb.py

@@ -1 +1,36 @@
 from __future__ import annotations
+
+from typing import Any
+
+from .device import SwitchbotDevice
+
+# Plug Mini keys
+PLUG_ON_KEY = "570f50010180"
+PLUG_OFF_KEY = "570f50010100"
+
+
+class SwitchbotBulb(SwitchbotDevice):
+    """Representation of a Switchbot bulb."""
+
+    def __init__(self, *args: Any, **kwargs: Any) -> None:
+        """Switchbot plug mini constructor."""
+        super().__init__(*args, **kwargs)
+        self._settings: dict[str, Any] = {}
+
+    async def update(self, interface: int | None = None) -> None:
+        """Update state of device."""
+        await self.get_device_data(retry=self._retry_count, interface=interface)
+
+    async def turn_on(self) -> bool:
+        """Turn device on."""
+        result = await self._sendcommand(PLUG_ON_KEY, self._retry_count)
+        return result[1] == 0x80
+
+    async def turn_off(self) -> bool:
+        """Turn device off."""
+        result = await self._sendcommand(PLUG_OFF_KEY, self._retry_count)
+        return result[1] == 0x00
+
+    def is_on(self) -> bool | None:
+        """Return blub state from cache."""
+        return self._get_adv_value("isOn")

+ 2 - 6
switchbot/devices/plug.py

@@ -32,10 +32,6 @@ class SwitchbotPlugMini(SwitchbotDevice):
         result = await self._sendcommand(PLUG_OFF_KEY, self._retry_count)
         return result[1] == 0x00
 
-    def is_on(self) -> Any:
+    def is_on(self) -> bool | None:
         """Return switch state from cache."""
-        # To get actual position call update() first.
-        value = self._get_adv_value("isOn")
-        if value is None:
-            return None
-        return value
+        return self._get_adv_value("isOn")