Listen to node events in the zwave_js node status sensor (#55341)

This commit is contained in:
Raman Gupta 2021-08-27 18:01:20 -04:00 committed by GitHub
parent 1f37c215f6
commit 714564eaa6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 6 deletions

View file

@ -462,6 +462,7 @@ class ZWaveNodeStatusSensor(SensorEntity):
"""Poll a value.""" """Poll a value."""
raise ValueError("There is no value to poll for this entity") raise ValueError("There is no value to poll for this entity")
@callback
def _status_changed(self, _: dict) -> None: def _status_changed(self, _: dict) -> None:
"""Call when status event is received.""" """Call when status event is received."""
self._attr_native_value = self.node.status.name.lower() self._attr_native_value = self.node.status.name.lower()
@ -480,8 +481,3 @@ class ZWaveNodeStatusSensor(SensorEntity):
) )
) )
self.async_write_ha_state() self.async_write_ha_state()
@property
def available(self) -> bool:
"""Return entity availability."""
return self.client.connected and bool(self.node.ready)

View file

@ -769,6 +769,16 @@ def lock_id_lock_as_id150(client, lock_id_lock_as_id150_state):
return node return node
@pytest.fixture(name="lock_id_lock_as_id150_not_ready")
def node_not_ready(client, lock_id_lock_as_id150_state):
"""Mock an id lock id-150 lock node that's not ready."""
state = copy.deepcopy(lock_id_lock_as_id150_state)
state["ready"] = False
node = Node(client, state)
client.driver.controller.nodes[node.node_id] = node
return node
@pytest.fixture(name="climate_radio_thermostat_ct101_multiple_temp_units") @pytest.fixture(name="climate_radio_thermostat_ct101_multiple_temp_units")
def climate_radio_thermostat_ct101_multiple_temp_units_fixture( def climate_radio_thermostat_ct101_multiple_temp_units_fixture(
client, climate_radio_thermostat_ct101_multiple_temp_units_state client, climate_radio_thermostat_ct101_multiple_temp_units_state

View file

@ -23,6 +23,7 @@ from homeassistant.const import (
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
POWER_WATT, POWER_WATT,
STATE_UNAVAILABLE,
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
@ -136,7 +137,7 @@ async def test_config_parameter_sensor(hass, lock_id_lock_as_id150, integration)
assert entity_entry.disabled assert entity_entry.disabled
async def test_node_status_sensor(hass, lock_id_lock_as_id150, integration): async def test_node_status_sensor(hass, client, lock_id_lock_as_id150, integration):
"""Test node status sensor is created and gets updated on node state changes.""" """Test node status sensor is created and gets updated on node state changes."""
NODE_STATUS_ENTITY = "sensor.z_wave_module_for_id_lock_150_and_101_node_status" NODE_STATUS_ENTITY = "sensor.z_wave_module_for_id_lock_150_and_101_node_status"
node = lock_id_lock_as_id150 node = lock_id_lock_as_id150
@ -179,6 +180,44 @@ async def test_node_status_sensor(hass, lock_id_lock_as_id150, integration):
node.receive_event(event) node.receive_event(event)
assert hass.states.get(NODE_STATUS_ENTITY).state == "alive" assert hass.states.get(NODE_STATUS_ENTITY).state == "alive"
# Disconnect the client and make sure the entity is still available
await client.disconnect()
assert hass.states.get(NODE_STATUS_ENTITY).state != STATE_UNAVAILABLE
async def test_node_status_sensor_not_ready(
hass,
client,
lock_id_lock_as_id150_not_ready,
lock_id_lock_as_id150_state,
integration,
):
"""Test node status sensor is created and available if node is not ready."""
NODE_STATUS_ENTITY = "sensor.z_wave_module_for_id_lock_150_and_101_node_status"
node = lock_id_lock_as_id150_not_ready
assert not node.ready
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(NODE_STATUS_ENTITY)
assert entity_entry.disabled
assert entity_entry.disabled_by == er.DISABLED_INTEGRATION
updated_entry = ent_reg.async_update_entity(
entity_entry.entity_id, **{"disabled_by": None}
)
await hass.config_entries.async_reload(integration.entry_id)
await hass.async_block_till_done()
assert not updated_entry.disabled
assert hass.states.get(NODE_STATUS_ENTITY)
assert hass.states.get(NODE_STATUS_ENTITY).state == "alive"
# Mark node as ready
event = Event("ready", {"nodeState": lock_id_lock_as_id150_state})
node.receive_event(event)
assert node.ready
assert hass.states.get(NODE_STATUS_ENTITY)
assert hass.states.get(NODE_STATUS_ENTITY).state == "alive"
async def test_reset_meter( async def test_reset_meter(
hass, hass,