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
@@ -0,0 +1,84 @@
|
||||
"""Distance util functions."""
|
||||
|
||||
import logging
|
||||
from numbers import Number
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
KILOMETERS_SYMBOL = 'km'
|
||||
METERS_SYMBOL = 'm'
|
||||
FEET_SYMBOL = 'ft'
|
||||
MILES_SYMBOL = 'mi'
|
||||
|
||||
VALID_UNITS = [
|
||||
KILOMETERS_SYMBOL,
|
||||
METERS_SYMBOL,
|
||||
FEET_SYMBOL,
|
||||
MILES_SYMBOL,
|
||||
]
|
||||
|
||||
|
||||
def convert(value, unit_1, unit_2):
|
||||
"""Convert one unit of measurement to another."""
|
||||
if not isinstance(value, Number):
|
||||
raise TypeError(str(value) + ' is not of numeric type')
|
||||
|
||||
if unit_1 == unit_2:
|
||||
return value
|
||||
|
||||
if unit_1 not in VALID_UNITS:
|
||||
_LOGGER.error('Unknown unit of measure: ' + str(unit_1))
|
||||
raise ValueError('Unknown unit of measure: ' + str(unit_1))
|
||||
elif unit_2 not in VALID_UNITS:
|
||||
_LOGGER.error('Unknown unit of measure: ' + str(unit_2))
|
||||
raise ValueError('Unknown unit of measure: ' + str(unit_2))
|
||||
|
||||
meters = value
|
||||
|
||||
if unit_1 == MILES_SYMBOL:
|
||||
meters = __miles_to_meters(value)
|
||||
elif unit_1 == FEET_SYMBOL:
|
||||
meters = __feet_to_meters(value)
|
||||
elif unit_1 == KILOMETERS_SYMBOL:
|
||||
meters = __kilometers_to_meters(value)
|
||||
|
||||
result = meters
|
||||
|
||||
if unit_2 == MILES_SYMBOL:
|
||||
result = __meters_to_miles(meters)
|
||||
elif unit_2 == FEET_SYMBOL:
|
||||
result = __meters_to_feet(meters)
|
||||
elif unit_2 == KILOMETERS_SYMBOL:
|
||||
result = __meters_to_kilometers(meters)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def __miles_to_meters(miles):
|
||||
"""Convert miles to meters."""
|
||||
return miles * 1609.344
|
||||
|
||||
|
||||
def __feet_to_meters(feet):
|
||||
"""Convert feet to meters."""
|
||||
return feet * 0.3048
|
||||
|
||||
|
||||
def __kilometers_to_meters(kilometers):
|
||||
"""Convert kilometers to meters."""
|
||||
return kilometers * 1000
|
||||
|
||||
|
||||
def __meters_to_miles(meters):
|
||||
"""Convert meters to miles."""
|
||||
return meters * 0.000621371
|
||||
|
||||
|
||||
def __meters_to_feet(meters):
|
||||
"""Convert meters to feet."""
|
||||
return meters * 3.28084
|
||||
|
||||
|
||||
def __meters_to_kilometers(meters):
|
||||
"""Convert meters to kilometers."""
|
||||
return meters * 0.001
|
||||
Reference in new issue
Block a user