Skip to content

Commit

Permalink
Fix 'same state' monitoring in numeric_state trigger (home-assistant#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbruckner authored and pvizeli committed Jul 2, 2019
1 parent c0a342d commit 945afbc
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion homeassistant/components/automation/numeric_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def call_action():

if time_delta:
unsub_track_same[entity] = async_track_same_state(
hass, time_delta, call_action, entity_ids=entity_id,
hass, time_delta, call_action, entity_ids=entity,
async_check_same_func=check_numeric_state)
else:
call_action()
Expand Down
103 changes: 103 additions & 0 deletions tests/components/automation/test_numeric_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,3 +906,106 @@ async def test_wait_template_with_trigger(hass, calls):
assert 1 == len(calls)
assert 'numeric_state - test.entity - 12' == \
calls[0].data['some']


async def test_if_fires_on_entities_change_no_overlap(hass, calls):
"""Test for firing on entities change with no overlap."""
assert await async_setup_component(hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'numeric_state',
'entity_id': [
'test.entity_1',
'test.entity_2',
],
'above': 8,
'below': 12,
'for': {
'seconds': 5
},
},
'action': {
'service': 'test.automation',
'data_template': {
'some': '{{ trigger.entity_id }}',
},
}
}
})
await hass.async_block_till_done()

utcnow = dt_util.utcnow()
with patch('homeassistant.core.dt_util.utcnow') as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set('test.entity_1', 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=10)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert 1 == len(calls)
assert 'test.entity_1' == calls[0].data['some']

hass.states.async_set('test.entity_2', 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=10)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert 2 == len(calls)
assert 'test.entity_2' == calls[1].data['some']


async def test_if_fires_on_entities_change_overlap(hass, calls):
"""Test for firing on entities change with overlap."""
assert await async_setup_component(hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'numeric_state',
'entity_id': [
'test.entity_1',
'test.entity_2',
],
'above': 8,
'below': 12,
'for': {
'seconds': 5
},
},
'action': {
'service': 'test.automation',
'data_template': {
'some': '{{ trigger.entity_id }}',
},
}
}
})
await hass.async_block_till_done()

utcnow = dt_util.utcnow()
with patch('homeassistant.core.dt_util.utcnow') as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set('test.entity_1', 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set('test.entity_2', 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set('test.entity_2', 15)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set('test.entity_2', 9)
await hass.async_block_till_done()
assert 0 == len(calls)
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert 1 == len(calls)
assert 'test.entity_1' == calls[0].data['some']

mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert 2 == len(calls)
assert 'test.entity_2' == calls[1].data['some']

0 comments on commit 945afbc

Please sign in to comment.