From 80887d757a3fab9a728101774134b731e36e2394 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 22 Jan 2020 17:46:12 -0800 Subject: [PATCH] Simplify automation services (#30996) * Simplify automation services * Empty commit to re-trigger build Co-authored-by: Franck Nijhof --- .../components/automation/__init__.py | 87 ++++--------------- 1 file changed, 18 insertions(+), 69 deletions(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 9f51127cf99..70b8b26fa2c 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -1,5 +1,4 @@ """Allow to set up simple automation rules via the config file.""" -import asyncio from functools import partial import importlib import logging @@ -24,7 +23,6 @@ from homeassistant.core import Context, CoreState, HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import condition, extract_domain_configs, script 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_component import EntityComponent 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 def is_on(hass, entity_id): @@ -136,42 +125,25 @@ async def async_setup(hass, config): 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.""" - tasks = [] - for entity in await component.async_extract_from_service(service_call): - tasks.append( - entity.async_trigger( - service_call.data[ATTR_VARIABLES], - skip_condition=service_call.data[CONF_SKIP_CONDITION], - context=service_call.context, - ) - ) + await entity.async_trigger( + service_call.data[ATTR_VARIABLES], + skip_condition=service_call.data[CONF_SKIP_CONDITION], + context=service_call.context, + ) - if tasks: - await asyncio.wait(tasks) - - async def turn_onoff_service_handler(service_call): - """Handle automation turn on/off service calls.""" - tasks = [] - method = f"async_{service_call.service}" - for entity in await component.async_extract_from_service(service_call): - tasks.append(getattr(entity, method)()) - - if tasks: - 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) + component.async_register_entity_service( + SERVICE_TRIGGER, + { + vol.Optional(ATTR_VARIABLES, default={}): dict, + vol.Optional(CONF_SKIP_CONDITION, default=True): bool, + }, + trigger_service_handler, + ) + component.async_register_entity_service(SERVICE_TOGGLE, {}, "async_toggle") + component.async_register_entity_service(SERVICE_TURN_ON, {}, "async_turn_on") + component.async_register_entity_service(SERVICE_TURN_OFF, {}, "async_turn_off") async def reload_service_handler(service_call): """Remove all automations and load new ones from config.""" @@ -180,33 +152,10 @@ async def async_setup(hass, config): return 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( - hass, - DOMAIN, - SERVICE_RELOAD, - reload_service_handler, - schema=RELOAD_SERVICE_SCHEMA, + hass, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=vol.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