Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Support unencrypted devices #11

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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