Browse Source

Add support for data with zero values from WoSensorTH (#230)

Co-authored-by: J. Nick Koston <nick@koston.org>
Sheldon Ip 7 months ago
parent
commit
b3cbda4e79
2 changed files with 28 additions and 1 deletions
  1. 5 1
      switchbot/adv_parsers/meter.py
  2. 23 0
      tests/test_adv_parser.py

+ 5 - 1
switchbot/adv_parsers/meter.py

@@ -26,13 +26,17 @@ def process_wosensorth(data: bytes | None, mfr_data: bytes | None) -> dict[str,
     )
     _temp_f = (_temp_c * 9 / 5) + 32
     _temp_f = (_temp_f * 10) / 10
+    humidity = temp_data[2] & 0b01111111
+
+    if _temp_c == 0 and humidity == 0 and battery == 0:
+        return {}
 
     _wosensorth_data = {
         # Data should be flat, but we keep the original structure for now
         "temp": {"c": _temp_c, "f": _temp_f},
         "temperature": _temp_c,
         "fahrenheit": bool(temp_data[2] & 0b10000000),
-        "humidity": temp_data[2] & 0b01111111,
+        "humidity": humidity,
         "battery": battery,
     }
 

+ 23 - 0
tests/test_adv_parser.py

@@ -940,6 +940,29 @@ def test_wosensor_passive_only():
     )
 
 
+def test_wosensor_active_zero_data():
+    """Test parsing wosensor with active data but all values are zero."""
+    ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
+    adv_data = generate_advertisement_data(
+        manufacturer_data={},
+        service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"T\x00\x00\x00\x00\x00"},
+        tx_power=-127,
+        rssi=-50,
+    )
+    result = parse_advertisement_data(ble_device, adv_data)
+    assert result == SwitchBotAdvertisement(
+        address="aa:bb:cc:dd:ee:ff",
+        data={
+            "data": {},
+            "isEncrypted": False,
+            "model": "T",
+            "rawAdvData": b"T\x00\x00\x00\x00\x00",
+        },
+        device=ble_device,
+        rssi=-50,
+        active=True,
+    )
+
 def test_woiosensor_passive_and_active():
     """Test parsing woiosensor as passive with active data as well."""
     ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")