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
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.
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
_LOGGER.warning(
"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",
entity_id,
f"from integration {domain} " if domain else "",
state.state,
previous_fstate,
state.last_updated.isoformat(),
_suggest_report_issue(hass, entity_id),
)
@ -359,7 +362,7 @@ def reset_detected(
return False
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:
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)
wait_recording_done(hass)
state = states["sensor.test1"][6].state
previous_state = float(states["sensor.test1"][5].state)
last_updated = states["sensor.test1"][6].last_updated.isoformat()
assert (
"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"{last_updated}. Please create a bug report at https://github.com/home-assistant"
"/core/issues?q=is%3Aopen+is%3Aissue"
f"strictly increasing. Triggered by state {state} ({previous_state}) with "
f"last_updated set to {last_updated}. Please create a bug report at "
"https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue"
) in caplog.text
statistic_ids = list_statistic_ids(hass)
assert statistic_ids == [