2016-02-21 19:13:40 +00:00
|
|
|
"""Tests Home Assistant location helpers."""
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
|
|
|
|
from homeassistant.core import State
|
|
|
|
from homeassistant.helpers import location
|
|
|
|
|
|
|
|
|
|
|
|
class TestHelpersLocation(unittest.TestCase):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up the tests."""
|
2016-03-09 09:25:50 +00:00
|
|
|
|
2016-02-21 19:13:40 +00:00
|
|
|
def test_has_location_with_invalid_states(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up the tests."""
|
2016-02-21 19:13:40 +00:00
|
|
|
for state in (None, 1, "hello", object):
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not location.has_location(state)
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_has_location_with_states_with_invalid_locations(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up the tests."""
|
2016-02-21 19:13:40 +00:00
|
|
|
state = State('hello.world', 'invalid', {
|
|
|
|
ATTR_LATITUDE: 'no number',
|
|
|
|
ATTR_LONGITUDE: 123.12
|
|
|
|
})
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not location.has_location(state)
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_has_location_with_states_with_valid_location(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up the tests."""
|
2016-02-21 19:13:40 +00:00
|
|
|
state = State('hello.world', 'invalid', {
|
|
|
|
ATTR_LATITUDE: 123.12,
|
|
|
|
ATTR_LONGITUDE: 123.12
|
|
|
|
})
|
2018-10-24 10:10:05 +00:00
|
|
|
assert location.has_location(state)
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_with_no_states_with_location(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up the tests."""
|
2016-02-21 19:13:40 +00:00
|
|
|
state = State('light.test', 'on')
|
|
|
|
state2 = State('light.test', 'on', {
|
|
|
|
ATTR_LATITUDE: 'invalid',
|
|
|
|
ATTR_LONGITUDE: 123.45,
|
|
|
|
})
|
|
|
|
state3 = State('light.test', 'on', {
|
|
|
|
ATTR_LONGITUDE: 123.45,
|
|
|
|
})
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert \
|
|
|
|
location.closest(123.45, 123.45, [state, state2, state3]) is None
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_returns_closest(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test ."""
|
2016-02-21 19:13:40 +00:00
|
|
|
state = State('light.test', 'on', {
|
|
|
|
ATTR_LATITUDE: 124.45,
|
|
|
|
ATTR_LONGITUDE: 124.45,
|
|
|
|
})
|
|
|
|
state2 = State('light.test', 'on', {
|
|
|
|
ATTR_LATITUDE: 125.45,
|
|
|
|
ATTR_LONGITUDE: 125.45,
|
|
|
|
})
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert state == location.closest(123.45, 123.45, [state, state2])
|