Browse Source

Support Curtain 3 (#218)

Tereza Tomcova 7 months ago
parent
commit
8c38ab81df
4 changed files with 81 additions and 3 deletions
  1. 6 0
      switchbot/adv_parser.py
  2. 7 2
      switchbot/adv_parsers/curtain.py
  3. 3 1
      switchbot/discovery.py
  4. 65 0
      tests/test_adv_parser.py

+ 6 - 0
switchbot/adv_parser.py

@@ -71,6 +71,12 @@ SUPPORTED_TYPES: dict[str, SwitchbotSupportedType] = {
         "manufacturer_id": 2409,
         "manufacturer_data_length": 16,
     },
+    "{": {
+        "modelName": SwitchbotModel.CURTAIN,
+        "modelFriendlyName": "Curtain 3",
+        "func": process_wocurtain,
+        "manufacturer_id": 2409,
+    },
     "c": {
         "modelName": SwitchbotModel.CURTAIN,
         "modelFriendlyName": "Curtain",

+ 7 - 2
switchbot/adv_parsers/curtain.py

@@ -6,10 +6,15 @@ def process_wocurtain(
     data: bytes | None, mfr_data: bytes | None, reverse: bool = True
 ) -> dict[str, bool | int]:
     """Process woCurtain/Curtain services data."""
-    if mfr_data and len(mfr_data) >= 11:
+    if mfr_data and len(mfr_data) >= 12: # Curtain 3
         device_data = mfr_data[8:11]
+        battery_data = mfr_data[12]
+    elif mfr_data and len(mfr_data) >= 11:
+        device_data = mfr_data[8:11]
+        battery_data = data[2] if data else None
     elif data:
         device_data = data[3:6]
+        battery_data = data[2]
     else:
         return {}
 
@@ -20,7 +25,7 @@ def process_wocurtain(
 
     return {
         "calibration": bool(data[1] & 0b01000000) if data else None,
-        "battery": data[2] & 0b01111111 if data else None,
+        "battery": battery_data & 0b01111111 if battery_data is not None else None,
         "inMotion": _in_motion,
         "position": (100 - _position) if reverse else _position,
         "lightLevel": _light_level,

+ 3 - 1
switchbot/discovery.py

@@ -93,7 +93,9 @@ class GetSwitchbotDevices:
         """Return all WoCurtain/Curtains devices with services data."""
         regular_curtains = await self._get_devices_by_model("c")
         pairing_curtains = await self._get_devices_by_model("C")
-        return {**regular_curtains, **pairing_curtains}
+        regular_curtains3 = await self._get_devices_by_model("{")
+        pairing_curtains3 = await self._get_devices_by_model("[")
+        return {**regular_curtains, **pairing_curtains, **regular_curtains3, **pairing_curtains3}
 
     async def get_bots(self) -> dict[str, SwitchBotAdvertisement]:
         """Return all WoHand/Bot devices with services data."""

+ 65 - 0
tests/test_adv_parser.py

@@ -322,6 +322,71 @@ def test_parse_advertisement_data_curtain_fully_open():
     )
 
 
+def test_parse_advertisement_data_curtain3():
+    """Test parse_advertisement_data for curtain 3."""
+    ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
+    adv_data = generate_advertisement_data(
+        manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xf7\x07\x00\x11\x04\x00\x49"},
+        service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"{\xc0\x49\x00\x11\x04"},
+        rssi=-80,
+    )
+
+    result = parse_advertisement_data(ble_device, adv_data)
+    assert result == SwitchBotAdvertisement(
+        address="aa:bb:cc:dd:ee:ff",
+        data={
+            "rawAdvData": b"{\xc0\x49\x00\x11\x04",
+            "data": {
+                "calibration": True,
+                "battery": 73,
+                "inMotion": False,
+                "position": 100,
+                "lightLevel": 1,
+                "deviceChain": 1,
+            },
+            "isEncrypted": False,
+            "model": "{",
+            "modelFriendlyName": "Curtain 3",
+            "modelName": SwitchbotModel.CURTAIN,
+        },
+        device=ble_device,
+        rssi=-80,
+        active=True,
+    )
+
+
+def test_parse_advertisement_data_curtain3_passive():
+    """Test parse_advertisement_data for curtain passive."""
+    ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
+    adv_data = generate_advertisement_data(
+        manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xf7\x07\x00\x11\x04\x00\x49"},
+        service_data={},
+        rssi=-80,
+    )
+    result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.CURTAIN)
+    assert result == SwitchBotAdvertisement(
+        address="aa:bb:cc:dd:ee:ff",
+        data={
+            "rawAdvData": None,
+            "data": {
+                "calibration": None,
+                "battery": 73,
+                "inMotion": False,
+                "position": 100,
+                "lightLevel": 1,
+                "deviceChain": 1,
+            },
+            "isEncrypted": False,
+            "model": "c",
+            "modelFriendlyName": "Curtain",
+            "modelName": SwitchbotModel.CURTAIN,
+        },
+        device=ble_device,
+        rssi=-80,
+        active=False,
+    )
+
+
 def test_parse_advertisement_data_contact():
     """Test parse_advertisement_data for the contact sensor."""
     ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")