From 46f3337b07266f701fff9e8cb5a5ffbb644d921c Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 4 Oct 2016 08:41:08 +0200 Subject: [PATCH] Customize initial state of automation --- .../components/automation/__init__.py | 7 +++- tests/components/automation/test_init.py | 40 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 579d4b400035c3..0de3cf93df1794 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -38,6 +38,7 @@ CONF_ACTION = 'action' CONF_TRIGGER = 'trigger' CONF_CONDITION_TYPE = 'condition_type' +CONF_INITIAL_STATE = 'initial_state' CONDITION_USE_TRIGGER_VALUES = 'use_trigger_values' CONDITION_TYPE_AND = 'and' @@ -45,6 +46,7 @@ DEFAULT_CONDITION_TYPE = CONDITION_TYPE_AND DEFAULT_HIDE_ENTITY = False +DEFAULT_INITIAL_STATE = True ATTR_LAST_TRIGGERED = 'last_triggered' ATTR_VARIABLES = 'variables' @@ -79,6 +81,8 @@ def _platform_validator(config): PLATFORM_SCHEMA = vol.Schema({ CONF_ALIAS: cv.string, + vol.Optional(CONF_INITIAL_STATE, + default=DEFAULT_INITIAL_STATE): cv.boolean, vol.Optional(CONF_HIDE_ENTITY, default=DEFAULT_HIDE_ENTITY): cv.boolean, vol.Required(CONF_TRIGGER): _TRIGGER_SCHEMA, vol.Optional(CONF_CONDITION): _CONDITION_SCHEMA, @@ -333,7 +337,8 @@ def cond_func(variables): config_block.get(CONF_TRIGGER, []), name) entity = AutomationEntity(name, async_attach_triggers, cond_func, action, hidden) - tasks.append(hass.loop.create_task(entity.async_enable())) + if config_block[CONF_INITIAL_STATE]: + tasks.append(hass.loop.create_task(entity.async_enable())) entities.append(entity) yield from asyncio.gather(*tasks, loop=hass.loop) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 8cc7825bb1f699..f33c6964f2e657 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -96,6 +96,46 @@ def test_service_specify_entity_id(self): self.assertEqual(['hello.world'], self.calls[0].data.get(ATTR_ENTITY_ID)) + def test_service_initial_value_off(self): + """Test initial value off.""" + entity_id = 'automation.hello' + + assert setup_component(self.hass, automation.DOMAIN, { + automation.DOMAIN: { + 'alias': 'hello', + 'initial_state': 'off', + 'trigger': { + 'platform': 'event', + 'event_type': 'test_event', + }, + 'action': { + 'service': 'test.automation', + 'entity_id': ['hello.world', 'hello.world2'] + } + } + }) + assert not automation.is_on(self.hass, entity_id) + + def test_service_initial_value_on(self): + """Test initial value on.""" + entity_id = 'automation.hello' + + assert setup_component(self.hass, automation.DOMAIN, { + automation.DOMAIN: { + 'alias': 'hello', + 'initial_state': 'on', + 'trigger': { + 'platform': 'event', + 'event_type': 'test_event', + }, + 'action': { + 'service': 'test.automation', + 'entity_id': ['hello.world', 'hello.world2'] + } + } + }) + assert automation.is_on(self.hass, entity_id) + def test_service_specify_entity_id_list(self): """Test service data.""" assert setup_component(self.hass, automation.DOMAIN, {