"""The tests for the location condition.""" import pytest from homeassistant.components.zone import condition as zone_condition from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConditionError from homeassistant.helpers import condition, config_validation as cv async def test_zone_raises(hass: HomeAssistant) -> None: """Test that zone raises ConditionError on errors.""" config = { "condition": "zone", "entity_id": "device_tracker.cat", "zone": "zone.home", } config = cv.CONDITION_SCHEMA(config) config = await condition.async_validate_condition_config(hass, config) test = await condition.async_from_config(hass, config) with pytest.raises(ConditionError, match="no zone"): zone_condition.zone(hass, zone_ent=None, entity="sensor.any") with pytest.raises(ConditionError, match="unknown zone"): test(hass) hass.states.async_set( "zone.home", "zoning", {"name": "home", "latitude": 2.1, "longitude": 1.1, "radius": 10}, ) with pytest.raises(ConditionError, match="no entity"): zone_condition.zone(hass, zone_ent="zone.home", entity=None) with pytest.raises(ConditionError, match="unknown entity"): test(hass) hass.states.async_set( "device_tracker.cat", "home", {"friendly_name": "cat"}, ) with pytest.raises(ConditionError, match="latitude"): test(hass) hass.states.async_set( "device_tracker.cat", "home", {"friendly_name": "cat", "latitude": 2.1}, ) with pytest.raises(ConditionError, match="longitude"): test(hass) hass.states.async_set( "device_tracker.cat", "home", {"friendly_name": "cat", "latitude": 2.1, "longitude": 1.1}, ) # All okay, now test multiple failed conditions assert test(hass) config = { "condition": "zone", "entity_id": ["device_tracker.cat", "device_tracker.dog"], "zone": ["zone.home", "zone.work"], } config = cv.CONDITION_SCHEMA(config) config = await condition.async_validate_condition_config(hass, config) test = await condition.async_from_config(hass, config) with pytest.raises(ConditionError, match="dog"): test(hass) with pytest.raises(ConditionError, match="work"): test(hass) hass.states.async_set( "zone.work", "zoning", {"name": "work", "latitude": 20, "longitude": 10, "radius": 25000}, ) hass.states.async_set( "device_tracker.dog", "work", {"friendly_name": "dog", "latitude": 20.1, "longitude": 10.1}, ) assert test(hass) async def test_zone_multiple_entities(hass: HomeAssistant) -> None: """Test with multiple entities in condition.""" config = { "condition": "and", "conditions": [ { "alias": "Zone Condition", "condition": "zone", "entity_id": ["device_tracker.person_1", "device_tracker.person_2"], "zone": "zone.home", }, ], } config = cv.CONDITION_SCHEMA(config) config = await condition.async_validate_condition_config(hass, config) test = await condition.async_from_config(hass, config) hass.states.async_set( "zone.home", "zoning", {"name": "home", "latitude": 2.1, "longitude": 1.1, "radius": 10}, ) hass.states.async_set( "device_tracker.person_1", "home", {"friendly_name": "person_1", "latitude": 2.1, "longitude": 1.1}, ) hass.states.async_set( "device_tracker.person_2", "home", {"friendly_name": "person_2", "latitude": 2.1, "longitude": 1.1}, ) assert test(hass) hass.states.async_set( "device_tracker.person_1", "home", {"friendly_name": "person_1", "latitude": 20.1, "longitude": 10.1}, ) hass.states.async_set( "device_tracker.person_2", "home", {"friendly_name": "person_2", "latitude": 2.1, "longitude": 1.1}, ) assert not test(hass) hass.states.async_set( "device_tracker.person_1", "home", {"friendly_name": "person_1", "latitude": 2.1, "longitude": 1.1}, ) hass.states.async_set( "device_tracker.person_2", "home", {"friendly_name": "person_2", "latitude": 20.1, "longitude": 10.1}, ) assert not test(hass) async def test_multiple_zones(hass: HomeAssistant) -> None: """Test with multiple entities in condition.""" config = { "condition": "and", "conditions": [ { "condition": "zone", "entity_id": "device_tracker.person", "zone": ["zone.home", "zone.work"], }, ], } config = cv.CONDITION_SCHEMA(config) config = await condition.async_validate_condition_config(hass, config) test = await condition.async_from_config(hass, config) hass.states.async_set( "zone.home", "zoning", {"name": "home", "latitude": 2.1, "longitude": 1.1, "radius": 10}, ) hass.states.async_set( "zone.work", "zoning", {"name": "work", "latitude": 20.1, "longitude": 10.1, "radius": 10}, ) hass.states.async_set( "device_tracker.person", "home", {"friendly_name": "person", "latitude": 2.1, "longitude": 1.1}, ) assert test(hass) hass.states.async_set( "device_tracker.person", "home", {"friendly_name": "person", "latitude": 20.1, "longitude": 10.1}, ) assert test(hass) hass.states.async_set( "device_tracker.person", "home", {"friendly_name": "person", "latitude": 50.1, "longitude": 20.1}, ) assert not test(hass)