3 Commits 57feb86868 ... a2698dcac1

Author SHA1 Message Date
  J. Nick Koston a2698dcac1 0.40.0 9 months ago
  J. Nick Koston 495490dbb0 Remove passive detection of contact sensors (#219) 9 months ago
  Tereza Tomcova 8c38ab81df Support Curtain 3 (#218) 9 months ago
5 changed files with 86 additions and 27 deletions
  1. 1 1
      setup.py
  2. 6 1
      switchbot/adv_parser.py
  3. 7 2
      switchbot/adv_parsers/curtain.py
  4. 3 1
      switchbot/discovery.py
  5. 69 22
      tests/test_adv_parser.py

+ 1 - 1
setup.py

@@ -12,7 +12,7 @@ setup(
         "boto3>=1.20.24",
         "requests>=2.28.1",
     ],
-    version="0.39.1",
+    version="0.40.0",
     description="A library to communicate with Switchbot",
     author="Daniel Hjelseth Hoyer",
     url="https://github.com/Danielhiversen/pySwitchbot/",

+ 6 - 1
switchbot/adv_parser.py

@@ -49,7 +49,6 @@ SUPPORTED_TYPES: dict[str, SwitchbotSupportedType] = {
         "modelFriendlyName": "Contact Sensor",
         "func": process_wocontact,
         "manufacturer_id": 2409,
-        "manufacturer_data_length": 13,
     },
     "H": {
         "modelName": SwitchbotModel.BOT,
@@ -71,6 +70,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."""

+ 69 - 22
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")
@@ -528,28 +593,10 @@ def test_contact_sensor_mfr_no_service_data():
         rssi=-70,
     )
     result = parse_advertisement_data(ble_device, adv_data)
-    assert result == SwitchBotAdvertisement(
-        address="aa:bb:cc:dd:ee:ff",
-        data={
-            "data": {
-                "battery": None,
-                "button_count": 4,
-                "contact_open": True,
-                "contact_timeout": True,
-                "is_light": False,
-                "motion_detected": False,
-                "tested": None,
-            },
-            "isEncrypted": False,
-            "model": "d",
-            "modelFriendlyName": "Contact Sensor",
-            "modelName": SwitchbotModel.CONTACT_SENSOR,
-            "rawAdvData": None,
-        },
-        device=ble_device,
-        rssi=-70,
-        active=False,
-    )
+    # Passive detection of contact sensor is not supported
+    # anymore since the Switchbot Curtain v3 was released
+    # which uses the heuristics for the contact sensor.
+    assert result is None
 
 
 def test_contact_sensor_srv():