Change data source for charger energy meter#235
Change data source for charger energy meter#235sveinse merged 5 commits intocustom-components:masterfrom
Conversation
* Create new ZaptecEnergySensor class * Implement a simple OCMF decoder to extract the latest energy meter reading
steinmn
left a comment
There was a problem hiding this comment.
This should work as written, but I think it can be simplified by ignoring time and just using the max value.
custom_components/zaptec/misc.py
Outdated
| def get_ocmf_latest_reader_value(data: dict) -> int: | ||
| """Return the latest reader value from OCMF data.""" | ||
|
|
||
| if not isinstance(data, dict): | ||
| return 0 | ||
| if "RD" not in data: | ||
| return 0 | ||
|
|
||
| # Find the latest reading | ||
| readings = [] | ||
| for reading in data["RD"]: | ||
| value = reading.get("RV") | ||
| if value is None: | ||
| continue | ||
| ts = None | ||
| m = RE_OCMF_TIMESTAMP.match(reading.get("TM", '')) | ||
| if m: | ||
| ts = datetime( | ||
| int(m[1]), # Year | ||
| int(m[2]), # Month | ||
| int(m[3]), # Day | ||
| int(m[4]), # Hours | ||
| int(m[5]), # Minutes | ||
| int(m[6]), # Seconds | ||
| int(m[7]) * 1000, # Milliseconds | ||
| tzinfo=timezone(timedelta(hours=int(m[8]), minutes=int(m[9]))), # Time zone | ||
| ) | ||
| readings.append((ts, value)) | ||
|
|
||
| # Sort readings by timestamp | ||
| readings.sort(key=lambda x: x[0]) | ||
| if readings: | ||
| return readings[-1][1] # Return the latest value | ||
| return 0 |
There was a problem hiding this comment.
Aren't we overcomplicating this? This is an increasing value that will never decrease, isn't it? So we just need the maximum value, right?
There was a problem hiding this comment.
Yes, I am. Fixed now.
custom_components/zaptec/sensor.py
Outdated
| ), | ||
| ZapSensorEntityDescription( | ||
| key="signed_meter_value_kwh", | ||
| key="signed_meter_value_kwh", # This key is unused, but kept for <0.7 compatibility |
There was a problem hiding this comment.
| key="signed_meter_value_kwh", # This key is unused, but kept for <0.7 compatibility | |
| key="signed_meter_value_kwh", # This key is no longer used to get Zaptec values, but is linked to the entity unique_id, so it is kept for <0.7 compatibility |
|
@steinmn Please review now |
steinmn
left a comment
There was a problem hiding this comment.
The energy meter values should be floats, otherwise LGTM
custom_components/zaptec/misc.py
Outdated
| raise AttributeError(f"Unknown record type {hex(record_type)}") | ||
|
|
||
|
|
||
| def get_ocmf_max_reader_value(data: dict) -> int: |
There was a problem hiding this comment.
| def get_ocmf_max_reader_value(data: dict) -> int: | |
| def get_ocmf_max_reader_value(data: dict) -> float: |
custom_components/zaptec/misc.py
Outdated
| if not isinstance(data, dict): | ||
| return 0 | ||
| return max( | ||
| int(reading.get("RV", 0)) |
There was a problem hiding this comment.
| int(reading.get("RV", 0)) | |
| float(reading.get("RV", 0)) |
|
Look at this from the positive side: With all these PRs back and forth we get some real hand-on experience with switching branches in git 😁 |
Fix #233, Fix #219