Simplify automation services (#30996)

* Simplify automation services

* Empty commit to re-trigger build

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Paulus Schoutsen 2020-01-22 17:46:12 -08:00 committed by GitHub
parent 192b656635
commit 80887d757a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,4 @@
"""Allow to set up simple automation rules via the config file.""" """Allow to set up simple automation rules via the config file."""
import asyncio
from functools import partial from functools import partial
import importlib import importlib
import logging import logging
@ -24,7 +23,6 @@ from homeassistant.core import Context, CoreState, HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import condition, extract_domain_configs, script from homeassistant.helpers import condition, extract_domain_configs, script
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import make_entity_service_schema
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
@ -110,15 +108,6 @@ PLATFORM_SCHEMA = vol.All(
), ),
) )
TRIGGER_SERVICE_SCHEMA = make_entity_service_schema(
{
vol.Optional(ATTR_VARIABLES, default={}): dict,
vol.Optional(CONF_SKIP_CONDITION, default=True): bool,
}
)
RELOAD_SERVICE_SCHEMA = vol.Schema({})
@bind_hass @bind_hass
def is_on(hass, entity_id): def is_on(hass, entity_id):
@ -136,42 +125,25 @@ async def async_setup(hass, config):
await _async_process_config(hass, config, component) await _async_process_config(hass, config, component)
async def trigger_service_handler(service_call): async def trigger_service_handler(entity, service_call):
"""Handle automation triggers.""" """Handle automation triggers."""
tasks = [] await entity.async_trigger(
for entity in await component.async_extract_from_service(service_call): service_call.data[ATTR_VARIABLES],
tasks.append( skip_condition=service_call.data[CONF_SKIP_CONDITION],
entity.async_trigger( context=service_call.context,
service_call.data[ATTR_VARIABLES], )
skip_condition=service_call.data[CONF_SKIP_CONDITION],
context=service_call.context,
)
)
if tasks: component.async_register_entity_service(
await asyncio.wait(tasks) SERVICE_TRIGGER,
{
async def turn_onoff_service_handler(service_call): vol.Optional(ATTR_VARIABLES, default={}): dict,
"""Handle automation turn on/off service calls.""" vol.Optional(CONF_SKIP_CONDITION, default=True): bool,
tasks = [] },
method = f"async_{service_call.service}" trigger_service_handler,
for entity in await component.async_extract_from_service(service_call): )
tasks.append(getattr(entity, method)()) component.async_register_entity_service(SERVICE_TOGGLE, {}, "async_toggle")
component.async_register_entity_service(SERVICE_TURN_ON, {}, "async_turn_on")
if tasks: component.async_register_entity_service(SERVICE_TURN_OFF, {}, "async_turn_off")
await asyncio.wait(tasks)
async def toggle_service_handler(service_call):
"""Handle automation toggle service calls."""
tasks = []
for entity in await component.async_extract_from_service(service_call):
if entity.is_on:
tasks.append(entity.async_turn_off())
else:
tasks.append(entity.async_turn_on())
if tasks:
await asyncio.wait(tasks)
async def reload_service_handler(service_call): async def reload_service_handler(service_call):
"""Remove all automations and load new ones from config.""" """Remove all automations and load new ones from config."""
@ -180,33 +152,10 @@ async def async_setup(hass, config):
return return
await _async_process_config(hass, conf, component) await _async_process_config(hass, conf, component)
hass.services.async_register(
DOMAIN, SERVICE_TRIGGER, trigger_service_handler, schema=TRIGGER_SERVICE_SCHEMA
)
async_register_admin_service( async_register_admin_service(
hass, hass, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=vol.Schema({}),
DOMAIN,
SERVICE_RELOAD,
reload_service_handler,
schema=RELOAD_SERVICE_SCHEMA,
) )
hass.services.async_register(
DOMAIN,
SERVICE_TOGGLE,
toggle_service_handler,
schema=make_entity_service_schema({}),
)
for service in (SERVICE_TURN_ON, SERVICE_TURN_OFF):
hass.services.async_register(
DOMAIN,
service,
turn_onoff_service_handler,
schema=make_entity_service_schema({}),
)
return True return True