2018-10-22 18:01:01 +00:00
|
|
|
"""The tests for the geo location trigger."""
|
2018-10-26 09:31:14 +00:00
|
|
|
import pytest
|
2018-10-22 18:01:01 +00:00
|
|
|
|
|
|
|
from homeassistant.components import automation, zone
|
2018-10-26 09:31:14 +00:00
|
|
|
from homeassistant.core import Context
|
|
|
|
from homeassistant.setup import async_setup_component
|
2018-10-22 18:01:01 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
from tests.common import mock_component
|
2018-10-22 18:01:01 +00:00
|
|
|
from tests.components.automation import common
|
2018-10-26 09:31:14 +00:00
|
|
|
from tests.common import async_mock_service
|
2018-10-22 18:01:01 +00:00
|
|
|
|
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def calls(hass):
|
|
|
|
"""Track calls to a mock serivce."""
|
|
|
|
return async_mock_service(hass, 'test', 'automation')
|
2018-10-22 18:01:01 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_comp(hass):
|
|
|
|
"""Initialize components."""
|
|
|
|
mock_component(hass, 'group')
|
|
|
|
hass.loop.run_until_complete(async_setup_component(hass, zone.DOMAIN, {
|
2018-10-22 18:01:01 +00:00
|
|
|
'zone': {
|
|
|
|
'name': 'test',
|
|
|
|
'latitude': 32.880837,
|
|
|
|
'longitude': -117.237561,
|
|
|
|
'radius': 250,
|
|
|
|
}
|
2018-10-26 09:31:14 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_zone_enter(hass, calls):
|
|
|
|
"""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.%s }}' % '}} - {{ trigger.'.join((
|
|
|
|
'platform', 'entity_id',
|
|
|
|
'from_state.state', 'to_state.state',
|
|
|
|
'zone.name'))
|
2018-10-22 18:01:01 +00:00
|
|
|
},
|
2018-10-26 09:31:14 +00:00
|
|
|
|
2018-10-22 18:01:01 +00:00
|
|
|
}
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
hass.states.async_set('geo_location.entity', 'hello', {
|
|
|
|
'latitude': 32.880586,
|
|
|
|
'longitude': -117.237564
|
|
|
|
}, context=context)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert 1 == len(calls)
|
|
|
|
assert calls[0].context is context
|
|
|
|
assert 'geo_location - geo_location.entity - hello - hello - test' == \
|
|
|
|
calls[0].data['some']
|
|
|
|
|
|
|
|
# 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 common.async_turn_off(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
hass.states.async_set('geo_location.entity', 'hello', {
|
|
|
|
'latitude': 32.880586,
|
|
|
|
'longitude': -117.237564
|
|
|
|
})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert 1 == len(calls)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_not_fires_for_enter_on_zone_leave(hass, calls):
|
|
|
|
"""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',
|
2018-10-22 18:01:01 +00:00
|
|
|
}
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
hass.states.async_set('geo_location.entity', 'hello', {
|
|
|
|
'latitude': 32.881011,
|
|
|
|
'longitude': -117.234758
|
|
|
|
})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert 0 == len(calls)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_zone_leave(hass, calls):
|
|
|
|
"""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',
|
2018-10-22 18:01:01 +00:00
|
|
|
}
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
hass.states.async_set('geo_location.entity', 'hello', {
|
|
|
|
'latitude': 32.881011,
|
|
|
|
'longitude': -117.234758,
|
|
|
|
'source': 'test_source'
|
|
|
|
})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert 1 == len(calls)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_not_fires_for_leave_on_zone_enter(hass, calls):
|
|
|
|
"""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',
|
2018-10-22 18:01:01 +00:00
|
|
|
}
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
hass.states.async_set('geo_location.entity', 'hello', {
|
|
|
|
'latitude': 32.880586,
|
|
|
|
'longitude': -117.237564
|
|
|
|
})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert 0 == len(calls)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_zone_appear(hass, calls):
|
|
|
|
"""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.%s }}' % '}} - {{ trigger.'.join((
|
|
|
|
'platform', 'entity_id',
|
|
|
|
'from_state.state', 'to_state.state',
|
|
|
|
'zone.name'))
|
2018-10-22 18:01:01 +00:00
|
|
|
},
|
2018-10-26 09:31:14 +00:00
|
|
|
|
2018-10-22 18:01:01 +00:00
|
|
|
}
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
# 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 1 == len(calls)
|
|
|
|
assert calls[0].context is context
|
|
|
|
assert 'geo_location - geo_location.entity - - hello - test' == \
|
|
|
|
calls[0].data['some']
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_zone_disappear(hass, calls):
|
|
|
|
"""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.%s }}' % '}} - {{ trigger.'.join((
|
|
|
|
'platform', 'entity_id',
|
|
|
|
'from_state.state', 'to_state.state',
|
|
|
|
'zone.name'))
|
2018-10-22 18:01:01 +00:00
|
|
|
},
|
2018-10-26 09:31:14 +00:00
|
|
|
|
2018-10-22 18:01:01 +00:00
|
|
|
}
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
|
|
|
})
|
2018-10-22 18:01:01 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
# Entity disappears from zone without new coordinates outside the zone.
|
|
|
|
hass.states.async_remove('geo_location.entity')
|
|
|
|
await hass.async_block_till_done()
|
2018-10-22 18:01:01 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
assert 1 == len(calls)
|
|
|
|
assert 'geo_location - geo_location.entity - hello - - test' == \
|
|
|
|
calls[0].data['some']
|