Raise ValueError when `last_reset` set and not `total` state class (#108391)

* Raise ValueError when last_reset set and not total state class

* Fix test

* Break long string into smaller ones
pull/109069/head
Jan-Philipp Benecke 2024-01-29 20:21:35 +01:00 committed by GitHub
parent aefae5bdae
commit 80bfd4cef7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 18 deletions

View File

@ -416,21 +416,12 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""Return state attributes."""
if last_reset := self.last_reset:
state_class = self.state_class
if state_class != SensorStateClass.TOTAL and not self._last_reset_reported:
self._last_reset_reported = True
report_issue = self._suggest_report_issue()
# This should raise in Home Assistant Core 2022.5
_LOGGER.warning(
(
"Entity %s (%s) with state_class %s has set last_reset. Setting"
" last_reset for entities with state_class other than 'total'"
" is not supported. Please update your configuration if"
" state_class is manually configured, otherwise %s"
),
self.entity_id,
type(self),
state_class,
report_issue,
if state_class != SensorStateClass.TOTAL:
raise ValueError(
f"Entity {self.entity_id} ({type(self)}) with state_class {state_class}"
" has set last_reset. Setting last_reset for entities with state_class"
" other than 'total' is not supported. Please update your configuration"
" if state_class is manually configured."
)
if state_class == SensorStateClass.TOTAL:

View File

@ -170,12 +170,11 @@ async def test_deprecated_last_reset(
"Entity sensor.test (<class 'custom_components.test.sensor.MockSensor'>) "
f"with state_class {state_class} has set last_reset. Setting last_reset for "
"entities with state_class other than 'total' is not supported. Please update "
"your configuration if state_class is manually configured, otherwise report it "
"to the author of the 'test' custom integration"
"your configuration if state_class is manually configured."
) in caplog.text
state = hass.states.get("sensor.test")
assert "last_reset" not in state.attributes
assert state is None
async def test_datetime_conversion(