Skip to content

Commit c7db35d

Browse files
authored
✨ Support unencrypted devices (#11)
Seems like some devices built after 2020 don't try to "encrypted" the data anymore. I've noticed this in a few repos, will do the check at runtime to avoid any guesswork.
1 parent ded2799 commit c7db35d

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

co2mini/meter.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def _hd(data):
2929
return " ".join("%02X" % e for e in data)
3030

3131

32+
def _is_valid_msg(data):
33+
return data[4] == 0x0D and (sum(data[:3]) & 0xFF) == data[3]
34+
35+
3236
class CO2Meter(threading.Thread):
3337
_key = [0xC4, 0xC6, 0xC0, 0x92, 0x40, 0x23, 0xDC, 0x96]
3438
_device = ""
@@ -58,10 +62,11 @@ def _read_data(self):
5862
"""
5963
try:
6064
data = list(self._file.read(8))
61-
decrypted = self._decrypt(data)
62-
if decrypted[4] != 0x0D or (sum(decrypted[:3]) & 0xFF) != decrypted[3]:
63-
logger.error("Checksum error: %s => %s", _hd(data), _hd(decrypted))
65+
if _is_valid_msg(data):
66+
decrypted = data
6467
else:
68+
decrypted = self._decrypt(data)
69+
if _is_valid_msg(decrypted):
6570
operation = decrypted[0]
6671
val = decrypted[1] << 8 | decrypted[2]
6772
self._values[operation] = _convert_value(operation, val)
@@ -70,6 +75,9 @@ def _read_data(self):
7075
operation == CO2METER_HUM and val != 0
7176
):
7277
self._callback(sensor=operation, value=self._values[operation])
78+
else:
79+
logger.error("Checksum error: %s => %s", _hd(data), _hd(decrypted))
80+
7381
except Exception:
7482
logger.exception("Exception reading data")
7583
self.running = False

0 commit comments

Comments
 (0)