From c937a7bcb05e7690a97cfd5a0bd69f877d72ff54 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Wed, 8 Mar 2017 07:51:34 +0100 Subject: [PATCH] Add support for remove services / Reload script support (#6441) * Add support for remove services / Reload script support * Reload support for scripts * Add more unittest for services * Add unittest for script reload * Address paulus comments --- .../components/automation/__init__.py | 3 +- homeassistant/components/group.py | 8 +- homeassistant/components/script.py | 76 +++++++++++++----- homeassistant/const.py | 2 + homeassistant/core.py | 29 ++++++- tests/components/test_script.py | 36 +++++++++ tests/test_core.py | 78 +++++++++++++++++-- 7 files changed, 197 insertions(+), 35 deletions(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 7233ffc5c66..96d5b0499d2 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -15,7 +15,7 @@ from homeassistant.setup import async_prepare_setup_platform from homeassistant import config as conf_util from homeassistant.const import ( ATTR_ENTITY_ID, CONF_PLATFORM, STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, - SERVICE_TOGGLE) + SERVICE_TOGGLE, SERVICE_RELOAD) from homeassistant.components import logbook from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import extract_domain_configs, script, condition @@ -51,7 +51,6 @@ DEFAULT_INITIAL_STATE = True ATTR_LAST_TRIGGERED = 'last_triggered' ATTR_VARIABLES = 'variables' SERVICE_TRIGGER = 'trigger' -SERVICE_RELOAD = 'reload' _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/group.py b/homeassistant/components/group.py index 06e029ffd8c..f582ff33a07 100644 --- a/homeassistant/components/group.py +++ b/homeassistant/components/group.py @@ -14,7 +14,7 @@ from homeassistant import config as conf_util, core as ha from homeassistant.const import ( ATTR_ENTITY_ID, CONF_ICON, CONF_NAME, STATE_CLOSED, STATE_HOME, STATE_NOT_HOME, STATE_OFF, STATE_ON, STATE_OPEN, STATE_LOCKED, - STATE_UNLOCKED, STATE_UNKNOWN, ATTR_ASSUMED_STATE) + STATE_UNLOCKED, STATE_UNKNOWN, ATTR_ASSUMED_STATE, SERVICE_RELOAD) from homeassistant.core import callback from homeassistant.helpers.entity import Entity, async_generate_entity_id from homeassistant.helpers.entity_component import EntityComponent @@ -42,7 +42,6 @@ SET_VISIBILITY_SERVICE_SCHEMA = vol.Schema({ vol.Required(ATTR_VISIBLE): cv.boolean }) -SERVICE_RELOAD = 'reload' RELOAD_SERVICE_SCHEMA = vol.Schema({}) _LOGGER = logging.getLogger(__name__) @@ -395,17 +394,16 @@ class Group(Entity): self._state = STATE_UNKNOWN self._async_update_group_state() - @asyncio.coroutine def async_remove(self): """Remove group from HASS. - This method must be run in the event loop. + This method must be run in the event loop and returns a coroutine. """ if self._async_unsub_state_changed: self._async_unsub_state_changed() self._async_unsub_state_changed = None - yield from super().async_remove() + return super().async_remove() @asyncio.coroutine def _async_state_changed_listener(self, entity_id, old_state, new_state): diff --git a/homeassistant/components/script.py b/homeassistant/components/script.py index cf4843353b5..bcab6465dc1 100644 --- a/homeassistant/components/script.py +++ b/homeassistant/components/script.py @@ -14,7 +14,7 @@ import voluptuous as vol from homeassistant.const import ( ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON, - SERVICE_TOGGLE, STATE_ON, CONF_ALIAS) + SERVICE_TOGGLE, SERVICE_RELOAD, STATE_ON, CONF_ALIAS) from homeassistant.core import split_entity_id from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity_component import EntityComponent @@ -49,6 +49,7 @@ SCRIPT_TURN_ONOFF_SCHEMA = vol.Schema({ vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, vol.Optional(ATTR_VARIABLES): dict, }) +RELOAD_SERVICE_SCHEMA = vol.Schema({}) def is_on(hass, entity_id): @@ -56,6 +57,11 @@ def is_on(hass, entity_id): return hass.states.is_state(entity_id, STATE_ON) +def reload(hass): + """Reload script component.""" + hass.services.call(DOMAIN, SERVICE_RELOAD) + + def turn_on(hass, entity_id, variables=None): """Turn script on.""" _, object_id = split_entity_id(entity_id) @@ -76,29 +82,19 @@ def toggle(hass, entity_id): @asyncio.coroutine def async_setup(hass, config): """Load the scripts from the configuration.""" - component = EntityComponent(_LOGGER, DOMAIN, hass, - group_name=GROUP_NAME_ALL_SCRIPTS) + component = EntityComponent( + _LOGGER, DOMAIN, hass, group_name=GROUP_NAME_ALL_SCRIPTS) + + yield from _async_process_config(hass, config, component) @asyncio.coroutine - def service_handler(service): - """Execute a service call to script.