From 14bd630c1d87f3fb444d8298eb8c3373d589eb9d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 23 Apr 2016 07:11:21 +0200 Subject: [PATCH] Service/Script cleanup --- homeassistant/helpers/config_validation.py | 6 ++--- homeassistant/helpers/script.py | 9 ++++---- homeassistant/helpers/service.py | 27 +++++++--------------- tests/helpers/test_service.py | 18 --------------- 4 files changed, 15 insertions(+), 45 deletions(-) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 71e103f7dd3..3a8e6179c86 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -264,14 +264,12 @@ SERVICE_SCHEMA = vol.All(vol.Schema({ vol.Optional('entity_id'): entity_ids, }), has_at_least_one_key('service', 'service_template')) -# ----- SCRIPT - -_DELAY_SCHEMA = vol.Schema({ +_SCRIPT_DELAY_SCHEMA = vol.Schema({ vol.Optional(CONF_ALIAS): string, vol.Required("delay"): vol.All(time_period, positive_timedelta) }) SCRIPT_SCHEMA = vol.All( ensure_list, - [vol.Any(SERVICE_SCHEMA, _DELAY_SCHEMA, EVENT_SCHEMA)], + [vol.Any(SERVICE_SCHEMA, _SCRIPT_DELAY_SCHEMA, EVENT_SCHEMA)], ) diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index 6c938fd4032..05e49c6e9ce 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -76,12 +76,12 @@ class Script(): self._change_listener() return - elif service.validate_service_call(action) is None: - self._call_service(action, variables) - elif CONF_EVENT in action: self._fire_event(action) + else: + self._call_service(action, variables) + self._cur = -1 self.last_action = None if self._change_listener: @@ -102,7 +102,8 @@ class Script(): """Call the service specified in the action.""" self.last_action = action.get(CONF_ALIAS, 'call service') self._log("Executing step %s", self.last_action) - service.call_from_config(self.hass, action, True, variables) + service.call_from_config(self.hass, action, True, variables, + validate_config=False) def _fire_event(self, action): """Fire an event.""" diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 8b89d856c50..95dce9516de 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -32,13 +32,15 @@ def service(domain, service_name): return register_service_decorator -def call_from_config(hass, config, blocking=False, variables=None): +def call_from_config(hass, config, blocking=False, variables=None, + validate_config=True): """Call a service based on a config hash.""" - try: - config = cv.SERVICE_SCHEMA(config) - except vol.Invalid as ex: - _LOGGER.error("Invalid config for calling service: %s", ex) - return + if validate_config: + try: + config = cv.SERVICE_SCHEMA(config) + except vol.Invalid as ex: + _LOGGER.error("Invalid config for calling service: %s", ex) + return if CONF_SERVICE in config: domain_service = config[CONF_SERVICE] @@ -85,16 +87,3 @@ def extract_entity_ids(hass, service_call): return group.expand_entity_ids(hass, [service_ent_id]) return [ent_id for ent_id in group.expand_entity_ids(hass, service_ent_id)] - - -def validate_service_call(config): - """Validate service call configuration. - - Helper method to validate that a configuration is a valid service call. - Returns None if validation succeeds, else an error description - """ - try: - cv.SERVICE_SCHEMA(config) - return None - except vol.Invalid as ex: - return str(ex) diff --git a/tests/helpers/test_service.py b/tests/helpers/test_service.py index 11ace1ab5d8..5372b6a77d4 100644 --- a/tests/helpers/test_service.py +++ b/tests/helpers/test_service.py @@ -140,21 +140,3 @@ class TestServiceHelpers(unittest.TestCase): self.assertEqual(['light.ceiling', 'light.kitchen'], service.extract_entity_ids(self.hass, call)) - - def test_validate_service_call(self): - """Test is_valid_service_call method.""" - self.assertNotEqual( - service.validate_service_call( - {}), - None - ) - self.assertEqual( - service.validate_service_call( - {'service': 'test_domain.test_service'}), - None - ) - self.assertEqual( - service.validate_service_call( - {'service_template': 'test_domain.{{ \'test_service\' }}'}), - None - )