Skip to content

Commit b6ef3dc

Browse files
authored
Add support for reading the co2 value from meter pro (#260)
1 parent eec843f commit b6ef3dc

File tree

2 files changed

+11
-36
lines changed

2 files changed

+11
-36
lines changed

switchbot/adv_parsers/meter.py

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
"""Meter parser."""
22
from __future__ import annotations
33

4+
import struct
45
from typing import Any
56

7+
CO2_UNPACK = struct.Struct(">H").unpack_from
8+
69

710
def process_wosensorth(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]:
811
"""Process woSensorTH/Temp sensor services data."""
9-
temp_data = None
10-
battery = None
12+
temp_data: bytes | None = None
13+
battery: bytes | None = None
1114

1215
if mfr_data:
1316
temp_data = mfr_data[8:11]
@@ -45,38 +48,8 @@ def process_wosensorth(data: bytes | None, mfr_data: bytes | None) -> dict[str,
4548

4649
def process_wosensorth_c(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]:
4750
"""Process woSensorTH/Temp sensor services data with CO2."""
48-
temp_data = None
49-
battery = None
50-
51-
if mfr_data:
52-
temp_data = mfr_data[8:11]
53-
54-
if data:
55-
if not temp_data:
56-
temp_data = data[3:6]
57-
battery = data[2] & 0b01111111
58-
59-
if not temp_data:
60-
return {}
61-
62-
_temp_sign = 1 if temp_data[1] & 0b10000000 else -1
63-
_temp_c = _temp_sign * (
64-
(temp_data[1] & 0b01111111) + ((temp_data[0] & 0b00001111) / 10)
65-
)
66-
_temp_f = (_temp_c * 9 / 5) + 32
67-
_temp_f = (_temp_f * 10) / 10
68-
humidity = temp_data[2] & 0b01111111
69-
70-
if _temp_c == 0 and humidity == 0 and battery == 0:
71-
return {}
72-
73-
_wosensorth_data = {
74-
# Data should be flat, but we keep the original structure for now
75-
"temp": {"c": _temp_c, "f": _temp_f},
76-
"temperature": _temp_c,
77-
"fahrenheit": bool(temp_data[2] & 0b10000000),
78-
"humidity": humidity,
79-
"battery": battery,
80-
}
81-
51+
_wosensorth_data = process_wosensorth(data, mfr_data)
52+
if _wosensorth_data and mfr_data and len(mfr_data) >= 15:
53+
co2_data = mfr_data[13:15]
54+
_wosensorth_data["co2"] = CO2_UNPACK(co2_data)[0]
8255
return _wosensorth_data

tests/test_adv_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,7 @@ def test_meter_pro_c_active() -> None:
16401640
"humidity": 36,
16411641
"temp": {"c": 27.7, "f": 81.86},
16421642
"temperature": 27.7,
1643+
"co2": 725,
16431644
},
16441645
"isEncrypted": False,
16451646
"model": "5",
@@ -1671,6 +1672,7 @@ def test_meter_pro_c_passive() -> None:
16711672
"humidity": 36,
16721673
"temp": {"c": 27.7, "f": 81.86},
16731674
"temperature": 27.7,
1675+
"co2": 725,
16741676
},
16751677
"isEncrypted": False,
16761678
"model": "5",

0 commit comments

Comments
 (0)