core/tests/components/automation/test_zone.py

219 lines
6.6 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the location automation."""
2015-09-29 07:18:52 +00:00
import unittest
from homeassistant.core import Context, callback
from homeassistant.setup import setup_component
2015-09-29 07:18:52 +00:00
from homeassistant.components import automation, zone
from tests.common import get_test_home_assistant, mock_component
from tests.components.automation import common
2015-09-29 07:18:52 +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
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()
mock_component(self.hass, 'group')
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 = []
@callback
2015-09-29 07:18:52 +00:00
def record_call(service):
"""Record calls."""
2015-09-29 07:18:52 +00:00
self.calls.append(service)
self.hass.services.register('test', 'automation', record_call)
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."""
context = Context()
2015-09-29 07:18:52 +00:00
self.hass.states.set('test.entity', 'hello', {
'latitude': 32.881011,
'longitude': -117.234758
})
self.hass.block_till_done()
2015-09-29 07:18:52 +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',
'data_template': {
'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join((
'platform', 'entity_id',
'from_state.state', 'to_state.state',
'zone.name'))
},
2015-09-29 07:18:52 +00:00
}
}
})
2015-09-29 07:18:52 +00:00
self.hass.states.set('test.entity', 'hello', {
'latitude': 32.880586,
'longitude': -117.237564
}, context=context)
self.hass.block_till_done()
2015-09-29 07:18:52 +00:00
assert 1 == len(self.calls)
assert self.calls[0].context is context
assert 'zone - test.entity - hello - hello - test' == \
self.calls[0].data['some']
2015-09-29 07:18:52 +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
})
self.hass.block_till_done()
common.turn_off(self.hass)
self.hass.block_till_done()
self.hass.states.set('test.entity', 'hello', {
'latitude': 32.880586,
'longitude': -117.237564
})
self.hass.block_till_done()
assert 1 == len(self.calls)
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
})
self.hass.block_till_done()
2015-09-29 07:18:52 +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',
}
}
})
2015-09-29 07:18:52 +00:00
self.hass.states.set('test.entity', 'hello', {
'latitude': 32.881011,
'longitude': -117.234758
})
self.hass.block_till_done()
2015-09-29 07:18:52 +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
})
self.hass.block_till_done()
2015-09-29 07:18:52 +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',
}
}
})
2015-09-29 07:18:52 +00:00
self.hass.states.set('test.entity', 'hello', {
'latitude': 32.881011,
'longitude': -117.234758
})
self.hass.block_till_done()
2015-09-29 07:18:52 +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
})
self.hass.block_till_done()
2015-09-29 07:18:52 +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',
}
}
})
2015-09-29 07:18:52 +00:00
self.hass.states.set('test.entity', 'hello', {
'latitude': 32.880586,
'longitude': -117.237564
})
self.hass.block_till_done()
2015-09-29 07:18:52 +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
})
self.hass.block_till_done()
2015-09-29 07:18:52 +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',
}
}
})
2015-09-29 07:18:52 +00:00
self.hass.bus.fire('test_event')
self.hass.block_till_done()
assert 1 == len(self.calls)