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: """Get the node status sensor entity ID for a given Z-Wave JS device.""" if not ent_reg: ent_reg = er.async_get(hass) if not dev_reg: dev_reg = dr.async_get(hass) if not (device := dev_reg.async_get(device_id)): raise HomeAssistantError("Invalid Device ID provided")
def async_get_node_from_device_id( hass: HomeAssistant, device_id: str, dev_reg: dr.DeviceRegistry | None = None ) -> ZwaveNode: """ Get node from a device ID. Raises ValueError if device is invalid or node can't be found. """ if not dev_reg: dev_reg = dr.async_get(hass) if not (device_entry := dev_reg.async_get(device_id)): raise ValueError(f"Device ID {device_id} is not valid")
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: """Get the node status sensor entity ID for a given Z-Wave JS device.""" if not ent_reg: ent_reg = er.async_get(hass) if not dev_reg: dev_reg = dr.async_get(hass) device = dev_reg.async_get(device_id) if not device: raise HomeAssistantError("Invalid Device ID provided") entry_id = next(entry_id for entry_id in device.config_entries) 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( 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 async_get_node_from_device_id( hass: HomeAssistant, device_id: str, dev_reg: dr.DeviceRegistry | None = None) -> ZwaveNode: """ Get node from a device ID. Raises ValueError if device is invalid or node can't be found. """ if not dev_reg: dev_reg = dr.async_get(hass) device_entry = dev_reg.async_get(device_id) if not device_entry: raise ValueError(f"Device ID {device_id} is not valid") # Use device config entry ID's to validate that this is a valid zwave_js device # and to get the client config_entry_ids = device_entry.config_entries config_entry_id = next( (config_entry_id for config_entry_id in config_entry_ids if cast( ConfigEntry, hass.config_entries.async_get_entry(config_entry_id), ).domain == DOMAIN), None, ) if config_entry_id is None or config_entry_id not in hass.data[DOMAIN]: raise ValueError( f"Device {device_id} is not from an existing zwave_js config entry" ) client = hass.data[DOMAIN][config_entry_id][DATA_CLIENT] # Get node ID from device identifier, perform some validation, and then get the # node identifier = next( (get_home_and_node_id_from_device_id(identifier) for identifier in device_entry.identifiers if identifier[0] == DOMAIN), None, ) node_id = int(identifier[1]) if identifier is not None else None if node_id is None or node_id not in client.driver.controller.nodes: raise ValueError(f"Node for device {device_id} can't be found") return client.driver.controller.nodes[node_id]