diff --git a/homeassistant/components/zwave_js/sensor.py b/homeassistant/components/zwave_js/sensor.py index 9fed7158d4a..0b9defc5f62 100644 --- a/homeassistant/components/zwave_js/sensor.py +++ b/homeassistant/components/zwave_js/sensor.py @@ -343,6 +343,7 @@ class ZWaveJSStatisticsSensorEntityDescription(SensorEntityDescription): convert: Callable[ [ControllerStatisticsDataType | NodeStatisticsDataType, str], Any ] = lambda statistics, key: statistics.get(key) + entity_registry_enabled_default: bool = False # Controller statistics descriptions @@ -487,6 +488,7 @@ ENTITY_DESCRIPTION_NODE_STATISTICS_LIST = [ else None ) ), + entity_registry_enabled_default=True, ), ] @@ -930,7 +932,6 @@ class ZWaveStatisticsSensor(SensorEntity): entity_description: ZWaveJSStatisticsSensorEntityDescription _attr_should_poll = False _attr_entity_category = EntityCategory.DIAGNOSTIC - _attr_entity_registry_enabled_default = False _attr_has_entity_name = True def __init__( diff --git a/tests/components/zwave_js/fixtures/zp3111-5_state.json b/tests/components/zwave_js/fixtures/zp3111-5_state.json index 68bb0f03af8..55f27b7fa5a 100644 --- a/tests/components/zwave_js/fixtures/zp3111-5_state.json +++ b/tests/components/zwave_js/fixtures/zp3111-5_state.json @@ -694,7 +694,8 @@ "commandsRX": 0, "commandsDroppedRX": 0, "commandsDroppedTX": 0, - "timeoutResponse": 0 + "timeoutResponse": 0, + "lastSeen": "2024-01-01T12:00:00+00" }, "highestSecurityClass": -1, "isControllerNode": false diff --git a/tests/components/zwave_js/test_discovery.py b/tests/components/zwave_js/test_discovery.py index 569e36d3b5c..67f4a8d962f 100644 --- a/tests/components/zwave_js/test_discovery.py +++ b/tests/components/zwave_js/test_discovery.py @@ -22,9 +22,10 @@ from homeassistant.components.zwave_js.discovery import ( from homeassistant.components.zwave_js.discovery_data_template import ( DynamicCurrentTempClimateDataTemplate, ) +from homeassistant.components.zwave_js.helpers import get_device_id from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_UNKNOWN, EntityCategory from homeassistant.core import HomeAssistant -from homeassistant.helpers import entity_registry as er +from homeassistant.helpers import device_registry as dr, entity_registry as er async def test_iblinds_v2(hass: HomeAssistant, client, iblinds_v2, integration) -> None: @@ -224,14 +225,21 @@ async def test_indicator_test( This test covers indicators that we don't already have device fixtures for. """ + device = dr.async_get(hass).async_get_device( + identifiers={get_device_id(client.driver, indicator_test)} + ) + assert device ent_reg = er.async_get(hass) - assert len(hass.states.async_entity_ids(NUMBER_DOMAIN)) == 0 - assert len(hass.states.async_entity_ids(BUTTON_DOMAIN)) == 1 # only ping - assert len(hass.states.async_entity_ids(BINARY_SENSOR_DOMAIN)) == 1 - assert ( - len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 3 - ) # include node + controller status - assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1 + entities = er.async_entries_for_device(ent_reg, device.id) + + def len_domain(domain): + return len([entity for entity in entities if entity.domain == domain]) + + assert len_domain(NUMBER_DOMAIN) == 0 + assert len_domain(BUTTON_DOMAIN) == 1 # only ping + assert len_domain(BINARY_SENSOR_DOMAIN) == 1 + assert len_domain(SENSOR_DOMAIN) == 3 # include node status + last seen + assert len_domain(SWITCH_DOMAIN) == 1 entity_id = "binary_sensor.this_is_a_fake_device_binary_sensor" entry = ent_reg.async_get(entity_id) diff --git a/tests/components/zwave_js/test_init.py b/tests/components/zwave_js/test_init.py index 7f3a9428dad..4555ee59e1e 100644 --- a/tests/components/zwave_js/test_init.py +++ b/tests/components/zwave_js/test_init.py @@ -227,14 +227,16 @@ async def test_on_node_added_not_ready( client.driver.receive_event(event) await hass.async_block_till_done() - # the only entities are the node status sensor and ping button - assert len(hass.states.async_all()) == 3 - device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)}) assert device # no extended device identifier yet assert len(device.identifiers) == 1 + ent_reg = er.async_get(hass) + entities = er.async_entries_for_device(ent_reg, device.id) + # the only entities are the node status sensor, last_seen sensor, and ping button + assert len(entities) == 3 + async def test_existing_node_ready( hass: HomeAssistant, client, multisensor_6, integration @@ -329,14 +331,16 @@ async def test_existing_node_not_ready( assert not device.model assert not device.sw_version - # the only entities are the node status sensor and ping button - assert len(hass.states.async_all()) == 3 - device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)}) assert device # no extended device identifier yet assert len(device.identifiers) == 1 + ent_reg = er.async_get(hass) + entities = er.async_entries_for_device(ent_reg, device.id) + # the only entities are the node status sensor, last_seen sensor, and ping button + assert len(entities) == 3 + async def test_existing_node_not_replaced_when_not_ready( hass: HomeAssistant, diff --git a/tests/components/zwave_js/test_sensor.py b/tests/components/zwave_js/test_sensor.py index 4e88b2b50cc..a3d36b84382 100644 --- a/tests/components/zwave_js/test_sensor.py +++ b/tests/components/zwave_js/test_sensor.py @@ -731,14 +731,13 @@ NODE_STATISTICS_SUFFIXES = { NODE_STATISTICS_SUFFIXES_UNKNOWN = { "round_trip_time": 6, "rssi": 7, - "last_seen": "2024-01-01T00:00:00+00:00", } -async def test_statistics_sensors( +async def test_statistics_sensors_no_last_seen( hass: HomeAssistant, zp3111, client, integration, caplog: pytest.LogCaptureFixture ) -> None: - """Test statistics sensors.""" + """Test all statistics sensors but last seen which is enabled by default.""" ent_reg = er.async_get(hass) for prefix, suffixes in ( @@ -880,6 +879,22 @@ async def test_statistics_sensors( ) +async def test_last_seen_statistics_sensors( + hass: HomeAssistant, zp3111, client, integration +) -> None: + """Test last_seen statistics sensors.""" + ent_reg = er.async_get(hass) + + entity_id = f"{NODE_STATISTICS_ENTITY_PREFIX}last_seen" + entry = ent_reg.async_get(entity_id) + assert entry + assert not entry.disabled + + state = hass.states.get(entity_id) + assert state + assert state.state == "2024-01-01T12:00:00+00:00" + + ENERGY_PRODUCTION_ENTITY_MAP = { "energy_production_power": { "state": 1.23,