mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Merge pull request #1346 from stefan-jonasson/add_for_to_conditions
Added the "for" param to the conditions as well
This commit is contained in:
commit
88dc7a08c4
@ -24,24 +24,11 @@ CONF_STATE = "state"
|
|||||||
CONF_FOR = "for"
|
CONF_FOR = "for"
|
||||||
|
|
||||||
|
|
||||||
def trigger(hass, config, action):
|
def get_time_config(config):
|
||||||
""" Listen for state changes based on `config`. """
|
""" Helper function to extract the time specified in the config """
|
||||||
entity_id = config.get(CONF_ENTITY_ID)
|
if CONF_FOR not in config:
|
||||||
|
return None
|
||||||
|
|
||||||
if entity_id is None:
|
|
||||||
logging.getLogger(__name__).error(
|
|
||||||
"Missing trigger configuration key %s", CONF_ENTITY_ID)
|
|
||||||
return False
|
|
||||||
|
|
||||||
from_state = config.get(CONF_FROM, MATCH_ALL)
|
|
||||||
to_state = config.get(CONF_TO) or config.get(CONF_STATE) or MATCH_ALL
|
|
||||||
|
|
||||||
if isinstance(from_state, bool) or isinstance(to_state, bool):
|
|
||||||
logging.getLogger(__name__).error(
|
|
||||||
'Config error. Surround to/from values with quotes.')
|
|
||||||
return False
|
|
||||||
|
|
||||||
if CONF_FOR in config:
|
|
||||||
hours = config[CONF_FOR].get(CONF_HOURS)
|
hours = config[CONF_FOR].get(CONF_HOURS)
|
||||||
minutes = config[CONF_FOR].get(CONF_MINUTES)
|
minutes = config[CONF_FOR].get(CONF_MINUTES)
|
||||||
seconds = config[CONF_FOR].get(CONF_SECONDS)
|
seconds = config[CONF_FOR].get(CONF_SECONDS)
|
||||||
@ -50,13 +37,39 @@ def trigger(hass, config, action):
|
|||||||
logging.getLogger(__name__).error(
|
logging.getLogger(__name__).error(
|
||||||
"Received invalid value for '%s': %s",
|
"Received invalid value for '%s': %s",
|
||||||
config[CONF_FOR], CONF_FOR)
|
config[CONF_FOR], CONF_FOR)
|
||||||
return False
|
return None
|
||||||
|
|
||||||
if config.get(CONF_TO) is None and config.get(CONF_STATE) is None:
|
if config.get(CONF_TO) is None and config.get(CONF_STATE) is None:
|
||||||
logging.getLogger(__name__).error(
|
logging.getLogger(__name__).error(
|
||||||
"For: requires a to: value'%s': %s",
|
"For: requires a to: value'%s': %s",
|
||||||
config[CONF_FOR], CONF_FOR)
|
config[CONF_FOR], CONF_FOR)
|
||||||
return False
|
return None
|
||||||
|
|
||||||
|
return timedelta(hours=(hours or 0.0),
|
||||||
|
minutes=(minutes or 0.0),
|
||||||
|
seconds=(seconds or 0.0))
|
||||||
|
|
||||||
|
|
||||||
|
def trigger(hass, config, action):
|
||||||
|
""" Listen for state changes based on `config`. """
|
||||||
|
entity_id = config.get(CONF_ENTITY_ID)
|
||||||
|
|
||||||
|
if entity_id is None:
|
||||||
|
logging.getLogger(__name__).error(
|
||||||
|
"Missing trigger configuration key %s", CONF_ENTITY_ID)
|
||||||
|
return None
|
||||||
|
|
||||||
|
from_state = config.get(CONF_FROM, MATCH_ALL)
|
||||||
|
to_state = config.get(CONF_TO) or config.get(CONF_STATE) or MATCH_ALL
|
||||||
|
time_delta = get_time_config(config)
|
||||||
|
|
||||||
|
if isinstance(from_state, bool) or isinstance(to_state, bool):
|
||||||
|
logging.getLogger(__name__).error(
|
||||||
|
'Config error. Surround to/from values with quotes.')
|
||||||
|
return None
|
||||||
|
|
||||||
|
if CONF_FOR in config and time_delta is None:
|
||||||
|
return None
|
||||||
|
|
||||||
def state_automation_listener(entity, from_s, to_s):
|
def state_automation_listener(entity, from_s, to_s):
|
||||||
""" Listens for state changes and calls action. """
|
""" Listens for state changes and calls action. """
|
||||||
@ -76,12 +89,8 @@ def trigger(hass, config, action):
|
|||||||
hass.bus.remove_listener(
|
hass.bus.remove_listener(
|
||||||
EVENT_STATE_CHANGED, for_state_listener)
|
EVENT_STATE_CHANGED, for_state_listener)
|
||||||
|
|
||||||
if CONF_FOR in config:
|
if time_delta is not None:
|
||||||
now = dt_util.now()
|
target_tm = dt_util.utcnow() + time_delta
|
||||||
target_tm = now + timedelta(
|
|
||||||
hours=(hours or 0.0),
|
|
||||||
minutes=(minutes or 0.0),
|
|
||||||
seconds=(seconds or 0.0))
|
|
||||||
for_time_listener = track_point_in_time(
|
for_time_listener = track_point_in_time(
|
||||||
hass, state_for_listener, target_tm)
|
hass, state_for_listener, target_tm)
|
||||||
for_state_listener = track_state_change(
|
for_state_listener = track_state_change(
|
||||||
@ -107,10 +116,18 @@ def if_action(hass, config):
|
|||||||
CONF_STATE)
|
CONF_STATE)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
time_delta = get_time_config(config)
|
||||||
|
if CONF_FOR in config and time_delta is None:
|
||||||
|
return None
|
||||||
|
|
||||||
state = str(state)
|
state = str(state)
|
||||||
|
|
||||||
def if_state():
|
def if_state():
|
||||||
""" Test if condition. """
|
""" Test if condition. """
|
||||||
return hass.states.is_state(entity_id, state)
|
is_state = hass.states.is_state(entity_id, state)
|
||||||
|
return (time_delta is None and is_state or
|
||||||
|
time_delta is not None and
|
||||||
|
dt_util.utcnow() - time_delta >
|
||||||
|
hass.states.get(entity_id).last_changed)
|
||||||
|
|
||||||
return if_state
|
return if_state
|
||||||
|
@ -6,6 +6,7 @@ Tests state automation.
|
|||||||
"""
|
"""
|
||||||
import unittest
|
import unittest
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
import homeassistant.components.automation as automation
|
import homeassistant.components.automation as automation
|
||||||
@ -423,3 +424,60 @@ class TestAutomationState(unittest.TestCase):
|
|||||||
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
|
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
self.assertEqual(1, len(self.calls))
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
|
def test_if_fires_on_for_condition(self):
|
||||||
|
point1 = dt_util.utcnow()
|
||||||
|
point2 = point1 + timedelta(seconds=10)
|
||||||
|
with patch('homeassistant.core.dt_util.utcnow') as mock_utcnow:
|
||||||
|
mock_utcnow.return_value = point1
|
||||||
|
self.hass.states.set('test.entity', 'on')
|
||||||
|
self.assertTrue(automation.setup(self.hass, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'event',
|
||||||
|
'event_type': 'test_event',
|
||||||
|
},
|
||||||
|
'condition': {
|
||||||
|
'platform': 'state',
|
||||||
|
'entity_id': 'test.entity',
|
||||||
|
'state': 'on',
|
||||||
|
'for': {
|
||||||
|
'seconds': 5
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
# not enough time has passed
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(0, len(self.calls))
|
||||||
|
|
||||||
|
# Time travel 10 secs into the future
|
||||||
|
mock_utcnow.return_value = point2
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
|
def test_if_fails_setup_for_without_time(self):
|
||||||
|
self.assertIsNone(state.if_action(
|
||||||
|
self.hass, {
|
||||||
|
'platform': 'state',
|
||||||
|
'entity_id': 'test.entity',
|
||||||
|
'state': 'on',
|
||||||
|
'for': {},
|
||||||
|
}))
|
||||||
|
|
||||||
|
def test_if_fails_setup_for_without_entity(self):
|
||||||
|
self.assertIsNone(state.if_action(
|
||||||
|
self.hass, {
|
||||||
|
'platform': 'state',
|
||||||
|
'state': 'on',
|
||||||
|
'for': {
|
||||||
|
'seconds': 5
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user