2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the location automation."""
|
2015-09-29 07:18:52 +00:00
|
|
|
import unittest
|
|
|
|
|
2018-09-04 19:16:24 +00:00
|
|
|
from homeassistant.core import Context, callback
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2015-09-29 07:18:52 +00:00
|
|
|
from homeassistant.components import automation, zone
|
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
from tests.common import get_test_home_assistant, mock_component
|
2018-09-26 07:49:55 +00:00
|
|
|
from tests.components.automation import common
|
2015-09-29 07:18:52 +00:00
|
|
|
|
|
|
|
|
2016-12-02 05:45:19 +00:00
|
|
|
# pylint: disable=invalid-name
|
2015-10-03 06:57:26 +00:00
|
|
|
class TestAutomationZone(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the event automation."""
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2016-12-02 05:45:19 +00:00
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2015-09-29 07:18:52 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2017-03-01 04:33:19 +00:00
|
|
|
mock_component(self.hass, 'group')
|
2016-09-17 17:29:58 +00:00
|
|
|
assert setup_component(self.hass, zone.DOMAIN, {
|
2015-09-30 06:08:37 +00:00
|
|
|
'zone': {
|
|
|
|
'name': 'test',
|
2015-09-29 07:18:52 +00:00
|
|
|
'latitude': 32.880837,
|
|
|
|
'longitude': -117.237561,
|
|
|
|
'radius': 250,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
self.calls = []
|
|
|
|
|
2016-11-05 23:36:20 +00:00
|
|
|
@callback
|
2015-09-29 07:18:52 +00:00
|
|
|
def record_call(service):
|
2018-08-24 08:28:43 +00:00
|
|
|
"""Record calls."""
|
2015-09-29 07:18:52 +00:00
|
|
|
self.calls.append(service)
|
|
|
|
|
|
|
|
self.hass.services.register('test', 'automation', record_call)
|
|
|
|
|
2016-12-02 05:45:19 +00:00
|
|
|
def tearDown(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2015-09-29 07:18:52 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_if_fires_on_zone_enter(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on zone enter."""
|
2018-09-04 19:16:24 +00:00
|
|
|
context = Context()
|
2015-09-29 07:18:52 +00:00
|
|
|
self.hass.states.set('test.entity', 'hello', {
|
|
|
|
'latitude': 32.881011,
|
|
|
|
'longitude': -117.234758
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2016-09-17 17:29:58 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-29 07:18:52 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'zone',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'zone': 'zone.test',
|
|
|
|
'event': 'enter',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
2016-04-21 20:59:42 +00:00
|
|
|
'data_template': {
|
|
|
|
'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join((
|
2016-12-02 05:45:19 +00:00
|
|
|
'platform', 'entity_id',
|
|
|
|
'from_state.state', 'to_state.state',
|
|
|
|
'zone.name'))
|
2016-04-21 20:59:42 +00:00
|
|
|
},
|
|
|
|
|
2015-09-29 07:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-29 07:18:52 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'hello', {
|
|
|
|
'latitude': 32.880586,
|
|
|
|
'longitude': -117.237564
|
2018-09-04 19:16:24 +00:00
|
|
|
}, context=context)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == len(self.calls)
|
2018-09-04 19:16:24 +00:00
|
|
|
assert self.calls[0].context is context
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'zone - test.entity - hello - hello - test' == \
|
|
|
|
self.calls[0].data['some']
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2016-08-26 06:25:57 +00:00
|
|
|
# Set out of zone again so we can trigger call
|
|
|
|
self.hass.states.set('test.entity', 'hello', {
|
|
|
|
'latitude': 32.881011,
|
|
|
|
'longitude': -117.234758
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
|
2018-09-26 07:49:55 +00:00
|
|
|
common.turn_off(self.hass)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'hello', {
|
|
|
|
'latitude': 32.880586,
|
|
|
|
'longitude': -117.237564
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == len(self.calls)
|
2016-08-26 06:25:57 +00:00
|
|
|
|
2015-09-29 07:18:52 +00:00
|
|
|
def test_if_not_fires_for_enter_on_zone_leave(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for not firing on zone leave."""
|
2015-09-29 07:18:52 +00:00
|
|
|
self.hass.states.set('test.entity', 'hello', {
|
|
|
|
'latitude': 32.880586,
|
|
|
|
'longitude': -117.237564
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2016-09-17 17:29:58 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-29 07:18:52 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'zone',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'zone': 'zone.test',
|
|
|
|
'event': 'enter',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-29 07:18:52 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'hello', {
|
|
|
|
'latitude': 32.881011,
|
|
|
|
'longitude': -117.234758
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 0 == len(self.calls)
|
2015-09-29 07:18:52 +00:00
|
|
|
|
|
|
|
def test_if_fires_on_zone_leave(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for firing on zone leave."""
|
2015-09-29 07:18:52 +00:00
|
|
|
self.hass.states.set('test.entity', 'hello', {
|
|
|
|
'latitude': 32.880586,
|
|
|
|
'longitude': -117.237564
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2016-09-17 17:29:58 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-29 07:18:52 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'zone',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'zone': 'zone.test',
|
|
|
|
'event': 'leave',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-29 07:18:52 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'hello', {
|
|
|
|
'latitude': 32.881011,
|
|
|
|
'longitude': -117.234758
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == len(self.calls)
|
2015-09-29 07:18:52 +00:00
|
|
|
|
|
|
|
def test_if_not_fires_for_leave_on_zone_enter(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for not firing on zone enter."""
|
2015-09-29 07:18:52 +00:00
|
|
|
self.hass.states.set('test.entity', 'hello', {
|
|
|
|
'latitude': 32.881011,
|
|
|
|
'longitude': -117.234758
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2016-09-17 17:29:58 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-29 07:18:52 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'zone',
|
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'zone': 'zone.test',
|
|
|
|
'event': 'leave',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-29 07:18:52 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.entity', 'hello', {
|
|
|
|
'latitude': 32.880586,
|
|
|
|
'longitude': -117.237564
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 0 == len(self.calls)
|
2015-09-29 07:18:52 +00:00
|
|
|
|
|
|
|
def test_zone_condition(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for zone condition."""
|
2015-09-29 07:18:52 +00:00
|
|
|
self.hass.states.set('test.entity', 'hello', {
|
|
|
|
'latitude': 32.880586,
|
|
|
|
'longitude': -117.237564
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2016-09-17 17:29:58 +00:00
|
|
|
assert setup_component(self.hass, automation.DOMAIN, {
|
2015-09-29 07:18:52 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event_type': 'test_event'
|
|
|
|
},
|
|
|
|
'condition': {
|
2016-10-01 08:22:13 +00:00
|
|
|
'condition': 'zone',
|
2015-09-29 07:18:52 +00:00
|
|
|
'entity_id': 'test.entity',
|
|
|
|
'zone': 'zone.test',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-09-29 07:18:52 +00:00
|
|
|
|
|
|
|
self.hass.bus.fire('test_event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == len(self.calls)
|