Improve statistics error messages when sensor's unit is changing (#55436)

* Improve error messages when sensor's unit is changing

* Improve test coverage
This commit is contained in:
Erik Montnemery 2021-08-30 12:51:46 +02:00 committed by GitHub
parent 7e9f8de7e0
commit 722aa0895e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 3 deletions

View file

@ -200,11 +200,18 @@ def _normalize_states(
hass.data[WARN_UNSTABLE_UNIT] = set()
if entity_id not in hass.data[WARN_UNSTABLE_UNIT]:
hass.data[WARN_UNSTABLE_UNIT].add(entity_id)
extra = ""
if old_metadata := statistics.get_metadata(hass, entity_id):
extra = (
" and matches the unit of already compiled statistics "
f"({old_metadata['unit_of_measurement']})"
)
_LOGGER.warning(
"The unit of %s is changing, got %s, generation of long term "
"statistics will be suppressed unless the unit is stable",
"The unit of %s is changing, got multiple %s, generation of long term "
"statistics will be suppressed unless the unit is stable%s",
entity_id,
all_units,
extra,
)
return None, []
unit = fstates[0][1].attributes.get(ATTR_UNIT_OF_MEASUREMENT)
@ -320,7 +327,7 @@ def compile_statistics(
entity_id,
unit,
old_metadata["unit_of_measurement"],
unit,
old_metadata["unit_of_measurement"],
)
continue