Find related items scripts/automations (#31293)
* Find related items scripts/automations * Update manifest
This commit is contained in:
parent
881437c085
commit
424e15c7a7
11 changed files with 586 additions and 49 deletions
|
@ -1,6 +1,7 @@
|
|||
"""Support for scripts."""
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import List
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
|
@ -15,6 +16,7 @@ from homeassistant.const import (
|
|||
SERVICE_TURN_ON,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.config_validation import make_entity_service_schema
|
||||
from homeassistant.helpers.entity import ToggleEntity
|
||||
|
@ -69,9 +71,75 @@ 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."""
|
||||
if DOMAIN not in hass.data:
|
||||
return []
|
||||
|
||||
component = hass.data[DOMAIN]
|
||||
|
||||
results = []
|
||||
|
||||
for script_entity in component.entities:
|
||||
if entity_id in script_entity.script.referenced_entities:
|
||||
results.append(script_entity.entity_id)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
@callback
|
||||
def entities_in_script(hass: HomeAssistant, entity_id: str) -> List[str]:
|
||||
"""Return all entities in a scene."""
|
||||
if DOMAIN not in hass.data:
|
||||
return []
|
||||
|
||||
component = hass.data[DOMAIN]
|
||||
|
||||
script_entity = component.get_entity(entity_id)
|
||||
|
||||
if script_entity is None:
|
||||
return []
|
||||
|
||||
return list(script_entity.script.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]
|
||||
|
||||
results = []
|
||||
|
||||
for script_entity in component.entities:
|
||||
if device_id in script_entity.script.referenced_devices:
|
||||
results.append(script_entity.entity_id)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
@callback
|
||||
def devices_in_script(hass: HomeAssistant, entity_id: str) -> List[str]:
|
||||
"""Return all devices in a scene."""
|
||||
if DOMAIN not in hass.data:
|
||||
return []
|
||||
|
||||
component = hass.data[DOMAIN]
|
||||
|
||||
script_entity = component.get_entity(entity_id)
|
||||
|
||||
if script_entity is None:
|
||||
return []
|
||||
|
||||
return list(script_entity.script.referenced_devices)
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Load the scripts from the configuration."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
hass.data[DOMAIN] = component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
|
||||
await _async_process_config(hass, config, component)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue