mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Service/Script cleanup
This commit is contained in:
parent
533799656e
commit
14bd630c1d
@ -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)],
|
||||
)
|
||||
|
@ -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."""
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user