mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Deduplicate some code in scripts and automations (#78443)
This commit is contained in:
parent
0a1fd36e03
commit
30702bdcd2
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user