mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add condition type to automation component
This commit is contained in:
parent
20f021d05f
commit
b2ad8db86b
@ -23,6 +23,11 @@ CONF_SERVICE_DATA = "service_data"
|
|||||||
CONF_CONDITION = "condition"
|
CONF_CONDITION = "condition"
|
||||||
CONF_ACTION = 'action'
|
CONF_ACTION = 'action'
|
||||||
CONF_TRIGGER = "trigger"
|
CONF_TRIGGER = "trigger"
|
||||||
|
CONF_CONDITION_TYPE = "condition_type"
|
||||||
|
|
||||||
|
CONDITION_TYPE_AND = "and"
|
||||||
|
CONDITION_TYPE_OR = "or"
|
||||||
|
DEFAULT_CONDITION_TYPE = CONDITION_TYPE_AND
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -44,8 +49,13 @@ def setup(hass, config):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if CONF_CONDITION in p_config:
|
if CONF_CONDITION in p_config:
|
||||||
|
cond_type = p_config.get(CONF_CONDITION_TYPE,
|
||||||
|
DEFAULT_CONDITION_TYPE).lower()
|
||||||
action = _process_if(hass, config, p_config[CONF_CONDITION],
|
action = _process_if(hass, config, p_config[CONF_CONDITION],
|
||||||
action)
|
action, cond_type)
|
||||||
|
|
||||||
|
if action is None:
|
||||||
|
continue
|
||||||
|
|
||||||
_process_trigger(hass, config, p_config.get(CONF_TRIGGER, []), name,
|
_process_trigger(hass, config, p_config.get(CONF_TRIGGER, []), name,
|
||||||
action)
|
action)
|
||||||
@ -116,21 +126,36 @@ def _migrate_old_config(config):
|
|||||||
return new_conf
|
return new_conf
|
||||||
|
|
||||||
|
|
||||||
def _process_if(hass, config, if_configs, action):
|
def _process_if(hass, config, if_configs, action, cond_type):
|
||||||
""" Processes if checks. """
|
""" Processes if checks. """
|
||||||
|
|
||||||
if isinstance(if_configs, dict):
|
if isinstance(if_configs, dict):
|
||||||
if_configs = [if_configs]
|
if_configs = [if_configs]
|
||||||
|
|
||||||
|
checks = []
|
||||||
for if_config in if_configs:
|
for if_config in if_configs:
|
||||||
platform = _resolve_platform('condition', hass, config,
|
platform = _resolve_platform('condition', hass, config,
|
||||||
if_config.get(CONF_PLATFORM))
|
if_config.get(CONF_PLATFORM))
|
||||||
if platform is None:
|
if platform is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
action = platform.if_action(hass, if_config, action)
|
check = platform.if_action(hass, if_config)
|
||||||
|
|
||||||
return action
|
if check is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
checks.append(check)
|
||||||
|
|
||||||
|
if cond_type == CONDITION_TYPE_AND:
|
||||||
|
def if_action():
|
||||||
|
if all(check() for check in checks):
|
||||||
|
action()
|
||||||
|
else:
|
||||||
|
def if_action():
|
||||||
|
if any(check() for check in checks):
|
||||||
|
action()
|
||||||
|
|
||||||
|
return if_action
|
||||||
|
|
||||||
|
|
||||||
def _process_trigger(hass, config, trigger_configs, name, action):
|
def _process_trigger(hass, config, trigger_configs, name, action):
|
||||||
|
@ -48,14 +48,14 @@ def trigger(hass, config, action):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def if_action(hass, config, action):
|
def if_action(hass, config):
|
||||||
""" Wraps action method with state based condition. """
|
""" Wraps action method with state based condition. """
|
||||||
|
|
||||||
entity_id = config.get(CONF_ENTITY_ID)
|
entity_id = config.get(CONF_ENTITY_ID)
|
||||||
|
|
||||||
if entity_id is None:
|
if entity_id is None:
|
||||||
_LOGGER.error("Missing configuration key %s", CONF_ENTITY_ID)
|
_LOGGER.error("Missing configuration key %s", CONF_ENTITY_ID)
|
||||||
return action
|
return None
|
||||||
|
|
||||||
below = config.get(CONF_BELOW)
|
below = config.get(CONF_BELOW)
|
||||||
above = config.get(CONF_ABOVE)
|
above = config.get(CONF_ABOVE)
|
||||||
@ -64,16 +64,14 @@ def if_action(hass, config, action):
|
|||||||
_LOGGER.error("Missing configuration key."
|
_LOGGER.error("Missing configuration key."
|
||||||
" One of %s or %s is required",
|
" One of %s or %s is required",
|
||||||
CONF_BELOW, CONF_ABOVE)
|
CONF_BELOW, CONF_ABOVE)
|
||||||
return action
|
return None
|
||||||
|
|
||||||
def state_if():
|
|
||||||
""" Execute action if state matches. """
|
|
||||||
|
|
||||||
|
def if_numeric_state():
|
||||||
|
""" Test numeric state condition. """
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
if state is not None and _in_range(state.state, above, below):
|
return state is not None and _in_range(state.state, above, below)
|
||||||
action()
|
|
||||||
|
|
||||||
return state_if
|
return if_numeric_state
|
||||||
|
|
||||||
|
|
||||||
def _in_range(value, range_start, range_end):
|
def _in_range(value, range_start, range_end):
|
||||||
|
@ -38,7 +38,7 @@ def trigger(hass, config, action):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def if_action(hass, config, action):
|
def if_action(hass, config):
|
||||||
""" Wraps action method with state based condition. """
|
""" Wraps action method with state based condition. """
|
||||||
entity_id = config.get(CONF_ENTITY_ID)
|
entity_id = config.get(CONF_ENTITY_ID)
|
||||||
state = config.get(CONF_STATE)
|
state = config.get(CONF_STATE)
|
||||||
@ -47,11 +47,12 @@ def if_action(hass, config, action):
|
|||||||
logging.getLogger(__name__).error(
|
logging.getLogger(__name__).error(
|
||||||
"Missing if-condition configuration key %s or %s", CONF_ENTITY_ID,
|
"Missing if-condition configuration key %s or %s", CONF_ENTITY_ID,
|
||||||
CONF_STATE)
|
CONF_STATE)
|
||||||
return action
|
return None
|
||||||
|
|
||||||
def state_if():
|
state = str(state)
|
||||||
""" Execute action if state matches. """
|
|
||||||
if hass.states.is_state(entity_id, state):
|
|
||||||
action()
|
|
||||||
|
|
||||||
return state_if
|
def if_state():
|
||||||
|
""" Test if condition. """
|
||||||
|
return hass.states.is_state(entity_id, state)
|
||||||
|
|
||||||
|
return if_state
|
||||||
|
@ -36,7 +36,7 @@ def trigger(hass, config, action):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def if_action(hass, config, action):
|
def if_action(hass, config):
|
||||||
""" Wraps action method with time based condition. """
|
""" Wraps action method with time based condition. """
|
||||||
before = config.get(CONF_BEFORE)
|
before = config.get(CONF_BEFORE)
|
||||||
after = config.get(CONF_AFTER)
|
after = config.get(CONF_AFTER)
|
||||||
@ -46,6 +46,7 @@ def if_action(hass, config, action):
|
|||||||
logging.getLogger(__name__).error(
|
logging.getLogger(__name__).error(
|
||||||
"Missing if-condition configuration key %s, %s or %s",
|
"Missing if-condition configuration key %s, %s or %s",
|
||||||
CONF_BEFORE, CONF_AFTER, CONF_WEEKDAY)
|
CONF_BEFORE, CONF_AFTER, CONF_WEEKDAY)
|
||||||
|
return None
|
||||||
|
|
||||||
def time_if():
|
def time_if():
|
||||||
""" Validate time based if-condition """
|
""" Validate time based if-condition """
|
||||||
@ -59,7 +60,7 @@ def if_action(hass, config, action):
|
|||||||
minute=int(before_m))
|
minute=int(before_m))
|
||||||
|
|
||||||
if now > before_point:
|
if now > before_point:
|
||||||
return
|
return False
|
||||||
|
|
||||||
if after is not None:
|
if after is not None:
|
||||||
# Strip seconds if given
|
# Strip seconds if given
|
||||||
@ -68,15 +69,15 @@ def if_action(hass, config, action):
|
|||||||
after_point = now.replace(hour=int(after_h), minute=int(after_m))
|
after_point = now.replace(hour=int(after_h), minute=int(after_m))
|
||||||
|
|
||||||
if now < after_point:
|
if now < after_point:
|
||||||
return
|
return False
|
||||||
|
|
||||||
if weekday is not None:
|
if weekday is not None:
|
||||||
now_weekday = WEEKDAYS[now.weekday()]
|
now_weekday = WEEKDAYS[now.weekday()]
|
||||||
|
|
||||||
if isinstance(weekday, str) and weekday != now_weekday or \
|
if isinstance(weekday, str) and weekday != now_weekday or \
|
||||||
now_weekday not in weekday:
|
now_weekday not in weekday:
|
||||||
return
|
return False
|
||||||
|
|
||||||
action()
|
return True
|
||||||
|
|
||||||
return time_if
|
return time_if
|
||||||
|
@ -162,7 +162,6 @@ class TestAutomationEvent(unittest.TestCase):
|
|||||||
],
|
],
|
||||||
'action': {
|
'action': {
|
||||||
'execute_service': 'test.automation',
|
'execute_service': 'test.automation',
|
||||||
'service_entity_id': ['hello.world', 'hello.world2']
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -173,3 +172,90 @@ class TestAutomationEvent(unittest.TestCase):
|
|||||||
self.hass.states.set('test.entity', 'hello')
|
self.hass.states.set('test.entity', 'hello')
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
self.assertEqual(2, len(self.calls))
|
self.assertEqual(2, len(self.calls))
|
||||||
|
|
||||||
|
def test_two_conditions_with_and(self):
|
||||||
|
entity_id = 'test.entity'
|
||||||
|
automation.setup(self.hass, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': [
|
||||||
|
{
|
||||||
|
'platform': 'event',
|
||||||
|
'event_type': 'test_event',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
'condition': [
|
||||||
|
{
|
||||||
|
'platform': 'state',
|
||||||
|
'entity_id': entity_id,
|
||||||
|
'state': 100
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'platform': 'numeric_state',
|
||||||
|
'entity_id': entity_id,
|
||||||
|
'below': 150
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'action': {
|
||||||
|
'execute_service': 'test.automation',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
self.hass.states.set(entity_id, 100)
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
|
self.hass.states.set(entity_id, 101)
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
|
self.hass.states.set(entity_id, 151)
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
|
def test_two_conditions_with_or(self):
|
||||||
|
entity_id = 'test.entity'
|
||||||
|
automation.setup(self.hass, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': [
|
||||||
|
{
|
||||||
|
'platform': 'event',
|
||||||
|
'event_type': 'test_event',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
'condition_type': 'OR',
|
||||||
|
'condition': [
|
||||||
|
{
|
||||||
|
'platform': 'state',
|
||||||
|
'entity_id': entity_id,
|
||||||
|
'state': 200
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'platform': 'numeric_state',
|
||||||
|
'entity_id': entity_id,
|
||||||
|
'below': 150
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'action': {
|
||||||
|
'execute_service': 'test.automation',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
self.hass.states.set(entity_id, 200)
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
|
self.hass.states.set(entity_id, 100)
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(2, len(self.calls))
|
||||||
|
|
||||||
|
self.hass.states.set(entity_id, 250)
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(2, len(self.calls))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user