diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index b1ec0c68e4b..b4fc82f52b9 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -148,9 +148,10 @@ def is_on(hass: HomeAssistant, entity_id: str) -> bool: return hass.states.is_state(entity_id, STATE_ON) -@callback -def automations_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]: - """Return all automations that reference the entity.""" +def _automations_with_x( + hass: HomeAssistant, referenced_id: str, property_name: str +) -> list[str]: + """Return all automations that reference the x.""" if DOMAIN not in hass.data: return [] @@ -159,13 +160,14 @@ def automations_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]: return [ automation_entity.entity_id for automation_entity in component.entities - if entity_id in automation_entity.referenced_entities + if referenced_id in getattr(automation_entity, property_name) ] -@callback -def entities_in_automation(hass: HomeAssistant, entity_id: str) -> list[str]: - """Return all entities in a scene.""" +def _x_in_automation( + hass: HomeAssistant, entity_id: str, property_name: str +) -> list[str]: + """Return all x in an automation.""" if DOMAIN not in hass.data: return [] @@ -174,65 +176,43 @@ def entities_in_automation(hass: HomeAssistant, entity_id: str) -> list[str]: if (automation_entity := component.get_entity(entity_id)) is None: return [] - return list(automation_entity.referenced_entities) + return list(getattr(automation_entity, property_name)) + + +@callback +def automations_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]: + """Return all automations that reference the entity.""" + return _automations_with_x(hass, entity_id, "referenced_entities") + + +@callback +def entities_in_automation(hass: HomeAssistant, entity_id: str) -> list[str]: + """Return all entities in an automation.""" + return _x_in_automation(hass, entity_id, "referenced_entities") @callback def automations_with_device(hass: HomeAssistant, device_id: str) -> list[str]: """Return all automations that reference the device.""" - if DOMAIN not in hass.data: - return [] - - component: EntityComponent[AutomationEntity] = hass.data[DOMAIN] - - return [ - automation_entity.entity_id - for automation_entity in component.entities - if device_id in automation_entity.referenced_devices - ] + return _automations_with_x(hass, device_id, "referenced_devices") @callback def devices_in_automation(hass: HomeAssistant, entity_id: str) -> list[str]: - """Return all devices in a scene.""" - if DOMAIN not in hass.data: - return [] - - component: EntityComponent[AutomationEntity] = hass.data[DOMAIN] - - if (automation_entity := component.get_entity(entity_id)) is None: - return [] - - return list(automation_entity.referenced_devices) + """Return all devices in an automation.""" + return _x_in_automation(hass, entity_id, "referenced_devices") @callback def automations_with_area(hass: HomeAssistant, area_id: str) -> list[str]: """Return all automations that reference the area.""" - if DOMAIN not in hass.data: - return [] - - component: EntityComponent[AutomationEntity] = hass.data[DOMAIN] - - return [ - automation_entity.entity_id - for automation_entity in component.entities - if area_id in automation_entity.referenced_areas - ] + return _automations_with_x(hass, area_id, "referenced_areas") @callback def areas_in_automation(hass: HomeAssistant, entity_id: str) -> list[str]: """Return all areas in an automation.""" - if DOMAIN not in hass.data: - return [] - - component: EntityComponent[AutomationEntity] = hass.data[DOMAIN] - - if (automation_entity := component.get_entity(entity_id)) is None: - return [] - - return list(automation_entity.referenced_areas) + return _x_in_automation(hass, entity_id, "referenced_areas") @callback diff --git a/homeassistant/components/script/__init__.py b/homeassistant/components/script/__init__.py index a5ea2a17e0b..9791f0e588e 100644 --- a/homeassistant/components/script/__init__.py +++ b/homeassistant/components/script/__init__.py @@ -79,9 +79,10 @@ def is_on(hass, entity_id): return hass.states.is_state(entity_id, STATE_ON) -@callback -def scripts_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]: - """Return all scripts that reference the entity.""" +def _scripts_with_x( + hass: HomeAssistant, referenced_id: str, property_name: str +) -> list[str]: + """Return all scripts that reference the x.""" if DOMAIN not in hass.data: return [] @@ -90,80 +91,57 @@ def scripts_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]: return [ script_entity.entity_id for script_entity in component.entities - if entity_id in script_entity.script.referenced_entities + if referenced_id in getattr(script_entity.script, property_name) ] +def _x_in_script(hass: HomeAssistant, entity_id: str, property_name: str) -> list[str]: + """Return all x in a script.""" + if DOMAIN not in hass.data: + return [] + + component = hass.data[DOMAIN] + + if (script_entity := component.get_entity(entity_id)) is None: + return [] + + return list(getattr(script_entity.script, property_name)) + + +@callback +def scripts_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]: + """Return all scripts that reference the entity.""" + return _scripts_with_x(hass, entity_id, "referenced_entities") + + @callback def entities_in_script(hass: HomeAssistant, entity_id: str) -> list[str]: """Return all entities in script.""" - if DOMAIN not in hass.data: - return [] - - component = hass.data[DOMAIN] - - if (script_entity := component.get_entity(entity_id)) is None: - return [] - - return list(script_entity.script.referenced_entities) + return _x_in_script(hass, entity_id, "referenced_entities") @callback def scripts_with_device(hass: HomeAssistant, device_id: str) -> list[str]: """Return all scripts that reference the device.""" - if DOMAIN not in hass.data: - return [] - - component = hass.data[DOMAIN] - - return [ - script_entity.entity_id - for script_entity in component.entities - if device_id in script_entity.script.referenced_devices - ] + return _scripts_with_x(hass, device_id, "referenced_devices") @callback def devices_in_script(hass: HomeAssistant, entity_id: str) -> list[str]: """Return all devices in script.""" - if DOMAIN not in hass.data: - return [] - - component = hass.data[DOMAIN] - - if (script_entity := component.get_entity(entity_id)) is None: - return [] - - return list(script_entity.script.referenced_devices) + return _x_in_script(hass, entity_id, "referenced_devices") @callback def scripts_with_area(hass: HomeAssistant, area_id: str) -> list[str]: """Return all scripts that reference the area.""" - if DOMAIN not in hass.data: - return [] - - component = hass.data[DOMAIN] - - return [ - script_entity.entity_id - for script_entity in component.entities - if area_id in script_entity.script.referenced_areas - ] + return _scripts_with_x(hass, area_id, "referenced_areas") @callback def areas_in_script(hass: HomeAssistant, entity_id: str) -> list[str]: """Return all areas in a script.""" - if DOMAIN not in hass.data: - return [] - - component = hass.data[DOMAIN] - - if (script_entity := component.get_entity(entity_id)) is None: - return [] - - return list(script_entity.script.referenced_areas) + return _x_in_script(hass, entity_id, "referenced_areas") @callback