Browse Source

Fix contact timeout never being set (#134)

J. Nick Koston 1 year ago
parent
commit
020197295b
2 changed files with 33 additions and 1 deletions
  1. 1 1
      switchbot/adv_parsers/contact.py
  2. 32 0
      tests/test_adv_parser.py

+ 1 - 1
switchbot/adv_parsers/contact.py

@@ -4,7 +4,7 @@ from __future__ import annotations
 
 def process_wocontact(data: bytes, mfr_data: bytes | None) -> dict[str, bool | int]:
     """Process woContact Sensor services data."""
-    contact_timeout = data[3] & 0b00000110 == 0b00000110
+    contact_timeout = data[3] & 0b00000100 == 0b00000100
     contact_open = data[3] & 0b00000010 == 0b00000010
     return {
         "tested": bool(data[1] & 0b10000000),

+ 32 - 0
tests/test_adv_parser.py

@@ -56,6 +56,38 @@ def test_parse_advertisement_data_curtain():
     )
 
 
+def test_parse_advertisement_data_contact():
+    """Test parse_advertisement_data for the contact sensor."""
+    ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")
+    adv_data = generate_advertisement_data(
+        manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
+        service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b'd@d\x05\x00u\x00\xf8\x12'},
+        rssi=-80,
+    )
+    result = parse_advertisement_data(ble_device, adv_data)
+    assert result == SwitchBotAdvertisement(
+        address="aa:bb:cc:dd:ee:ff",
+        data={
+            "rawAdvData": b'd@d\x05\x00u\x00\xf8\x12',
+            "data": {
+                "button_count": 2,
+                'contact_open': True,
+                'contact_timeout': True,
+                "is_light": True,
+                "battery": 100,
+                'motion_detected': True,
+                "tested": False
+            },
+            "isEncrypted": False,
+            "model": "d",
+            "modelFriendlyName":  'Contact Sensor',
+            "modelName": SwitchbotModel.CONTACT_SENSOR,
+        },
+        device=ble_device,
+        rssi=-80,
+    )
+
+
 def test_parse_advertisement_data_empty():
     """Test parse_advertisement_data with empty data does not blow up."""
     ble_device = BLEDevice("aa:bb:cc:dd:ee:ff", "any")