Merge pull request #3651 from home-assistant/automation
Customize initial state of automation
This commit is contained in:
commit
b8beae9c6c
2 changed files with 46 additions and 1 deletions
|
@ -40,6 +40,7 @@ CONF_CONDITION = 'condition'
|
||||||
CONF_ACTION = 'action'
|
CONF_ACTION = 'action'
|
||||||
CONF_TRIGGER = 'trigger'
|
CONF_TRIGGER = 'trigger'
|
||||||
CONF_CONDITION_TYPE = 'condition_type'
|
CONF_CONDITION_TYPE = 'condition_type'
|
||||||
|
CONF_INITIAL_STATE = 'initial_state'
|
||||||
|
|
||||||
CONDITION_USE_TRIGGER_VALUES = 'use_trigger_values'
|
CONDITION_USE_TRIGGER_VALUES = 'use_trigger_values'
|
||||||
CONDITION_TYPE_AND = 'and'
|
CONDITION_TYPE_AND = 'and'
|
||||||
|
@ -47,6 +48,7 @@ CONDITION_TYPE_OR = 'or'
|
||||||
|
|
||||||
DEFAULT_CONDITION_TYPE = CONDITION_TYPE_AND
|
DEFAULT_CONDITION_TYPE = CONDITION_TYPE_AND
|
||||||
DEFAULT_HIDE_ENTITY = False
|
DEFAULT_HIDE_ENTITY = False
|
||||||
|
DEFAULT_INITIAL_STATE = True
|
||||||
|
|
||||||
ATTR_LAST_TRIGGERED = 'last_triggered'
|
ATTR_LAST_TRIGGERED = 'last_triggered'
|
||||||
ATTR_VARIABLES = 'variables'
|
ATTR_VARIABLES = 'variables'
|
||||||
|
@ -81,6 +83,8 @@ _CONDITION_SCHEMA = vol.All(cv.ensure_list, [cv.CONDITION_SCHEMA])
|
||||||
|
|
||||||
PLATFORM_SCHEMA = vol.Schema({
|
PLATFORM_SCHEMA = vol.Schema({
|
||||||
CONF_ALIAS: cv.string,
|
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.Optional(CONF_HIDE_ENTITY, default=DEFAULT_HIDE_ENTITY): cv.boolean,
|
||||||
vol.Required(CONF_TRIGGER): _TRIGGER_SCHEMA,
|
vol.Required(CONF_TRIGGER): _TRIGGER_SCHEMA,
|
||||||
vol.Optional(CONF_CONDITION): _CONDITION_SCHEMA,
|
vol.Optional(CONF_CONDITION): _CONDITION_SCHEMA,
|
||||||
|
@ -336,7 +340,8 @@ def _async_process_config(hass, config, component):
|
||||||
config_block.get(CONF_TRIGGER, []), name)
|
config_block.get(CONF_TRIGGER, []), name)
|
||||||
entity = AutomationEntity(name, async_attach_triggers, cond_func,
|
entity = AutomationEntity(name, async_attach_triggers, cond_func,
|
||||||
action, hidden)
|
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)
|
entities.append(entity)
|
||||||
|
|
||||||
yield from asyncio.gather(*tasks, loop=hass.loop)
|
yield from asyncio.gather(*tasks, loop=hass.loop)
|
||||||
|
|
|
@ -100,6 +100,46 @@ class TestAutomation(unittest.TestCase):
|
||||||
self.assertEqual(['hello.world'],
|
self.assertEqual(['hello.world'],
|
||||||
self.calls[0].data.get(ATTR_ENTITY_ID))
|
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):
|
def test_service_specify_entity_id_list(self):
|
||||||
"""Test service data."""
|
"""Test service data."""
|
||||||
assert setup_component(self.hass, automation.DOMAIN, {
|
assert setup_component(self.hass, automation.DOMAIN, {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue