Add context to scripts and automations (#16415)

* Add context to script helper

* Update script component

* Add context to automations

* Lint
This commit is contained in:
Paulus Schoutsen 2018-09-04 21:16:24 +02:00 committed by Pascal Vizeli
parent e1501c83f8
commit 746f4ac158
17 changed files with 164 additions and 144 deletions

View file

@ -63,11 +63,11 @@ def is_on(hass, entity_id):
@bind_hass
def turn_on(hass, entity_id, variables=None):
def turn_on(hass, entity_id, variables=None, context=None):
"""Turn script on."""
_, object_id = split_entity_id(entity_id)
hass.services.call(DOMAIN, object_id, variables)
hass.services.call(DOMAIN, object_id, variables, context=context)
@bind_hass
@ -97,45 +97,41 @@ def async_reload(hass):
return hass.services.async_call(DOMAIN, SERVICE_RELOAD)
@asyncio.coroutine
def async_setup(hass, config):
async def async_setup(hass, config):
"""Load the scripts from the configuration."""
component = EntityComponent(
_LOGGER, DOMAIN, hass, group_name=GROUP_NAME_ALL_SCRIPTS)
yield from _async_process_config(hass, config, component)
await _async_process_config(hass, config, component)
@asyncio.coroutine
def reload_service(service):
async def reload_service(service):
"""Call a service to reload scripts."""
conf = yield from component.async_prepare_reload()
conf = await component.async_prepare_reload()
if conf is None:
return
yield from _async_process_config(hass, conf, component)
await _async_process_config(hass, conf, component)
@asyncio.coroutine
def turn_on_service(service):
async def turn_on_service(service):
"""Call a service to turn script on."""
# We could turn on script directly here, but we only want to offer
# one way to do it. Otherwise no easy way to detect invocations.
var = service.data.get(ATTR_VARIABLES)
for script in component.async_extract_from_service(service):
yield from hass.services.async_call(DOMAIN, script.object_id, var)
await hass.services.async_call(DOMAIN, script.object_id, var,
context=service.context)
@asyncio.coroutine
def turn_off_service(service):
async def turn_off_service(service):
"""Cancel a script."""
# Stopping a script is ok to be done in parallel
yield from asyncio.wait(
await asyncio.wait(
[script.async_turn_off() for script
in component.async_extract_from_service(service)], loop=hass.loop)
@asyncio.coroutine
def toggle_service(service):
async def toggle_service(service):
"""Toggle a script."""
for script in component.async_extract_from_service(service):
yield from script.async_toggle()
await script.async_toggle(context=service.context)
hass.services.async_register(DOMAIN, SERVICE_RELOAD, reload_service,
schema=RELOAD_SERVICE_SCHEMA)
@ -149,18 +145,17 @@ def async_setup(hass, config):
return True
@asyncio.coroutine
def _async_process_config(hass, config, component):
"""Process group configuration."""
@asyncio.coroutine
def service_handler(service):
async def _async_process_config(hass, config, component):
"""Process script configuration."""
async def service_handler(service):
"""Execute a service call to script.<script name>."""
entity_id = ENTITY_ID_FORMAT.format(service.service)
script = component.get_entity(entity_id)
if script.is_on:
_LOGGER.warning("Script %s already running.", entity_id)
return
yield from script.async_turn_on(variables=service.data)
await script.async_turn_on(variables=service.data,
context=service.context)
scripts = []
@ -171,7 +166,7 @@ def _async_process_config(hass, config, component):
hass.services.async_register(
DOMAIN, object_id, service_handler, schema=SCRIPT_SERVICE_SCHEMA)
yield from component.async_add_entities(scripts)
await component.async_add_entities(scripts)
class ScriptEntity(ToggleEntity):
@ -209,18 +204,16 @@ class ScriptEntity(ToggleEntity):
"""Return true if script is on."""
return self.script.is_running
@asyncio.coroutine
def async_turn_on(self, **kwargs):
async def async_turn_on(self, **kwargs):
"""Turn the script on."""
yield from self.script.async_run(kwargs.get(ATTR_VARIABLES))
await self.script.async_run(
kwargs.get(ATTR_VARIABLES), kwargs.get('context'))
@asyncio.coroutine
def async_turn_off(self, **kwargs):
async def async_turn_off(self, **kwargs):
"""Turn script off."""
self.script.async_stop()
@asyncio.coroutine
def async_will_remove_from_hass(self):
async def async_will_remove_from_hass(self):
"""Stop script and remove service when it will be removed from HASS."""
if self.script.is_running:
self.script.async_stop()