Skip to content

Commit

Permalink
Make sure energy sensors don't reset (return None instead of 0), make…
Browse files Browse the repository at this point in the history
… sure strings are not longer than 255 chars
  • Loading branch information
CJNE committed Sep 2, 2021
1 parent ed12a14 commit e9c3777
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions custom_components/sunspec/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ def unique_id(self):
def state(self):
"""Return the state of the sensor."""
val = self.coordinator.data[self.model_id].getValue(self.key, self.model_index)
# If this is an energy sensor a value of 0 woulld mess up long term stats because of how total_increasing works
if self.use_device_class == DEVICE_CLASS_ENERGY and val == 0:
_LOGGER.debug(
"Returning None instead of 0 for {self.name) to avoid resetting total_increasing counter"
)
return None
vtype = self._meta["type"]
if vtype in ("enum16", "bitfield32"):
symbols = self._meta.get("symbols", None)
Expand All @@ -167,13 +173,13 @@ def state(self):
if vtype == "enum16":
symbol = list(filter(lambda s: s["value"] == val, symbols))
if len(symbol) == 1:
return symbol[0]["name"]
return symbol[0]["name"][:255]
else:
symbols = list(
filter(lambda s: (val >> int(s["value"])) & 1 == 1, symbols)
)
if len(symbols) > 0:
return ",".join(map(lambda s: s["name"], symbols))
return ",".join(map(lambda s: s["name"], symbols))[:255]
return ""
return val

Expand Down

0 comments on commit e9c3777

Please sign in to comment.