Service/Script cleanup
This commit is contained in:
parent
533799656e
commit
14bd630c1d
4 changed files with 15 additions and 45 deletions
|
@ -264,14 +264,12 @@ SERVICE_SCHEMA = vol.All(vol.Schema({
|
||||||
vol.Optional('entity_id'): entity_ids,
|
vol.Optional('entity_id'): entity_ids,
|
||||||
}), has_at_least_one_key('service', 'service_template'))
|
}), has_at_least_one_key('service', 'service_template'))
|
||||||
|
|
||||||
# ----- SCRIPT
|
_SCRIPT_DELAY_SCHEMA = vol.Schema({
|
||||||
|
|
||||||
_DELAY_SCHEMA = vol.Schema({
|
|
||||||
vol.Optional(CONF_ALIAS): string,
|
vol.Optional(CONF_ALIAS): string,
|
||||||
vol.Required("delay"): vol.All(time_period, positive_timedelta)
|
vol.Required("delay"): vol.All(time_period, positive_timedelta)
|
||||||
})
|
})
|
||||||
|
|
||||||
SCRIPT_SCHEMA = vol.All(
|
SCRIPT_SCHEMA = vol.All(
|
||||||
ensure_list,
|
ensure_list,
|
||||||
[vol.Any(SERVICE_SCHEMA, _DELAY_SCHEMA, EVENT_SCHEMA)],
|
[vol.Any(SERVICE_SCHEMA, _SCRIPT_DELAY_SCHEMA, EVENT_SCHEMA)],
|
||||||
)
|
)
|
||||||
|
|
|
@ -76,12 +76,12 @@ class Script():
|
||||||
self._change_listener()
|
self._change_listener()
|
||||||
return
|
return
|
||||||
|
|
||||||
elif service.validate_service_call(action) is None:
|
|
||||||
self._call_service(action, variables)
|
|
||||||
|
|
||||||
elif CONF_EVENT in action:
|
elif CONF_EVENT in action:
|
||||||
self._fire_event(action)
|
self._fire_event(action)
|
||||||
|
|
||||||
|
else:
|
||||||
|
self._call_service(action, variables)
|
||||||
|
|
||||||
self._cur = -1
|
self._cur = -1
|
||||||
self.last_action = None
|
self.last_action = None
|
||||||
if self._change_listener:
|
if self._change_listener:
|
||||||
|
@ -102,7 +102,8 @@ class Script():
|
||||||
"""Call the service specified in the action."""
|
"""Call the service specified in the action."""
|
||||||
self.last_action = action.get(CONF_ALIAS, 'call service')
|
self.last_action = action.get(CONF_ALIAS, 'call service')
|
||||||
self._log("Executing step %s", self.last_action)
|
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):
|
def _fire_event(self, action):
|
||||||
"""Fire an event."""
|
"""Fire an event."""
|
||||||
|
|
|
@ -32,8 +32,10 @@ def service(domain, service_name):
|
||||||
return register_service_decorator
|
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."""
|
"""Call a service based on a config hash."""
|
||||||
|
if validate_config:
|
||||||
try:
|
try:
|
||||||
config = cv.SERVICE_SCHEMA(config)
|
config = cv.SERVICE_SCHEMA(config)
|
||||||
except vol.Invalid as ex:
|
except vol.Invalid as ex:
|
||||||
|
@ -85,16 +87,3 @@ def extract_entity_ids(hass, service_call):
|
||||||
return group.expand_entity_ids(hass, [service_ent_id])
|
return group.expand_entity_ids(hass, [service_ent_id])
|
||||||
|
|
||||||
return [ent_id for ent_id in 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)
|
|
||||||
|
|
|
@ -140,21 +140,3 @@ class TestServiceHelpers(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(['light.ceiling', 'light.kitchen'],
|
self.assertEqual(['light.ceiling', 'light.kitchen'],
|
||||||
service.extract_entity_ids(self.hass, call))
|
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
|
|
||||||
)
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue