Skip to content

Commit

Permalink
✨ Support unencrypted devices (#11)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jerr0328 authored Nov 25, 2023
1 parent ded2799 commit c7db35d
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions co2mini/meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def _hd(data):
return " ".join("%02X" % e for e in data)


def _is_valid_msg(data):
return data[4] == 0x0D and (sum(data[:3]) & 0xFF) == data[3]


class CO2Meter(threading.Thread):
_key = [0xC4, 0xC6, 0xC0, 0x92, 0x40, 0x23, 0xDC, 0x96]
_device = ""
Expand Down Expand Up @@ -58,10 +62,11 @@ def _read_data(self):
"""
try:
data = list(self._file.read(8))
decrypted = self._decrypt(data)
if decrypted[4] != 0x0D or (sum(decrypted[:3]) & 0xFF) != decrypted[3]:
logger.error("Checksum error: %s => %s", _hd(data), _hd(decrypted))
if _is_valid_msg(data):
decrypted = data
else:
decrypted = self._decrypt(data)
if _is_valid_msg(decrypted):
operation = decrypted[0]
val = decrypted[1] << 8 | decrypted[2]
self._values[operation] = _convert_value(operation, val)
Expand All @@ -70,6 +75,9 @@ def _read_data(self):
operation == CO2METER_HUM and val != 0
):
self._callback(sensor=operation, value=self._values[operation])
else:
logger.error("Checksum error: %s => %s", _hd(data), _hd(decrypted))

except Exception:
logger.exception("Exception reading data")
self.running = False
Expand Down

0 comments on commit c7db35d

Please sign in to comment.