Deduplicate some code in scripts and automations (#78443)

This commit is contained in:
Erik Montnemery 2022-09-15 07:41:11 +02:00 committed by GitHub
parent 0a1fd36e03
commit 30702bdcd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 98 deletions

View File

@ -148,9 +148,10 @@ def is_on(hass: HomeAssistant, entity_id: str) -> bool:
return hass.states.is_state(entity_id, STATE_ON) return hass.states.is_state(entity_id, STATE_ON)
@callback def _automations_with_x(
def automations_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]: hass: HomeAssistant, referenced_id: str, property_name: str
"""Return all automations that reference the entity.""" ) -> list[str]:
"""Return all automations that reference the x."""
if DOMAIN not in hass.data: if DOMAIN not in hass.data:
return [] return []
@ -159,13 +160,14 @@ def automations_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]:
return [ return [
automation_entity.entity_id automation_entity.entity_id
for automation_entity in component.entities 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 _x_in_automation(
def entities_in_automation(hass: HomeAssistant, entity_id: str) -> list[str]: hass: HomeAssistant, entity_id: str, property_name: str
"""Return all entities in a scene.""" ) -> list[str]:
"""Return all x in an automation."""
if DOMAIN not in hass.data: if DOMAIN not in hass.data:
return [] 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: if (automation_entity := component.get_entity(entity_id)) is None:
return [] 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 @callback
def automations_with_device(hass: HomeAssistant, device_id: str) -> list[str]: def automations_with_device(hass: HomeAssistant, device_id: str) -> list[str]:
"""Return all automations that reference the device.""" """Return all automations that reference the device."""
if DOMAIN not in hass.data: return _automations_with_x(hass, device_id, "referenced_devices")
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
]
@callback @callback
def devices_in_automation(hass: HomeAssistant, entity_id: str) -> list[str]: def devices_in_automation(hass: HomeAssistant, entity_id: str) -> list[str]:
"""Return all devices in a scene.""" """Return all devices in an automation."""
if DOMAIN not in hass.data: return _x_in_automation(hass, entity_id, "referenced_devices")
return []
component: EntityComponent[AutomationEntity] = hass.data[DOMAIN]
if (automation_entity := component.get_entity(entity_id)) is None:
return []
return list(automation_entity.referenced_devices)
@callback @callback
def automations_with_area(hass: HomeAssistant, area_id: str) -> list[str]: def automations_with_area(hass: HomeAssistant, area_id: str) -> list[str]:
"""Return all automations that reference the area.""" """Return all automations that reference the area."""
if DOMAIN not in hass.data: return _automations_with_x(hass, area_id, "referenced_areas")
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
]
@callback @callback
def areas_in_automation(hass: HomeAssistant, entity_id: str) -> list[str]: def areas_in_automation(hass: HomeAssistant, entity_id: str) -> list[str]:
"""Return all areas in an automation.""" """Return all areas in an automation."""
if DOMAIN not in hass.data: return _x_in_automation(hass, entity_id, "referenced_areas")
return []
component: EntityComponent[AutomationEntity] = hass.data[DOMAIN]
if (automation_entity := component.get_entity(entity_id)) is None:
return []
return list(automation_entity.referenced_areas)
@callback @callback

View File

@ -79,9 +79,10 @@ def is_on(hass, entity_id):
return hass.states.is_state(entity_id, STATE_ON) return hass.states.is_state(entity_id, STATE_ON)
@callback def _scripts_with_x(
def scripts_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]: hass: HomeAssistant, referenced_id: str, property_name: str
"""Return all scripts that reference the entity.""" ) -> list[str]:
"""Return all scripts that reference the x."""
if DOMAIN not in hass.data: if DOMAIN not in hass.data:
return [] return []
@ -90,80 +91,57 @@ def scripts_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]:
return [ return [
script_entity.entity_id script_entity.entity_id
for script_entity in component.entities 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 @callback
def entities_in_script(hass: HomeAssistant, entity_id: str) -> list[str]: def entities_in_script(hass: HomeAssistant, entity_id: str) -> list[str]:
"""Return all entities in script.""" """Return all entities in script."""
if DOMAIN not in hass.data: return _x_in_script(hass, entity_id, "referenced_entities")
return []
component = hass.data[DOMAIN]
if (script_entity := component.get_entity(entity_id)) is None:
return []
return list(script_entity.script.referenced_entities)
@callback @callback
def scripts_with_device(hass: HomeAssistant, device_id: str) -> list[str]: def scripts_with_device(hass: HomeAssistant, device_id: str) -> list[str]:
"""Return all scripts that reference the device.""" """Return all scripts that reference the device."""
if DOMAIN not in hass.data: return _scripts_with_x(hass, device_id, "referenced_devices")
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
]
@callback @callback
def devices_in_script(hass: HomeAssistant, entity_id: str) -> list[str]: def devices_in_script(hass: HomeAssistant, entity_id: str) -> list[str]:
"""Return all devices in script.""" """Return all devices in script."""
if DOMAIN not in hass.data: return _x_in_script(hass, entity_id, "referenced_devices")
return []
component = hass.data[DOMAIN]
if (script_entity := component.get_entity(entity_id)) is None:
return []
return list(script_entity.script.referenced_devices)
@callback @callback
def scripts_with_area(hass: HomeAssistant, area_id: str) -> list[str]: def scripts_with_area(hass: HomeAssistant, area_id: str) -> list[str]:
"""Return all scripts that reference the area.""" """Return all scripts that reference the area."""
if DOMAIN not in hass.data: return _scripts_with_x(hass, area_id, "referenced_areas")
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
]
@callback @callback
def areas_in_script(hass: HomeAssistant, entity_id: str) -> list[str]: def areas_in_script(hass: HomeAssistant, entity_id: str) -> list[str]:
"""Return all areas in a script.""" """Return all areas in a script."""
if DOMAIN not in hass.data: return _x_in_script(hass, entity_id, "referenced_areas")
return []
component = hass.data[DOMAIN]
if (script_entity := component.get_entity(entity_id)) is None:
return []
return list(script_entity.script.referenced_areas)
@callback @callback