Skip to content

Commit

Permalink
Customize initial state of automation
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel authored and Daniel committed Oct 5, 2016
1 parent d58548d commit 46f3337
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion homeassistant/components/automation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
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'
CONDITION_TYPE_OR = 'or'

DEFAULT_CONDITION_TYPE = CONDITION_TYPE_AND
DEFAULT_HIDE_ENTITY = False
DEFAULT_INITIAL_STATE = True

ATTR_LAST_TRIGGERED = 'last_triggered'
ATTR_VARIABLES = 'variables'
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions tests/components/automation/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit 46f3337

Please sign in to comment.