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 comment
This commit is contained in:
parent
f3bc9fc740
commit
3083f059cc
4 changed files with 30 additions and 15 deletions
|
@ -308,15 +308,16 @@ class SensorEntity(Entity):
|
||||||
"""Return state attributes."""
|
"""Return state attributes."""
|
||||||
if last_reset := self.last_reset:
|
if last_reset := self.last_reset:
|
||||||
if (
|
if (
|
||||||
self.state_class == SensorStateClass.MEASUREMENT
|
self.state_class != SensorStateClass.TOTAL
|
||||||
and not self._last_reset_reported
|
and not self._last_reset_reported
|
||||||
):
|
):
|
||||||
self._last_reset_reported = True
|
self._last_reset_reported = True
|
||||||
report_issue = self._suggest_report_issue()
|
report_issue = self._suggest_report_issue()
|
||||||
|
# This should raise in Home Assistant Core 2022.5
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Entity %s (%s) with state_class %s has set last_reset. Setting "
|
"Entity %s (%s) with state_class %s has set last_reset. Setting "
|
||||||
"last_reset for entities with state_class other than 'total' is "
|
"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 "
|
"Please update your configuration if state_class is manually "
|
||||||
"configured, otherwise %s",
|
"configured, otherwise %s",
|
||||||
self.entity_id,
|
self.entity_id,
|
||||||
|
@ -325,7 +326,8 @@ class SensorEntity(Entity):
|
||||||
report_issue,
|
report_issue,
|
||||||
)
|
)
|
||||||
|
|
||||||
return {ATTR_LAST_RESET: last_reset.isoformat()}
|
if self.state_class == SensorStateClass.TOTAL:
|
||||||
|
return {ATTR_LAST_RESET: last_reset.isoformat()}
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
@ -398,14 +398,17 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
|
||||||
state_attr[ATTR_CRON_PATTERN] = self._cron_pattern
|
state_attr[ATTR_CRON_PATTERN] = self._cron_pattern
|
||||||
if self._tariff is not None:
|
if self._tariff is not None:
|
||||||
state_attr[ATTR_TARIFF] = self._tariff
|
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
|
return state_attr
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the icon to use in the frontend, if any."""
|
"""Return the icon to use in the frontend, if any."""
|
||||||
return ICON
|
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: {
|
sensor.DOMAIN: {
|
||||||
"platform": "mqtt",
|
"platform": "mqtt",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
|
"state_class": "total",
|
||||||
"state_topic": "test-topic",
|
"state_topic": "test-topic",
|
||||||
"unit_of_measurement": "fav unit",
|
"unit_of_measurement": "fav unit",
|
||||||
"last_reset_topic": "last-reset-topic",
|
"last_reset_topic": "last-reset-topic",
|
||||||
|
@ -302,6 +303,7 @@ async def test_setting_sensor_bad_last_reset_via_mqtt_message(
|
||||||
sensor.DOMAIN: {
|
sensor.DOMAIN: {
|
||||||
"platform": "mqtt",
|
"platform": "mqtt",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
|
"state_class": "total",
|
||||||
"state_topic": "test-topic",
|
"state_topic": "test-topic",
|
||||||
"unit_of_measurement": "fav unit",
|
"unit_of_measurement": "fav unit",
|
||||||
"last_reset_topic": "last-reset-topic",
|
"last_reset_topic": "last-reset-topic",
|
||||||
|
@ -327,6 +329,7 @@ async def test_setting_sensor_empty_last_reset_via_mqtt_message(
|
||||||
sensor.DOMAIN: {
|
sensor.DOMAIN: {
|
||||||
"platform": "mqtt",
|
"platform": "mqtt",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
|
"state_class": "total",
|
||||||
"state_topic": "test-topic",
|
"state_topic": "test-topic",
|
||||||
"unit_of_measurement": "fav unit",
|
"unit_of_measurement": "fav unit",
|
||||||
"last_reset_topic": "last-reset-topic",
|
"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: {
|
sensor.DOMAIN: {
|
||||||
"platform": "mqtt",
|
"platform": "mqtt",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
|
"state_class": "total",
|
||||||
"state_topic": "test-topic",
|
"state_topic": "test-topic",
|
||||||
"unit_of_measurement": "fav unit",
|
"unit_of_measurement": "fav unit",
|
||||||
"last_reset_topic": "last-reset-topic",
|
"last_reset_topic": "last-reset-topic",
|
||||||
|
@ -379,6 +383,7 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message_2(
|
||||||
**{
|
**{
|
||||||
"platform": "mqtt",
|
"platform": "mqtt",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
|
"state_class": "total",
|
||||||
"state_topic": "test-topic",
|
"state_topic": "test-topic",
|
||||||
"unit_of_measurement": "kWh",
|
"unit_of_measurement": "kWh",
|
||||||
"value_template": "{{ value_json.value | float / 60000 }}",
|
"value_template": "{{ value_json.value | float / 60000 }}",
|
||||||
|
|
|
@ -81,12 +81,15 @@ async def test_deprecated_temperature_conversion(
|
||||||
) in caplog.text
|
) 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."""
|
"""Test warning on deprecated last reset."""
|
||||||
platform = getattr(hass.components, "test.sensor")
|
platform = getattr(hass.components, "test.sensor")
|
||||||
platform.init(empty=True)
|
platform.init(empty=True)
|
||||||
platform.ENTITIES["0"] = platform.MockSensor(
|
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"}})
|
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 (
|
assert (
|
||||||
"Entity sensor.test (<class 'custom_components.test.sensor.MockSensor'>) "
|
"Entity sensor.test (<class 'custom_components.test.sensor.MockSensor'>) "
|
||||||
"with state_class measurement has set last_reset. Setting last_reset for "
|
f"with state_class {state_class} has set last_reset. Setting last_reset for "
|
||||||
"entities with state_class other than 'total' is deprecated and will be "
|
"entities with state_class other than 'total' is not supported. Please update "
|
||||||
"removed from Home Assistant Core 2021.11. Please update your configuration if "
|
"your configuration if state_class is manually configured, otherwise report it "
|
||||||
"state_class is manually configured, otherwise report it to the custom "
|
"to the custom component author."
|
||||||
"component author."
|
|
||||||
) in caplog.text
|
) 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):
|
async def test_deprecated_unit_of_measurement(hass, caplog, enable_custom_integrations):
|
||||||
"""Test warning on deprecated unit_of_measurement."""
|
"""Test warning on deprecated unit_of_measurement."""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue