Add closest template helper
This commit is contained in:
parent
9f5f13644a
commit
c3bb6d32aa
4 changed files with 360 additions and 7 deletions
53
tests/helpers/test_location.py
Normal file
53
tests/helpers/test_location.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
"""Tests Home Assistant location helpers."""
|
||||
# pylint: disable=too-many-public-methods
|
||||
import unittest
|
||||
|
||||
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
|
||||
from homeassistant.core import State
|
||||
from homeassistant.helpers import location
|
||||
|
||||
|
||||
class TestHelpersLocation(unittest.TestCase):
|
||||
def test_has_location_with_invalid_states(self):
|
||||
for state in (None, 1, "hello", object):
|
||||
self.assertFalse(location.has_location(state))
|
||||
|
||||
def test_has_location_with_states_with_invalid_locations(self):
|
||||
state = State('hello.world', 'invalid', {
|
||||
ATTR_LATITUDE: 'no number',
|
||||
ATTR_LONGITUDE: 123.12
|
||||
})
|
||||
self.assertFalse(location.has_location(state))
|
||||
|
||||
def test_has_location_with_states_with_valid_location(self):
|
||||
state = State('hello.world', 'invalid', {
|
||||
ATTR_LATITUDE: 123.12,
|
||||
ATTR_LONGITUDE: 123.12
|
||||
})
|
||||
self.assertTrue(location.has_location(state))
|
||||
|
||||
def test_closest_with_no_states_with_location(self):
|
||||
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,
|
||||
})
|
||||
|
||||
self.assertIsNone(
|
||||
location.closest(123.45, 123.45, [state, state2, state3]))
|
||||
|
||||
def test_closest_returns_closest(self):
|
||||
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,
|
||||
})
|
||||
|
||||
self.assertEqual(
|
||||
state, location.closest(123.45, 123.45, [state, state2]))
|
Loading…
Add table
Add a link
Reference in a new issue