2016-03-09 09:25:50 +00:00
|
|
|
"""Test zone component."""
|
2016-01-22 07:53:57 +00:00
|
|
|
import unittest
|
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant import setup
|
2016-01-22 07:53:57 +00:00
|
|
|
from homeassistant.components import zone
|
|
|
|
|
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
|
|
|
|
2016-01-24 22:13:39 +00:00
|
|
|
class TestComponentZone(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the zone component."""
|
2016-01-22 07:53:57 +00:00
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-01-22 07:53:57 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop down everything that was started."""
|
2016-01-22 07:53:57 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2016-09-24 07:04:03 +00:00
|
|
|
def test_setup_no_zones_still_adds_home_zone(self):
|
|
|
|
"""Test if no config is passed in we still get the home zone."""
|
2017-03-05 09:41:54 +00:00
|
|
|
assert setup.setup_component(self.hass, zone.DOMAIN,
|
|
|
|
{'zone': None})
|
2016-09-24 07:04:03 +00:00
|
|
|
|
|
|
|
assert len(self.hass.states.entity_ids('zone')) == 1
|
|
|
|
state = self.hass.states.get('zone.home')
|
|
|
|
assert self.hass.config.location_name == state.name
|
|
|
|
assert self.hass.config.latitude == state.attributes['latitude']
|
|
|
|
assert self.hass.config.longitude == state.attributes['longitude']
|
|
|
|
assert not state.attributes.get('passive', False)
|
|
|
|
|
2016-01-22 07:53:57 +00:00
|
|
|
def test_setup(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test setup."""
|
2016-01-22 07:53:57 +00:00
|
|
|
info = {
|
|
|
|
'name': 'Test Zone',
|
|
|
|
'latitude': 32.880837,
|
|
|
|
'longitude': -117.237561,
|
|
|
|
'radius': 250,
|
|
|
|
'passive': True
|
|
|
|
}
|
2017-03-05 09:41:54 +00:00
|
|
|
assert setup.setup_component(self.hass, zone.DOMAIN, {
|
2016-01-22 07:53:57 +00:00
|
|
|
'zone': info
|
|
|
|
})
|
|
|
|
|
|
|
|
state = self.hass.states.get('zone.test_zone')
|
|
|
|
assert info['name'] == state.name
|
|
|
|
assert info['latitude'] == state.attributes['latitude']
|
|
|
|
assert info['longitude'] == state.attributes['longitude']
|
|
|
|
assert info['radius'] == state.attributes['radius']
|
|
|
|
assert info['passive'] == state.attributes['passive']
|
|
|
|
|
|
|
|
def test_active_zone_skips_passive_zones(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test active and passive zones."""
|
2017-03-05 09:41:54 +00:00
|
|
|
assert setup.setup_component(self.hass, zone.DOMAIN, {
|
2016-01-22 07:53:57 +00:00
|
|
|
'zone': [
|
|
|
|
{
|
|
|
|
'name': 'Passive Zone',
|
|
|
|
'latitude': 32.880600,
|
|
|
|
'longitude': -117.237561,
|
|
|
|
'radius': 250,
|
|
|
|
'passive': True
|
|
|
|
},
|
|
|
|
]
|
|
|
|
})
|
2017-03-01 04:33:19 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-22 07:53:57 +00:00
|
|
|
active = zone.active_zone(self.hass, 32.880600, -117.237561)
|
|
|
|
assert active is None
|
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
def test_active_zone_skips_passive_zones_2(self):
|
|
|
|
"""Test active and passive zones."""
|
2017-03-05 09:41:54 +00:00
|
|
|
assert setup.setup_component(self.hass, zone.DOMAIN, {
|
2016-01-22 07:53:57 +00:00
|
|
|
'zone': [
|
|
|
|
{
|
|
|
|
'name': 'Active Zone',
|
|
|
|
'latitude': 32.880800,
|
|
|
|
'longitude': -117.237561,
|
|
|
|
'radius': 500,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
})
|
2017-03-01 04:33:19 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-22 07:53:57 +00:00
|
|
|
active = zone.active_zone(self.hass, 32.880700, -117.237561)
|
|
|
|
assert 'zone.active_zone' == active.entity_id
|
|
|
|
|
|
|
|
def test_active_zone_prefers_smaller_zone_if_same_distance(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test zone size preferences."""
|
2016-01-22 07:53:57 +00:00
|
|
|
latitude = 32.880600
|
|
|
|
longitude = -117.237561
|
2017-03-05 09:41:54 +00:00
|
|
|
assert setup.setup_component(self.hass, zone.DOMAIN, {
|
2016-01-22 07:53:57 +00:00
|
|
|
'zone': [
|
|
|
|
{
|
|
|
|
'name': 'Small Zone',
|
|
|
|
'latitude': latitude,
|
|
|
|
'longitude': longitude,
|
|
|
|
'radius': 250,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'Big Zone',
|
|
|
|
'latitude': latitude,
|
|
|
|
'longitude': longitude,
|
|
|
|
'radius': 500,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
active = zone.active_zone(self.hass, latitude, longitude)
|
|
|
|
assert 'zone.small_zone' == active.entity_id
|
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
def test_active_zone_prefers_smaller_zone_if_same_distance_2(self):
|
|
|
|
"""Test zone size preferences."""
|
|
|
|
latitude = 32.880600
|
|
|
|
longitude = -117.237561
|
2017-03-05 09:41:54 +00:00
|
|
|
assert setup.setup_component(self.hass, zone.DOMAIN, {
|
2016-01-22 07:53:57 +00:00
|
|
|
'zone': [
|
|
|
|
{
|
|
|
|
'name': 'Smallest Zone',
|
|
|
|
'latitude': latitude,
|
|
|
|
'longitude': longitude,
|
|
|
|
'radius': 50,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
active = zone.active_zone(self.hass, latitude, longitude)
|
|
|
|
assert 'zone.smallest_zone' == active.entity_id
|
|
|
|
|
|
|
|
def test_in_zone_works_for_passive_zones(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test working in passive zones."""
|
2016-01-22 07:53:57 +00:00
|
|
|
latitude = 32.880600
|
|
|
|
longitude = -117.237561
|
2017-03-05 09:41:54 +00:00
|
|
|
assert setup.setup_component(self.hass, zone.DOMAIN, {
|
2016-01-22 07:53:57 +00:00
|
|
|
'zone': [
|
|
|
|
{
|
|
|
|
'name': 'Passive Zone',
|
|
|
|
'latitude': latitude,
|
|
|
|
'longitude': longitude,
|
|
|
|
'radius': 250,
|
|
|
|
'passive': True
|
|
|
|
},
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
assert zone.in_zone(self.hass.states.get('zone.passive_zone'),
|
|
|
|
latitude, longitude)
|