Drop last_reset attribute for non 'total' sensors (#63880)
* Drop last_reset attribute for non 'total' sensors * Adjust MQTT tests * Add exception for utility_meter * Rewrite exception for utility_meter * Add comment in utility_meter * Tweak commentpull/63623/head
parent
f3bc9fc740
commit
3083f059cc
|
@ -308,15 +308,16 @@ class SensorEntity(Entity):
|
|||
"""Return state attributes."""
|
||||
if last_reset := self.last_reset:
|
||||
if (
|
||||
self.state_class == SensorStateClass.MEASUREMENT
|
||||
self.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 "
|
||||
"deprecated and will be removed from Home Assistant Core 2021.11. "
|
||||
"not supported. "
|
||||
"Please update your configuration if state_class is manually "
|
||||
"configured, otherwise %s",
|
||||
self.entity_id,
|
||||
|
@ -325,7 +326,8 @@ class SensorEntity(Entity):
|
|||
report_issue,
|
||||
)
|
||||
|
||||
return {ATTR_LAST_RESET: last_reset.isoformat()}
|
||||
if self.state_class == SensorStateClass.TOTAL:
|
||||
return {ATTR_LAST_RESET: last_reset.isoformat()}
|
||||
|
||||
return None
|
||||
|
||||
|
|
|
@ -398,14 +398,17 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
|
|||
state_attr[ATTR_CRON_PATTERN] = self._cron_pattern
|
||||
if self._tariff is not None:
|
||||
state_attr[ATTR_TARIFF] = self._tariff
|
||||
# last_reset in utility meter was used before last_reset was added for long term
|
||||
# statistics in base sensor. base sensor only supports last reset
|
||||
# sensors with state_class set to total.
|
||||
# To avoid a breaking change we set last_reset directly
|
||||
# in extra state attributes.
|
||||
if last_reset := self._last_reset:
|
||||
state_attr[ATTR_LAST_RESET] = last_reset.isoformat()
|
||||
|
||||
return state_attr
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon to use in the frontend, if any."""
|
||||
return ICON
|
||||
|
||||
@property
|
||||
def last_reset(self):
|
||||
"""Return the time when the sensor was last reset."""
|
||||
return self._last_reset
|
||||
|
|
|
@ -272,6 +272,7 @@ async def test_setting_sensor_last_reset_via_mqtt_message(hass, mqtt_mock, caplo
|
|||
sensor.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_class": "total",
|
||||
"state_topic": "test-topic",
|
||||
"unit_of_measurement": "fav unit",
|
||||
"last_reset_topic": "last-reset-topic",
|
||||
|
@ -302,6 +303,7 @@ async def test_setting_sensor_bad_last_reset_via_mqtt_message(
|
|||
sensor.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_class": "total",
|
||||
"state_topic": "test-topic",
|
||||
"unit_of_measurement": "fav unit",
|
||||
"last_reset_topic": "last-reset-topic",
|
||||
|
@ -327,6 +329,7 @@ async def test_setting_sensor_empty_last_reset_via_mqtt_message(
|
|||
sensor.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_class": "total",
|
||||
"state_topic": "test-topic",
|
||||
"unit_of_measurement": "fav unit",
|
||||
"last_reset_topic": "last-reset-topic",
|
||||
|
@ -350,6 +353,7 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message(hass, mqtt_mock):
|
|||
sensor.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_class": "total",
|
||||
"state_topic": "test-topic",
|
||||
"unit_of_measurement": "fav unit",
|
||||
"last_reset_topic": "last-reset-topic",
|
||||
|
@ -379,6 +383,7 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message_2(
|
|||
**{
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_class": "total",
|
||||
"state_topic": "test-topic",
|
||||
"unit_of_measurement": "kWh",
|
||||
"value_template": "{{ value_json.value | float / 60000 }}",
|
||||
|
|
|
@ -81,12 +81,15 @@ async def test_deprecated_temperature_conversion(
|
|||
) in caplog.text
|
||||
|
||||
|
||||
async def test_deprecated_last_reset(hass, caplog, enable_custom_integrations):
|
||||
@pytest.mark.parametrize("state_class", ("measurement", "total_increasing"))
|
||||
async def test_deprecated_last_reset(
|
||||
hass, caplog, enable_custom_integrations, state_class
|
||||
):
|
||||
"""Test warning on deprecated last reset."""
|
||||
platform = getattr(hass.components, "test.sensor")
|
||||
platform.init(empty=True)
|
||||
platform.ENTITIES["0"] = platform.MockSensor(
|
||||
name="Test", state_class="measurement", last_reset=dt_util.utc_from_timestamp(0)
|
||||
name="Test", state_class=state_class, last_reset=dt_util.utc_from_timestamp(0)
|
||||
)
|
||||
|
||||
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
||||
|
@ -94,13 +97,15 @@ async def test_deprecated_last_reset(hass, caplog, enable_custom_integrations):
|
|||
|
||||
assert (
|
||||
"Entity sensor.test (<class 'custom_components.test.sensor.MockSensor'>) "
|
||||
"with state_class measurement has set last_reset. Setting last_reset for "
|
||||
"entities with state_class other than 'total' is deprecated and will be "
|
||||
"removed from Home Assistant Core 2021.11. Please update your configuration if "
|
||||
"state_class is manually configured, otherwise report it to the custom "
|
||||
"component author."
|
||||
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 custom component author."
|
||||
) in caplog.text
|
||||
|
||||
state = hass.states.get("sensor.test")
|
||||
assert "last_reset" not in state.attributes
|
||||
|
||||
|
||||
async def test_deprecated_unit_of_measurement(hass, caplog, enable_custom_integrations):
|
||||
"""Test warning on deprecated unit_of_measurement."""
|
||||
|
|
Loading…
Reference in New Issue