Proximity unit of measure (#2659)
* Allow multiple proximities * Distance conversion * Add unit of measurement and conversion to proximity * Shorten attribute name * Fix get unit of measurement * Fix the km <-> m conversion * Add type check and errors * first path unit test around distance utility * Fix numeric type check * Fix conversion type-os * Actually set the exception thrown flag * Test for exact conversion * More descriptive variable names * Update method invocation to match change in method name * Missed a couple variables * Line continuation * Fix linting too many return issue * Break out proximity setup for list of proximity and for single proximity device * Pass hass to setup function * Check if setup succeeded for each proximity component * Change variable name * Break out branches in convert to avoid too many branches linting error * Remove disable lint line * Variables for default properties * Combine logic * Test loading multiple proximities for 100% code coverage on proximity component * Unit test to reach 100% Fail to configure proximities missing devices * Fail first before processing * Combine return statements * lstrip = bad Teagan * Utilize string formating instead of concatenation * Fix variable reference * Typeo * Clean up conversion to reduce complexity * Update unit tests to match code changes on distance util * Test non numeric value * Private methods, value type has already been checked.
This commit is contained in:
1 parent
de7e27c92c
commit
122581da7f
4 files changed
+269
-26
No files matched your search
@@ -18,11 +18,73 @@ class TestProximity:
|
||||
'longitude': 1.1,
|
||||
'radius': 10
|
||||
})
|
||||
self.hass.states.set(
|
||||
'zone.work', 'zoning',
|
||||
{
|
||||
'name': 'work',
|
||||
'latitude': 2.3,
|
||||
'longitude': 1.3,
|
||||
'radius': 10
|
||||
})
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_proximities(self):
|
||||
"""Test a list of proximities."""
|
||||
assert proximity.setup(self.hass, {
|
||||
'proximity': [{
|
||||
'zone': 'home',
|
||||
'ignored_zones': {
|
||||
'work'
|
||||
},
|
||||
'devices': {
|
||||
'device_tracker.test1',
|
||||
'device_tracker.test2'
|
||||
},
|
||||
'tolerance': '1'
|
||||
}, {
|
||||
'zone': 'work',
|
||||
'devices': {
|
||||
'device_tracker.test1'
|
||||
},
|
||||
'tolerance': '1'
|
||||
}]
|
||||
})
|
||||
|
||||
proximities = ['home', 'work']
|
||||
|
||||
for prox in proximities:
|
||||
state = self.hass.states.get('proximity.' + prox)
|
||||
assert state.state == 'not set'
|
||||
assert state.attributes.get('nearest') == 'not set'
|
||||
assert state.attributes.get('dir_of_travel') == 'not set'
|
||||
|
||||
self.hass.states.set('proximity.' + prox, '0')
|
||||
self.hass.pool.block_till_done()
|
||||
state = self.hass.states.get('proximity.' + prox)
|
||||
assert state.state == '0'
|
||||
|
||||
def test_proximities_missing_devices(self):
|
||||
"""Test a list of proximities with one missing devices."""
|
||||
assert not proximity.setup(self.hass, {
|
||||
'proximity': [{
|
||||
'zone': 'home',
|
||||
'ignored_zones': {
|
||||
'work'
|
||||
},
|
||||
'devices': {
|
||||
'device_tracker.test1',
|
||||
'device_tracker.test2'
|
||||
},
|
||||
'tolerance': '1'
|
||||
}, {
|
||||
'zone': 'work',
|
||||
'tolerance': '1'
|
||||
}]
|
||||
})
|
||||
|
||||
def test_proximity(self):
|
||||
"""Test the proximity."""
|
||||
assert proximity.setup(self.hass, {
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
"""Test homeasssitant distance utility functions."""
|
||||
|
||||
import unittest
|
||||
import homeassistant.util.distance as distance_util
|
||||
|
||||
KILOMETERS = distance_util.KILOMETERS_SYMBOL
|
||||
METERS = distance_util.METERS_SYMBOL
|
||||
FEET = distance_util.FEET_SYMBOL
|
||||
MILES = distance_util.MILES_SYMBOL
|
||||
|
||||
INVALID_SYMBOL = 'bob'
|
||||
VALID_SYMBOL = KILOMETERS
|
||||
|
||||
|
||||
class TestDistanceUtil(unittest.TestCase):
|
||||
"""Test the distance utility functions."""
|
||||
|
||||
def test_convert_same_unit(self):
|
||||
"""Test conversion from any unit to same unit."""
|
||||
self.assertEqual(5, distance_util.convert(5, KILOMETERS, KILOMETERS))
|
||||
self.assertEqual(2, distance_util.convert(2, METERS, METERS))
|
||||
self.assertEqual(10, distance_util.convert(10, MILES, MILES))
|
||||
self.assertEqual(9, distance_util.convert(9, FEET, FEET))
|
||||
|
||||
def test_convert_invalid_unit(self):
|
||||
"""Test exception is thrown for invalid units."""
|
||||
with self.assertRaises(ValueError):
|
||||
distance_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
distance_util.convert(5, VALID_SYMBOL, INVALID_SYMBOL)
|
||||
|
||||
def test_convert_nonnumeric_value(self):
|
||||
"""Test exception is thrown for nonnumeric type."""
|
||||
with self.assertRaises(TypeError):
|
||||
distance_util.convert('a', KILOMETERS, METERS)
|
||||
|
||||
def test_convert_from_miles(self):
|
||||
"""Test conversion from miles to other units."""
|
||||
miles = 5
|
||||
self.assertEqual(distance_util.convert(miles, MILES, KILOMETERS),
|
||||
8.04672)
|
||||
self.assertEqual(distance_util.convert(miles, MILES, METERS), 8046.72)
|
||||
self.assertEqual(distance_util.convert(miles, MILES, FEET),
|
||||
26400.0008448)
|
||||
|
||||
def test_convert_from_feet(self):
|
||||
"""Test conversion from feet to other units."""
|
||||
feet = 5000
|
||||
self.assertEqual(distance_util.convert(feet, FEET, KILOMETERS), 1.524)
|
||||
self.assertEqual(distance_util.convert(feet, FEET, METERS), 1524)
|
||||
self.assertEqual(distance_util.convert(feet, FEET, MILES),
|
||||
0.9469694040000001)
|
||||
|
||||
def test_convert_from_kilometers(self):
|
||||
"""Test conversion from kilometers to other units."""
|
||||
km = 5
|
||||
self.assertEqual(distance_util.convert(km, KILOMETERS, FEET), 16404.2)
|
||||
self.assertEqual(distance_util.convert(km, KILOMETERS, METERS), 5000)
|
||||
self.assertEqual(distance_util.convert(km, KILOMETERS, MILES),
|
||||
3.106855)
|
||||
|
||||
def test_convert_from_meters(self):
|
||||
"""Test conversion from meters to other units."""
|
||||
m = 5000
|
||||
self.assertEqual(distance_util.convert(m, METERS, FEET), 16404.2)
|
||||
self.assertEqual(distance_util.convert(m, METERS, KILOMETERS), 5)
|
||||
self.assertEqual(distance_util.convert(m, METERS, MILES), 3.106855)
|
||||
Reference in new issue
Block a user