Limit zwave_js meter sensor last reset (#53921)
parent
f02259eb2d
commit
c959a0a484
|
@ -248,6 +248,7 @@ class ZWaveMeterSensor(ZWaveNumericSensor, RestoreEntity):
|
|||
|
||||
# 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
|
||||
|
@ -255,13 +256,12 @@ class ZWaveMeterSensor(ZWaveNumericSensor, RestoreEntity):
|
|||
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, ignore it
|
||||
if self.info.node != node or self.info.primary_value.endpoint != endpoint:
|
||||
return
|
||||
# If a meter type was specified and doesn't match this entity's meter type,
|
||||
# ignore it
|
||||
# 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 (
|
||||
meter_type is not None
|
||||
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
|
||||
):
|
||||
|
@ -274,6 +274,10 @@ class ZWaveMeterSensor(ZWaveNumericSensor, RestoreEntity):
|
|||
"""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:
|
||||
|
@ -310,7 +314,7 @@ class ZWaveMeterSensor(ZWaveNumericSensor, RestoreEntity):
|
|||
primary_value.endpoint,
|
||||
options,
|
||||
)
|
||||
self._attr_last_reset = dt.utcnow()
|
||||
|
||||
# Notify meters that may have been reset
|
||||
async_dispatcher_send(
|
||||
self.hass,
|
||||
|
|
|
@ -33,7 +33,8 @@ ID_LOCK_CONFIG_PARAMETER_SENSOR = (
|
|||
"sensor.z_wave_module_for_id_lock_150_and_101_config_parameter_door_lock_mode"
|
||||
)
|
||||
ZEN_31_ENTITY = "light.kitchen_under_cabinet_lights"
|
||||
METER_SENSOR = "sensor.smart_switch_6_electric_consumed_v"
|
||||
METER_ENERGY_SENSOR = "sensor.smart_switch_6_electric_consumed_kwh"
|
||||
METER_VOLTAGE_SENSOR = "sensor.smart_switch_6_electric_consumed_v"
|
||||
|
||||
DATETIME_ZERO = datetime(1970, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
|
||||
DATETIME_LAST_RESET = datetime(2020, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
|
||||
|
|
|
@ -36,7 +36,8 @@ from .common import (
|
|||
HUMIDITY_SENSOR,
|
||||
ID_LOCK_CONFIG_PARAMETER_SENSOR,
|
||||
INDICATOR_SENSOR,
|
||||
METER_SENSOR,
|
||||
METER_ENERGY_SENSOR,
|
||||
METER_VOLTAGE_SENSOR,
|
||||
NOTIFICATION_MOTION_SENSOR,
|
||||
POWER_SENSOR,
|
||||
VOLTAGE_SENSOR,
|
||||
|
@ -202,9 +203,13 @@ async def test_reset_meter(
|
|||
client.async_send_command.return_value = {}
|
||||
client.async_send_command_no_wait.return_value = {}
|
||||
|
||||
# Validate that non accumulating meter does not have a last reset attribute
|
||||
|
||||
assert ATTR_LAST_RESET not in hass.states.get(METER_VOLTAGE_SENSOR).attributes
|
||||
|
||||
# Validate that the sensor last reset is starting from nothing
|
||||
assert (
|
||||
hass.states.get(METER_SENSOR).attributes[ATTR_LAST_RESET]
|
||||
hass.states.get(METER_ENERGY_SENSOR).attributes[ATTR_LAST_RESET]
|
||||
== DATETIME_ZERO.isoformat()
|
||||
)
|
||||
|
||||
|
@ -215,13 +220,13 @@ async def test_reset_meter(
|
|||
DOMAIN,
|
||||
SERVICE_RESET_METER,
|
||||
{
|
||||
ATTR_ENTITY_ID: METER_SENSOR,
|
||||
ATTR_ENTITY_ID: METER_ENERGY_SENSOR,
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert (
|
||||
hass.states.get(METER_SENSOR).attributes[ATTR_LAST_RESET]
|
||||
hass.states.get(METER_ENERGY_SENSOR).attributes[ATTR_LAST_RESET]
|
||||
== DATETIME_LAST_RESET.isoformat()
|
||||
)
|
||||
|
||||
|
@ -232,6 +237,10 @@ async def test_reset_meter(
|
|||
assert args["endpoint"] == 0
|
||||
assert args["args"] == []
|
||||
|
||||
# Validate that non accumulating meter does not have a last reset attribute
|
||||
|
||||
assert ATTR_LAST_RESET not in hass.states.get(METER_VOLTAGE_SENSOR).attributes
|
||||
|
||||
client.async_send_command_no_wait.reset_mock()
|
||||
|
||||
# Test successful meter reset call with options
|
||||
|
@ -239,7 +248,7 @@ async def test_reset_meter(
|
|||
DOMAIN,
|
||||
SERVICE_RESET_METER,
|
||||
{
|
||||
ATTR_ENTITY_ID: METER_SENSOR,
|
||||
ATTR_ENTITY_ID: METER_ENERGY_SENSOR,
|
||||
ATTR_METER_TYPE: 1,
|
||||
ATTR_VALUE: 2,
|
||||
},
|
||||
|
@ -253,6 +262,10 @@ async def test_reset_meter(
|
|||
assert args["endpoint"] == 0
|
||||
assert args["args"] == [{"type": 1, "targetValue": 2}]
|
||||
|
||||
# Validate that non accumulating meter does not have a last reset attribute
|
||||
|
||||
assert ATTR_LAST_RESET not in hass.states.get(METER_VOLTAGE_SENSOR).attributes
|
||||
|
||||
client.async_send_command_no_wait.reset_mock()
|
||||
|
||||
|
||||
|
@ -265,6 +278,10 @@ async def test_restore_last_reset(
|
|||
):
|
||||
"""Test restoring last_reset on setup."""
|
||||
assert (
|
||||
hass.states.get(METER_SENSOR).attributes[ATTR_LAST_RESET]
|
||||
hass.states.get(METER_ENERGY_SENSOR).attributes[ATTR_LAST_RESET]
|
||||
== DATETIME_LAST_RESET.isoformat()
|
||||
)
|
||||
|
||||
# Validate that non accumulating meter does not have a last reset attribute
|
||||
|
||||
assert ATTR_LAST_RESET not in hass.states.get(METER_VOLTAGE_SENSOR).attributes
|
||||
|
|
Loading…
Reference in New Issue