Compare commits

...

3 Commits

Author SHA1 Message Date
Ville Skyttä
934eb12bf2 Fix state class info in warn_dip/negative docstrings 2025-11-29 11:00:45 +02:00
Ville Skyttä
58449458e6 Use state class constants in dip/negative warning messages 2025-11-29 11:00:44 +02:00
Ville Skyttä
564f6309e5 Clarify previous state in dip warning 2025-11-29 11:00:44 +02:00
2 changed files with 10 additions and 8 deletions

View File

@@ -394,7 +394,7 @@ def _suggest_report_issue(hass: HomeAssistant, entity_id: str) -> str:
def warn_dip( def warn_dip(
hass: HomeAssistant, entity_id: str, state: State, previous_fstate: float hass: HomeAssistant, entity_id: str, state: State, previous_fstate: float
) -> None: ) -> 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_INCREASING 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
rounding issues with databases storing the state as a single precision float, which rounding issues with databases storing the state as a single precision float, which
@@ -415,12 +415,13 @@ def warn_dip(
return return
_LOGGER.warning( _LOGGER.warning(
( (
"Entity %s %shas state class total_increasing, but its state is not" "Entity %s %shas state class %s, but its state is not"
" strictly increasing. Triggered by state %s (%s) with last_updated set" " strictly increasing. Triggered by state %s (previous %s) with"
" to %s. Please %s" " last_updated set to %s. Please %s"
), ),
entity_id, entity_id,
f"from integration {domain} " if domain else "", f"from integration {domain} " if domain else "",
SensorStateClass.TOTAL_INCREASING,
state.state, state.state,
previous_fstate, previous_fstate,
state.last_updated.isoformat(), state.last_updated.isoformat(),
@@ -429,7 +430,7 @@ def warn_dip(
def warn_negative(hass: HomeAssistant, entity_id: str, state: State) -> None: def warn_negative(hass: HomeAssistant, entity_id: str, state: State) -> None:
"""Log a warning once if a sensor with state_class_total has a negative value.""" """Log a warning once if a sensor with state class TOTAL_INCREASING has a negative value."""
if WARN_NEGATIVE not in hass.data: if WARN_NEGATIVE not in hass.data:
hass.data[WARN_NEGATIVE] = set() hass.data[WARN_NEGATIVE] = set()
if entity_id not in hass.data[WARN_NEGATIVE]: if entity_id not in hass.data[WARN_NEGATIVE]:
@@ -438,11 +439,12 @@ def warn_negative(hass: HomeAssistant, entity_id: str, state: State) -> None:
domain = entity_info["domain"] if entity_info else None domain = entity_info["domain"] if entity_info else None
_LOGGER.warning( _LOGGER.warning(
( (
"Entity %s %shas state class total_increasing, but its state is " "Entity %s %shas state class %s, but its state is "
"negative. Triggered by state %s with last_updated set to %s. Please %s" "negative. Triggered by state %s with last_updated set to %s. Please %s"
), ),
entity_id, entity_id,
f"from integration {domain} " if domain else "", f"from integration {domain} " if domain else "",
SensorStateClass.TOTAL_INCREASING,
state.state, state.state,
state.last_updated.isoformat(), state.last_updated.isoformat(),
_suggest_report_issue(hass, entity_id), _suggest_report_issue(hass, entity_id),

View File

@@ -2261,8 +2261,8 @@ async def test_compile_hourly_sum_statistics_total_increasing_small_dip(
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} ({previous_state}) with " f"strictly increasing. Triggered by state {state} (previous {previous_state}) "
f"last_updated set to {last_updated}. Please create a bug report at " f"with last_updated set to {last_updated}. Please create a bug report at "
"https://github.com/home-assistant/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 = await async_list_statistic_ids(hass) statistic_ids = await async_list_statistic_ids(hass)