hass-core/tests/components/zone/test_init.py

246 lines
8.5 KiB
Python
Raw Normal View History

2016-03-09 10:25:50 +01:00
"""Test zone component."""
2016-01-21 23:53:57 -08:00
import unittest
from unittest.mock import Mock
2016-01-21 23:53:57 -08:00
from homeassistant import setup
2016-01-21 23:53:57 -08:00
from homeassistant.components import zone
from tests.common import MockConfigEntry, get_test_home_assistant
async def test_setup_entry_successful(hass):
"""Test setup entry is successful."""
entry = Mock()
entry.data = {
2019-07-31 12:25:30 -07:00
zone.CONF_NAME: "Test Zone",
zone.CONF_LATITUDE: 1.1,
zone.CONF_LONGITUDE: -2.2,
2019-07-31 12:25:30 -07:00
zone.CONF_RADIUS: True,
}
hass.data[zone.DOMAIN] = {}
assert await zone.async_setup_entry(hass, entry) is True
2019-07-31 12:25:30 -07:00
assert "test_zone" in hass.data[zone.DOMAIN]
async def test_unload_entry_successful(hass):
"""Test unload entry is successful."""
entry = Mock()
entry.data = {
2019-07-31 12:25:30 -07:00
zone.CONF_NAME: "Test Zone",
zone.CONF_LATITUDE: 1.1,
2019-07-31 12:25:30 -07:00
zone.CONF_LONGITUDE: -2.2,
}
hass.data[zone.DOMAIN] = {}
assert await zone.async_setup_entry(hass, entry) is True
assert await zone.async_unload_entry(hass, entry) is True
assert not hass.data[zone.DOMAIN]
2016-01-21 23:53:57 -08:00
2016-01-24 14:13:39 -08:00
class TestComponentZone(unittest.TestCase):
2016-03-09 10:25:50 +01:00
"""Test the zone component."""
2016-01-21 23:53:57 -08:00
def setUp(self): # pylint: disable=invalid-name
2018-08-19 22:29:08 +02:00
"""Set up things to be run when tests are started."""
2016-01-21 23:53:57 -08:00
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
2016-03-09 10:25:50 +01:00
"""Stop down everything that was started."""
2016-01-21 23:53:57 -08:00
self.hass.stop()
2016-09-24 00:04:03 -07:00
def test_setup_no_zones_still_adds_home_zone(self):
"""Test if no config is passed in we still get the home zone."""
2019-07-31 12:25:30 -07:00
assert setup.setup_component(self.hass, zone.DOMAIN, {"zone": None})
assert len(self.hass.states.entity_ids("zone")) == 1
state = self.hass.states.get("zone.home")
2016-09-24 00:04:03 -07:00
assert self.hass.config.location_name == state.name
2019-07-31 12:25:30 -07:00
assert self.hass.config.latitude == state.attributes["latitude"]
assert self.hass.config.longitude == state.attributes["longitude"]
assert not state.attributes.get("passive", False)
2016-09-24 00:04:03 -07:00
2016-01-21 23:53:57 -08:00
def test_setup(self):
"""Test a successful setup."""
2016-01-21 23:53:57 -08:00
info = {
2019-07-31 12:25:30 -07:00
"name": "Test Zone",
"latitude": 32.880837,
"longitude": -117.237561,
"radius": 250,
"passive": True,
2016-01-21 23:53:57 -08:00
}
2019-07-31 12:25:30 -07:00
assert setup.setup_component(self.hass, zone.DOMAIN, {"zone": info})
2016-01-21 23:53:57 -08:00
2019-07-31 12:25:30 -07:00
assert len(self.hass.states.entity_ids("zone")) == 2
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_setup_zone_skips_home_zone(self):
"""Test that zone named Home should override hass home zone."""
2019-07-31 12:25:30 -07:00
info = {"name": "Home", "latitude": 1.1, "longitude": -2.2}
assert setup.setup_component(self.hass, zone.DOMAIN, {"zone": info})
2019-07-31 12:25:30 -07:00
assert len(self.hass.states.entity_ids("zone")) == 1
state = self.hass.states.get("zone.home")
assert info["name"] == state.name
def test_setup_name_can_be_same_on_multiple_zones(self):
"""Test that zone named Home should override hass home zone."""
2019-07-31 12:25:30 -07:00
info = {"name": "Test Zone", "latitude": 1.1, "longitude": -2.2}
assert setup.setup_component(self.hass, zone.DOMAIN, {"zone": [info, info]})
assert len(self.hass.states.entity_ids("zone")) == 3
def test_setup_registered_zone_skips_home_zone(self):
"""Test that config entry named home should override hass home zone."""
2019-07-31 12:25:30 -07:00
entry = MockConfigEntry(domain=zone.DOMAIN, data={zone.CONF_NAME: "home"})
entry.add_to_hass(self.hass)
2019-07-31 12:25:30 -07:00
assert setup.setup_component(self.hass, zone.DOMAIN, {"zone": None})
assert len(self.hass.states.entity_ids("zone")) == 0
def test_setup_registered_zone_skips_configured_zone(self):
"""Test if config entry will override configured zone."""
2019-07-31 12:25:30 -07:00
entry = MockConfigEntry(domain=zone.DOMAIN, data={zone.CONF_NAME: "Test Zone"})
entry.add_to_hass(self.hass)
2019-07-31 12:25:30 -07:00
info = {"name": "Test Zone", "latitude": 1.1, "longitude": -2.2}
assert setup.setup_component(self.hass, zone.DOMAIN, {"zone": info})
2019-07-31 12:25:30 -07:00
assert len(self.hass.states.entity_ids("zone")) == 1
state = self.hass.states.get("zone.test_zone")
assert not state
2016-01-21 23:53:57 -08:00
def test_active_zone_skips_passive_zones(self):
2016-03-09 10:25:50 +01:00
"""Test active and passive zones."""
2019-07-31 12:25:30 -07:00
assert setup.setup_component(
self.hass,
zone.DOMAIN,
{
"zone": [
{
"name": "Passive Zone",
"latitude": 32.880600,
"longitude": -117.237561,
"radius": 250,
"passive": True,
}
]
},
)
self.hass.block_till_done()
active = zone.async_active_zone(self.hass, 32.880600, -117.237561)
2016-01-21 23:53:57 -08:00
assert active is None
def test_active_zone_skips_passive_zones_2(self):
"""Test active and passive zones."""
2019-07-31 12:25:30 -07:00
assert setup.setup_component(
self.hass,
zone.DOMAIN,
{
"zone": [
{
"name": "Active Zone",
"latitude": 32.880800,
"longitude": -117.237561,
"radius": 500,
}
]
},
)
self.hass.block_till_done()
active = zone.async_active_zone(self.hass, 32.880700, -117.237561)
2019-07-31 12:25:30 -07:00
assert "zone.active_zone" == active.entity_id
2016-01-21 23:53:57 -08:00
def test_active_zone_prefers_smaller_zone_if_same_distance(self):
2016-03-09 10:25:50 +01:00
"""Test zone size preferences."""
2016-01-21 23:53:57 -08:00
latitude = 32.880600
longitude = -117.237561
2019-07-31 12:25:30 -07:00
assert setup.setup_component(
self.hass,
zone.DOMAIN,
{
"zone": [
{
"name": "Small Zone",
"latitude": latitude,
"longitude": longitude,
"radius": 250,
},
{
"name": "Big Zone",
"latitude": latitude,
"longitude": longitude,
"radius": 500,
},
]
},
)
2016-01-21 23:53:57 -08:00
active = zone.async_active_zone(self.hass, latitude, longitude)
2019-07-31 12:25:30 -07:00
assert "zone.small_zone" == active.entity_id
2016-01-21 23:53:57 -08:00
def test_active_zone_prefers_smaller_zone_if_same_distance_2(self):
"""Test zone size preferences."""
latitude = 32.880600
longitude = -117.237561
2019-07-31 12:25:30 -07:00
assert setup.setup_component(
self.hass,
zone.DOMAIN,
{
"zone": [
{
"name": "Smallest Zone",
"latitude": latitude,
"longitude": longitude,
"radius": 50,
}
]
},
)
2016-01-21 23:53:57 -08:00
active = zone.async_active_zone(self.hass, latitude, longitude)
2019-07-31 12:25:30 -07:00
assert "zone.smallest_zone" == active.entity_id
2016-01-21 23:53:57 -08:00
def test_in_zone_works_for_passive_zones(self):
2016-03-09 10:25:50 +01:00
"""Test working in passive zones."""
2016-01-21 23:53:57 -08:00
latitude = 32.880600
longitude = -117.237561
2019-07-31 12:25:30 -07:00
assert setup.setup_component(
self.hass,
zone.DOMAIN,
{
"zone": [
{
"name": "Passive Zone",
"latitude": latitude,
"longitude": longitude,
"radius": 250,
"passive": True,
}
]
},
)
assert zone.zone.in_zone(
self.hass.states.get("zone.passive_zone"), latitude, longitude
)
async def test_core_config_update(hass):
"""Test updating core config will update home zone."""
2019-07-31 12:25:30 -07:00
assert await setup.async_setup_component(hass, "zone", {})
2019-07-31 12:25:30 -07:00
home = hass.states.get("zone.home")
await hass.config.async_update(
2019-07-31 12:25:30 -07:00
location_name="Updated Name", latitude=10, longitude=20
)
await hass.async_block_till_done()
2019-07-31 12:25:30 -07:00
home_updated = hass.states.get("zone.home")
assert home is not home_updated
2019-07-31 12:25:30 -07:00
assert home_updated.name == "Updated Name"
assert home_updated.attributes["latitude"] == 10
assert home_updated.attributes["longitude"] == 20