diff --git a/homeassistant/components/zwave_js/device_trigger.py b/homeassistant/components/zwave_js/device_trigger.py index 89379f9a953..1e76f9e73ec 100644 --- a/homeassistant/components/zwave_js/device_trigger.py +++ b/homeassistant/components/zwave_js/device_trigger.py @@ -266,7 +266,11 @@ async def async_get_triggers( entity_id = async_get_node_status_sensor_entity_id( hass, device_id, ent_reg, dev_reg ) - if (entity := ent_reg.async_get(entity_id)) is not None and not entity.disabled: + if ( + entity_id + and (entity := ent_reg.async_get(entity_id)) is not None + and not entity.disabled + ): triggers.append( {**base_trigger, CONF_TYPE: NODE_STATUS, CONF_ENTITY_ID: entity_id} ) diff --git a/homeassistant/components/zwave_js/helpers.py b/homeassistant/components/zwave_js/helpers.py index a5a2da23206..17515f9aa99 100644 --- a/homeassistant/components/zwave_js/helpers.py +++ b/homeassistant/components/zwave_js/helpers.py @@ -329,13 +329,22 @@ def get_zwave_value_from_config(node: ZwaveNode, config: ConfigType) -> ZwaveVal return node.values[value_id] +def _zwave_js_config_entry(hass: HomeAssistant, device: dr.DeviceEntry) -> str | None: + """Find zwave_js config entry from a device.""" + for entry_id in device.config_entries: + entry = hass.config_entries.async_get_entry(entry_id) + if entry and entry.domain == DOMAIN: + return entry_id + return None + + @callback def async_get_node_status_sensor_entity_id( hass: HomeAssistant, device_id: str, ent_reg: er.EntityRegistry | None = None, dev_reg: dr.DeviceRegistry | None = None, -) -> str: +) -> str | None: """Get the node status sensor entity ID for a given Z-Wave JS device.""" if not ent_reg: ent_reg = er.async_get(hass) @@ -344,20 +353,16 @@ def async_get_node_status_sensor_entity_id( if not (device := dev_reg.async_get(device_id)): raise HomeAssistantError("Invalid Device ID provided") - entry_id = next(entry_id for entry_id in device.config_entries) + if not (entry_id := _zwave_js_config_entry(hass, device)): + return None + client = hass.data[DOMAIN][entry_id][DATA_CLIENT] node = async_get_node_from_device_id(hass, device_id, dev_reg) - entity_id = ent_reg.async_get_entity_id( + return ent_reg.async_get_entity_id( SENSOR_DOMAIN, DOMAIN, f"{client.driver.controller.home_id}.{node.node_id}.node_status", ) - if not entity_id: - raise HomeAssistantError( - "Node status sensor entity not found. Device may not be a zwave_js device" - ) - - return entity_id def remove_keys_with_empty_values(config: ConfigType) -> ConfigType: