-
-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Add speed conversion function #53846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3b0866f
Add speed conversion function
rianadon 19dc098
Add test for speed utility functions
rianadon 790e887
Merge remote-tracking branch 'upstream/dev' into speed-conversion
rianadon 8bd0f52
Merge remote-tracking branch 'upstream/dev' into speed-conversion
rianadon 632bc89
Update unit system tests
rianadon 506c148
Fix incorrect unit conversions in tests
rianadon 3490c01
Fix some test errors
rianadon dacd752
Calculate speed units from smaller set of constants
rianadon 52afe1e
Fix typo in speed test
rianadon 71e1e28
Use pytest.approx for checking floating point values
rianadon 088261c
Change other instance of speeds needing to be pytest.approx
rianadon 647345a
Merge remote-tracking branch 'upstream' into speed-conversion
rianadon 03e1c6d
Revert changes to unit system
rianadon 6cd08b2
Fix oopsie in defining in/day and in/hr
rianadon 8bef4bd
Parametrize test
rianadon b59b697
Add comments describing calculations & remove duplicate test
rianadon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """Distance util functions.""" | ||
| from __future__ import annotations | ||
|
|
||
| from numbers import Number | ||
|
|
||
| from homeassistant.const import ( | ||
| SPEED, | ||
| SPEED_INCHES_PER_DAY, | ||
| SPEED_INCHES_PER_HOUR, | ||
| SPEED_KILOMETERS_PER_HOUR, | ||
| SPEED_METERS_PER_SECOND, | ||
| SPEED_MILES_PER_HOUR, | ||
| SPEED_MILLIMETERS_PER_DAY, | ||
| UNIT_NOT_RECOGNIZED_TEMPLATE, | ||
| ) | ||
|
|
||
| VALID_UNITS: tuple[str, ...] = ( | ||
| SPEED_METERS_PER_SECOND, | ||
| SPEED_KILOMETERS_PER_HOUR, | ||
| SPEED_MILES_PER_HOUR, | ||
| SPEED_MILLIMETERS_PER_DAY, | ||
| SPEED_INCHES_PER_DAY, | ||
| SPEED_INCHES_PER_HOUR, | ||
| ) | ||
|
|
||
| HRS_TO_SECS = 60 * 60 # 1 hr = 3600 seconds | ||
| KM_TO_M = 1000 # 1 km = 1000 m | ||
| KM_TO_MILE = 0.62137119 # 1 km = 0.62137119 mi | ||
| KM_TO_IN = 39370.0787 # 1 km = 39370.0787 in | ||
|
|
||
| # Units in terms of m/s | ||
| UNIT_CONVERSION: dict[str, float] = { | ||
| SPEED_METERS_PER_SECOND: 1, | ||
| SPEED_KILOMETERS_PER_HOUR: HRS_TO_SECS / KM_TO_M, | ||
| SPEED_MILES_PER_HOUR: HRS_TO_SECS * KM_TO_MILE / KM_TO_M, | ||
| SPEED_MILLIMETERS_PER_DAY: (24 * HRS_TO_SECS) * 1000, | ||
| SPEED_INCHES_PER_DAY: (24 * HRS_TO_SECS) * KM_TO_IN, | ||
| SPEED_INCHES_PER_HOUR: HRS_TO_SECS * KM_TO_IN, | ||
| } | ||
|
|
||
|
|
||
| def convert(value: float, unit_1: str, unit_2: str) -> float: | ||
| """Convert one unit of measurement to another.""" | ||
| if unit_1 not in VALID_UNITS: | ||
| raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_1, SPEED)) | ||
| if unit_2 not in VALID_UNITS: | ||
| raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit_2, SPEED)) | ||
|
|
||
| if not isinstance(value, Number): | ||
| raise TypeError(f"{value} is not of numeric type") | ||
|
|
||
| if unit_1 == unit_2: | ||
| return value | ||
|
|
||
| mph = value / UNIT_CONVERSION[unit_1] | ||
| return mph * UNIT_CONVERSION[unit_2] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """Test Home Assistant speed utility functions.""" | ||
| import pytest | ||
|
|
||
| from homeassistant.const import ( | ||
| SPEED_INCHES_PER_DAY, | ||
| SPEED_INCHES_PER_HOUR, | ||
| SPEED_KILOMETERS_PER_HOUR, | ||
| SPEED_METERS_PER_SECOND, | ||
| SPEED_MILES_PER_HOUR, | ||
| SPEED_MILLIMETERS_PER_DAY, | ||
| ) | ||
| import homeassistant.util.speed as speed_util | ||
|
|
||
| INVALID_SYMBOL = "bob" | ||
| VALID_SYMBOL = SPEED_KILOMETERS_PER_HOUR | ||
|
|
||
|
|
||
| def test_convert_same_unit(): | ||
| """Test conversion from any unit to same unit.""" | ||
| assert speed_util.convert(2, SPEED_INCHES_PER_DAY, SPEED_INCHES_PER_DAY) == 2 | ||
| assert speed_util.convert(3, SPEED_INCHES_PER_HOUR, SPEED_INCHES_PER_HOUR) == 3 | ||
| assert ( | ||
| speed_util.convert(4, SPEED_KILOMETERS_PER_HOUR, SPEED_KILOMETERS_PER_HOUR) == 4 | ||
| ) | ||
| assert speed_util.convert(5, SPEED_METERS_PER_SECOND, SPEED_METERS_PER_SECOND) == 5 | ||
| assert speed_util.convert(6, SPEED_MILES_PER_HOUR, SPEED_MILES_PER_HOUR) == 6 | ||
| assert ( | ||
| speed_util.convert(7, SPEED_MILLIMETERS_PER_DAY, SPEED_MILLIMETERS_PER_DAY) == 7 | ||
| ) | ||
|
|
||
|
|
||
| def test_convert_invalid_unit(): | ||
| """Test exception is thrown for invalid units.""" | ||
| with pytest.raises(ValueError): | ||
| speed_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| speed_util.convert(5, VALID_SYMBOL, INVALID_SYMBOL) | ||
|
|
||
|
|
||
| def test_convert_nonnumeric_value(): | ||
| """Test exception is thrown for nonnumeric type.""" | ||
| with pytest.raises(TypeError): | ||
| speed_util.convert("a", SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR) | ||
|
|
||
|
|
||
| def test_convert_from_kph(): | ||
|
rianadon marked this conversation as resolved.
Outdated
|
||
| """Test conversion from kph to other units.""" | ||
| kph = 5 | ||
| assert speed_util.convert( | ||
| kph, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR | ||
| ) == pytest.approx(3.10686, abs=1e-5) | ||
|
|
||
|
|
||
| def test_convert_from_mph(): | ||
| """Test conversion from mph to other units.""" | ||
| mph = 5 | ||
| assert speed_util.convert( | ||
| mph, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR | ||
| ) == pytest.approx(8.04672, abs=1e-5) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.