mirror of https://github.com/home-assistant/core
481 lines
14 KiB
Python
481 lines
14 KiB
Python
"""The tests for the geolocation trigger."""
|
|
|
|
import logging
|
|
|
|
import pytest
|
|
|
|
from homeassistant.components import automation, zone
|
|
from homeassistant.const import (
|
|
ATTR_ENTITY_ID,
|
|
ENTITY_MATCH_ALL,
|
|
SERVICE_TURN_OFF,
|
|
STATE_UNAVAILABLE,
|
|
)
|
|
from homeassistant.core import Context, HomeAssistant, ServiceCall
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
from tests.common import async_mock_service, mock_component
|
|
|
|
|
|
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
|
def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
|
|
"""Stub copying the blueprints to the config folder."""
|
|
|
|
|
|
@pytest.fixture
|
|
def calls(hass: HomeAssistant) -> list[ServiceCall]:
|
|
"""Track calls to a mock service."""
|
|
return async_mock_service(hass, "test", "automation")
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def setup_comp(hass: HomeAssistant) -> None:
|
|
"""Initialize components."""
|
|
mock_component(hass, "group")
|
|
hass.loop.run_until_complete(
|
|
async_setup_component(
|
|
hass,
|
|
zone.DOMAIN,
|
|
{
|
|
"zone": {
|
|
"name": "test",
|
|
"latitude": 32.880837,
|
|
"longitude": -117.237561,
|
|
"radius": 250,
|
|
}
|
|
},
|
|
)
|
|
)
|
|
|
|
|
|
async def test_if_fires_on_zone_enter(
|
|
hass: HomeAssistant, service_calls: list[ServiceCall]
|
|
) -> None:
|
|
"""Test for firing on zone enter."""
|
|
context = Context()
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.881011, "longitude": -117.234758, "source": "test_source"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: {
|
|
"trigger": {
|
|
"platform": "geo_location",
|
|
"source": "test_source",
|
|
"zone": "zone.test",
|
|
"event": "enter",
|
|
},
|
|
"action": {
|
|
"service": "test.automation",
|
|
"data_template": {
|
|
"some": (
|
|
"{{ trigger.platform }}"
|
|
" - {{ trigger.entity_id }}"
|
|
" - {{ trigger.from_state.state }}"
|
|
" - {{ trigger.to_state.state }}"
|
|
" - {{ trigger.zone.name }}"
|
|
" - {{ trigger.id }}"
|
|
)
|
|
},
|
|
},
|
|
}
|
|
},
|
|
)
|
|
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.880586, "longitude": -117.237564},
|
|
context=context,
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(service_calls) == 1
|
|
assert service_calls[0].context.parent_id == context.id
|
|
assert (
|
|
service_calls[0].data["some"]
|
|
== "geo_location - geo_location.entity - hello - hello - test - 0"
|
|
)
|
|
|
|
# Set out of zone again so we can trigger call
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.881011, "longitude": -117.234758},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.services.async_call(
|
|
automation.DOMAIN,
|
|
SERVICE_TURN_OFF,
|
|
{ATTR_ENTITY_ID: ENTITY_MATCH_ALL},
|
|
blocking=True,
|
|
)
|
|
|
|
assert len(service_calls) == 2
|
|
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.880586, "longitude": -117.237564},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(service_calls) == 2
|
|
|
|
|
|
async def test_if_not_fires_for_enter_on_zone_leave(
|
|
hass: HomeAssistant, service_calls: list[ServiceCall]
|
|
) -> None:
|
|
"""Test for not firing on zone leave."""
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: {
|
|
"trigger": {
|
|
"platform": "geo_location",
|
|
"source": "test_source",
|
|
"zone": "zone.test",
|
|
"event": "enter",
|
|
},
|
|
"action": {"service": "test.automation"},
|
|
}
|
|
},
|
|
)
|
|
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.881011, "longitude": -117.234758},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(service_calls) == 0
|
|
|
|
|
|
async def test_if_fires_on_zone_leave(
|
|
hass: HomeAssistant, service_calls: list[ServiceCall]
|
|
) -> None:
|
|
"""Test for firing on zone leave."""
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: {
|
|
"trigger": {
|
|
"platform": "geo_location",
|
|
"source": "test_source",
|
|
"zone": "zone.test",
|
|
"event": "leave",
|
|
},
|
|
"action": {"service": "test.automation"},
|
|
}
|
|
},
|
|
)
|
|
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.881011, "longitude": -117.234758, "source": "test_source"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(service_calls) == 1
|
|
|
|
|
|
async def test_if_fires_on_zone_leave_2(
|
|
hass: HomeAssistant, service_calls: list[ServiceCall]
|
|
) -> None:
|
|
"""Test for firing on zone leave for unavailable entity."""
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: {
|
|
"trigger": {
|
|
"platform": "geo_location",
|
|
"source": "test_source",
|
|
"zone": "zone.test",
|
|
"event": "enter",
|
|
},
|
|
"action": {"service": "test.automation"},
|
|
}
|
|
},
|
|
)
|
|
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
STATE_UNAVAILABLE,
|
|
{"source": "test_source"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(service_calls) == 0
|
|
|
|
|
|
async def test_if_not_fires_for_leave_on_zone_enter(
|
|
hass: HomeAssistant, service_calls: list[ServiceCall]
|
|
) -> None:
|
|
"""Test for not firing on zone enter."""
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.881011, "longitude": -117.234758, "source": "test_source"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: {
|
|
"trigger": {
|
|
"platform": "geo_location",
|
|
"source": "test_source",
|
|
"zone": "zone.test",
|
|
"event": "leave",
|
|
},
|
|
"action": {"service": "test.automation"},
|
|
}
|
|
},
|
|
)
|
|
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.880586, "longitude": -117.237564},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(service_calls) == 0
|
|
|
|
|
|
async def test_if_fires_on_zone_appear(
|
|
hass: HomeAssistant, service_calls: list[ServiceCall]
|
|
) -> None:
|
|
"""Test for firing if entity appears in zone."""
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: {
|
|
"trigger": {
|
|
"platform": "geo_location",
|
|
"source": "test_source",
|
|
"zone": "zone.test",
|
|
"event": "enter",
|
|
},
|
|
"action": {
|
|
"service": "test.automation",
|
|
"data_template": {
|
|
"some": (
|
|
"{{ trigger.platform }}"
|
|
" - {{ trigger.entity_id }}"
|
|
" - {{ trigger.from_state.state }}"
|
|
" - {{ trigger.to_state.state }}"
|
|
" - {{ trigger.zone.name }}"
|
|
)
|
|
},
|
|
},
|
|
}
|
|
},
|
|
)
|
|
|
|
# Entity appears in zone without previously existing outside the zone.
|
|
context = Context()
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
|
context=context,
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(service_calls) == 1
|
|
assert service_calls[0].context.parent_id == context.id
|
|
assert (
|
|
service_calls[0].data["some"]
|
|
== "geo_location - geo_location.entity - - hello - test"
|
|
)
|
|
|
|
|
|
async def test_if_fires_on_zone_appear_2(
|
|
hass: HomeAssistant, service_calls: list[ServiceCall]
|
|
) -> None:
|
|
"""Test for firing if entity appears in zone."""
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: {
|
|
"trigger": {
|
|
"platform": "geo_location",
|
|
"source": "test_source",
|
|
"zone": "zone.test",
|
|
"event": "enter",
|
|
},
|
|
"action": {
|
|
"service": "test.automation",
|
|
"data_template": {
|
|
"some": (
|
|
"{{ trigger.platform }}"
|
|
" - {{ trigger.entity_id }}"
|
|
" - {{ trigger.from_state.state }}"
|
|
" - {{ trigger.to_state.state }}"
|
|
" - {{ trigger.zone.name }}"
|
|
)
|
|
},
|
|
},
|
|
}
|
|
},
|
|
)
|
|
|
|
# Entity appears in zone without previously existing outside the zone.
|
|
context = Context()
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"goodbye",
|
|
{"latitude": 32.881011, "longitude": -117.234758, "source": "test_source"},
|
|
context=context,
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
|
context=context,
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(service_calls) == 1
|
|
assert service_calls[0].context.parent_id == context.id
|
|
assert (
|
|
service_calls[0].data["some"]
|
|
== "geo_location - geo_location.entity - goodbye - hello - test"
|
|
)
|
|
|
|
|
|
async def test_if_fires_on_zone_disappear(
|
|
hass: HomeAssistant, service_calls: list[ServiceCall]
|
|
) -> None:
|
|
"""Test for firing if entity disappears from zone."""
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: {
|
|
"trigger": {
|
|
"platform": "geo_location",
|
|
"source": "test_source",
|
|
"zone": "zone.test",
|
|
"event": "leave",
|
|
},
|
|
"action": {
|
|
"service": "test.automation",
|
|
"data_template": {
|
|
"some": (
|
|
"{{ trigger.platform }}"
|
|
" - {{ trigger.entity_id }}"
|
|
" - {{ trigger.from_state.state }}"
|
|
" - {{ trigger.to_state.state }}"
|
|
" - {{ trigger.zone.name }}"
|
|
)
|
|
},
|
|
},
|
|
}
|
|
},
|
|
)
|
|
|
|
# Entity disappears from zone without new coordinates outside the zone.
|
|
hass.states.async_remove("geo_location.entity")
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(service_calls) == 1
|
|
assert (
|
|
service_calls[0].data["some"]
|
|
== "geo_location - geo_location.entity - hello - - test"
|
|
)
|
|
|
|
|
|
async def test_zone_undefined(
|
|
hass: HomeAssistant,
|
|
service_calls: list[ServiceCall],
|
|
caplog: pytest.LogCaptureFixture,
|
|
) -> None:
|
|
"""Test for undefined zone."""
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
caplog.set_level(logging.WARNING)
|
|
|
|
zone_does_not_exist = "zone.does_not_exist"
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: {
|
|
"trigger": {
|
|
"platform": "geo_location",
|
|
"source": "test_source",
|
|
"zone": zone_does_not_exist,
|
|
"event": "leave",
|
|
},
|
|
"action": {"service": "test.automation"},
|
|
}
|
|
},
|
|
)
|
|
|
|
hass.states.async_set(
|
|
"geo_location.entity",
|
|
"hello",
|
|
{"latitude": 32.881011, "longitude": -117.234758, "source": "test_source"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(service_calls) == 0
|
|
|
|
assert (
|
|
f"Unable to execute automation automation 0: Zone {zone_does_not_exist} not found"
|
|
in caplog.text
|
|
)
|