Service/Script cleanup

This commit is contained in:
Paulus Schoutsen 2016-04-23 07:11:21 +02:00
parent 533799656e
commit 14bd630c1d
4 changed files with 15 additions and 45 deletions

View File

@ -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)],
)

View File

@ -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."""

View File

@ -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)

View File

@ -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
)