Remove last_reset attribute and set state class to total_increasing for zwave_js energy sensors (#54818)

This commit is contained in:
Erik Montnemery 2021-08-18 15:11:10 +02:00 committed by GitHub
parent aef8ec968b
commit 5536e24dec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 129 deletions

View file

@ -11,13 +11,13 @@ from zwave_js_server.model.node import Node as ZwaveNode
from zwave_js_server.model.value import ConfigurationValue
from homeassistant.components.sensor import (
ATTR_LAST_RESET,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_POWER,
DOMAIN as SENSOR_DOMAIN,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
)
from homeassistant.config_entries import ConfigEntry
@ -36,8 +36,6 @@ from homeassistant.helpers.dispatcher import (
async_dispatcher_send,
)
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.util import dt
from .const import ATTR_METER_TYPE, ATTR_VALUE, DATA_CLIENT, DOMAIN, SERVICE_RESET_METER
from .discovery import ZwaveDiscoveryInfo
@ -218,7 +216,7 @@ class ZWaveNumericSensor(ZwaveSensorBase):
return str(self.info.primary_value.metadata.unit)
class ZWaveMeterSensor(ZWaveNumericSensor, RestoreEntity):
class ZWaveMeterSensor(ZWaveNumericSensor):
"""Representation of a Z-Wave Meter CC sensor."""
def __init__(
@ -231,51 +229,10 @@ class ZWaveMeterSensor(ZWaveNumericSensor, RestoreEntity):
super().__init__(config_entry, client, info)
# Entity class attributes
self._attr_state_class = STATE_CLASS_MEASUREMENT
if self.device_class == DEVICE_CLASS_ENERGY:
self._attr_last_reset = dt.utc_from_timestamp(0)
@callback
def async_update_last_reset(
self, node: ZwaveNode, endpoint: int, meter_type: int | None
) -> None:
"""Update last reset."""
# If the signal is not for this node or is for a different endpoint,
# or a meter type was specified and doesn't match this entity's meter type:
if (
self.info.node != node
or self.info.primary_value.endpoint != endpoint
or meter_type is not None
and self.info.primary_value.metadata.cc_specific.get("meterType")
!= meter_type
):
return
self._attr_last_reset = dt.utcnow()
self.async_write_ha_state()
async def async_added_to_hass(self) -> None:
"""Call when entity is added."""
await super().async_added_to_hass()
# If the meter is not an accumulating meter type, do not reset.
if self.device_class != DEVICE_CLASS_ENERGY:
return
# Restore the last reset time from stored state
restored_state = await self.async_get_last_state()
if restored_state and ATTR_LAST_RESET in restored_state.attributes:
self._attr_last_reset = dt.parse_datetime(
restored_state.attributes[ATTR_LAST_RESET]
)
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"{DOMAIN}_{SERVICE_RESET_METER}",
self.async_update_last_reset,
)
)
self._attr_state_class = STATE_CLASS_TOTAL_INCREASING
else:
self._attr_state_class = STATE_CLASS_MEASUREMENT
async def async_reset_meter(
self, meter_type: int | None = None, value: int | None = None