Improve debug log when warning about a dip in total_increasing sensor (#62501)

This commit is contained in:
Erik Montnemery 2021-12-21 17:22:40 +01:00 committed by GitHub
parent e48f567176
commit 6151bbe5c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View File

@ -298,7 +298,9 @@ def _suggest_report_issue(hass: HomeAssistant, entity_id: str) -> str:
return report_issue return report_issue
def warn_dip(hass: HomeAssistant, entity_id: str, state: State) -> None: def warn_dip(
hass: HomeAssistant, entity_id: str, state: State, previous_fstate: float
) -> None:
"""Log a warning once if a sensor with state_class_total has a decreasing value. """Log a warning once if a sensor with state_class_total has a decreasing value.
The log will be suppressed until two dips have been seen to prevent warning due to The log will be suppressed until two dips have been seen to prevent warning due to
@ -319,11 +321,12 @@ def warn_dip(hass: HomeAssistant, entity_id: str, state: State) -> None:
return return
_LOGGER.warning( _LOGGER.warning(
"Entity %s %shas state class total_increasing, but its state is " "Entity %s %shas state class total_increasing, but its state is "
"not strictly increasing. Triggered by state %s with last_updated set to %s. " "not strictly increasing. Triggered by state %s (%s) with last_updated set to %s. "
"Please %s", "Please %s",
entity_id, entity_id,
f"from integration {domain} " if domain else "", f"from integration {domain} " if domain else "",
state.state, state.state,
previous_fstate,
state.last_updated.isoformat(), state.last_updated.isoformat(),
_suggest_report_issue(hass, entity_id), _suggest_report_issue(hass, entity_id),
) )
@ -359,7 +362,7 @@ def reset_detected(
return False return False
if 0.9 * previous_fstate <= fstate < previous_fstate: if 0.9 * previous_fstate <= fstate < previous_fstate:
warn_dip(hass, entity_id, state) warn_dip(hass, entity_id, state, previous_fstate)
if fstate < 0: if fstate < 0:
warn_negative(hass, entity_id, state) warn_negative(hass, entity_id, state)

View File

@ -1032,12 +1032,13 @@ def test_compile_hourly_sum_statistics_total_increasing_small_dip(
recorder.do_adhoc_statistics(start=period2) recorder.do_adhoc_statistics(start=period2)
wait_recording_done(hass) wait_recording_done(hass)
state = states["sensor.test1"][6].state state = states["sensor.test1"][6].state
previous_state = float(states["sensor.test1"][5].state)
last_updated = states["sensor.test1"][6].last_updated.isoformat() last_updated = states["sensor.test1"][6].last_updated.isoformat()
assert ( assert (
"Entity sensor.test1 has state class total_increasing, but its state is not " "Entity sensor.test1 has state class total_increasing, but its state is not "
f"strictly increasing. Triggered by state {state} with last_updated set to " f"strictly increasing. Triggered by state {state} ({previous_state}) with "
f"{last_updated}. Please create a bug report at https://github.com/home-assistant" f"last_updated set to {last_updated}. Please create a bug report at "
"/core/issues?q=is%3Aopen+is%3Aissue" "https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue"
) in caplog.text ) in caplog.text
statistic_ids = list_statistic_ids(hass) statistic_ids = list_statistic_ids(hass)
assert statistic_ids == [ assert statistic_ids == [