mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Remove deprecated distance conversion functions (#101199)
This commit is contained in:
parent
31ea00f5c7
commit
8b7fae5200
@ -46,14 +46,6 @@ homeassistant.util.decorator
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
homeassistant.util.distance
|
||||
---------------------------
|
||||
|
||||
.. automodule:: homeassistant.util.distance
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
homeassistant.util.dt
|
||||
---------------------
|
||||
|
||||
|
@ -1,58 +0,0 @@
|
||||
"""Distance util functions."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
|
||||
# pylint: disable-next=hass-deprecated-import
|
||||
from homeassistant.const import ( # noqa: F401
|
||||
LENGTH,
|
||||
LENGTH_CENTIMETERS,
|
||||
LENGTH_FEET,
|
||||
LENGTH_INCHES,
|
||||
LENGTH_KILOMETERS,
|
||||
LENGTH_METERS,
|
||||
LENGTH_MILES,
|
||||
LENGTH_MILLIMETERS,
|
||||
LENGTH_YARD,
|
||||
UNIT_NOT_RECOGNIZED_TEMPLATE,
|
||||
)
|
||||
from homeassistant.helpers.frame import report
|
||||
|
||||
from .unit_conversion import DistanceConverter
|
||||
|
||||
VALID_UNITS = DistanceConverter.VALID_UNITS
|
||||
|
||||
TO_METERS: dict[str, Callable[[float], float]] = {
|
||||
LENGTH_METERS: lambda meters: meters,
|
||||
LENGTH_MILES: lambda miles: miles * 1609.344,
|
||||
LENGTH_YARD: lambda yards: yards * 0.9144,
|
||||
LENGTH_FEET: lambda feet: feet * 0.3048,
|
||||
LENGTH_INCHES: lambda inches: inches * 0.0254,
|
||||
LENGTH_KILOMETERS: lambda kilometers: kilometers * 1000,
|
||||
LENGTH_CENTIMETERS: lambda centimeters: centimeters * 0.01,
|
||||
LENGTH_MILLIMETERS: lambda millimeters: millimeters * 0.001,
|
||||
}
|
||||
|
||||
METERS_TO: dict[str, Callable[[float], float]] = {
|
||||
LENGTH_METERS: lambda meters: meters,
|
||||
LENGTH_MILES: lambda meters: meters * 0.000621371,
|
||||
LENGTH_YARD: lambda meters: meters * 1.09361,
|
||||
LENGTH_FEET: lambda meters: meters * 3.28084,
|
||||
LENGTH_INCHES: lambda meters: meters * 39.3701,
|
||||
LENGTH_KILOMETERS: lambda meters: meters * 0.001,
|
||||
LENGTH_CENTIMETERS: lambda meters: meters * 100,
|
||||
LENGTH_MILLIMETERS: lambda meters: meters * 1000,
|
||||
}
|
||||
|
||||
|
||||
def convert(value: float, from_unit: str, to_unit: str) -> float:
|
||||
"""Convert one unit of measurement to another."""
|
||||
report(
|
||||
(
|
||||
"uses distance utility. This is deprecated since 2022.10 and will "
|
||||
"stop working in Home Assistant 2023.4, it should be updated to use "
|
||||
"unit_conversion.DistanceConverter instead"
|
||||
),
|
||||
error_if_core=False,
|
||||
)
|
||||
return DistanceConverter.convert(value, from_unit, to_unit)
|
@ -1,211 +0,0 @@
|
||||
"""Test Home Assistant distance utility functions."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.const import UnitOfLength
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
import homeassistant.util.distance as distance_util
|
||||
|
||||
INVALID_SYMBOL = "bob"
|
||||
VALID_SYMBOL = UnitOfLength.KILOMETERS
|
||||
|
||||
|
||||
def test_raise_deprecation_warning(caplog: pytest.LogCaptureFixture) -> None:
|
||||
"""Ensure that a warning is raised on use of convert."""
|
||||
assert distance_util.convert(2, UnitOfLength.METERS, UnitOfLength.METERS) == 2
|
||||
assert "use unit_conversion.DistanceConverter instead" in caplog.text
|
||||
|
||||
|
||||
def test_convert_same_unit() -> None:
|
||||
"""Test conversion from any unit to same unit."""
|
||||
assert (
|
||||
distance_util.convert(5, UnitOfLength.KILOMETERS, UnitOfLength.KILOMETERS) == 5
|
||||
)
|
||||
assert distance_util.convert(2, UnitOfLength.METERS, UnitOfLength.METERS) == 2
|
||||
assert (
|
||||
distance_util.convert(6, UnitOfLength.CENTIMETERS, UnitOfLength.CENTIMETERS)
|
||||
== 6
|
||||
)
|
||||
assert (
|
||||
distance_util.convert(3, UnitOfLength.MILLIMETERS, UnitOfLength.MILLIMETERS)
|
||||
== 3
|
||||
)
|
||||
assert distance_util.convert(10, UnitOfLength.MILES, UnitOfLength.MILES) == 10
|
||||
assert distance_util.convert(9, UnitOfLength.YARDS, UnitOfLength.YARDS) == 9
|
||||
assert distance_util.convert(8, UnitOfLength.FEET, UnitOfLength.FEET) == 8
|
||||
assert distance_util.convert(7, UnitOfLength.INCHES, UnitOfLength.INCHES) == 7
|
||||
|
||||
|
||||
def test_convert_invalid_unit() -> None:
|
||||
"""Test exception is thrown for invalid units."""
|
||||
with pytest.raises(HomeAssistantError, match="is not a recognized .* unit"):
|
||||
distance_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL)
|
||||
|
||||
with pytest.raises(HomeAssistantError, match="is not a recognized .* unit"):
|
||||
distance_util.convert(5, VALID_SYMBOL, INVALID_SYMBOL)
|
||||
|
||||
|
||||
def test_convert_nonnumeric_value() -> None:
|
||||
"""Test exception is thrown for nonnumeric type."""
|
||||
with pytest.raises(TypeError):
|
||||
distance_util.convert("a", UnitOfLength.KILOMETERS, UnitOfLength.METERS)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("unit", "expected"),
|
||||
[
|
||||
(UnitOfLength.KILOMETERS, 8.04672),
|
||||
(UnitOfLength.METERS, 8046.72),
|
||||
(UnitOfLength.CENTIMETERS, 804672.0),
|
||||
(UnitOfLength.MILLIMETERS, 8046720.0),
|
||||
(UnitOfLength.YARDS, 8800.0),
|
||||
(UnitOfLength.FEET, 26400.0008448),
|
||||
(UnitOfLength.INCHES, 316800.171072),
|
||||
],
|
||||
)
|
||||
def test_convert_from_miles(unit, expected) -> None:
|
||||
"""Test conversion from miles to other units."""
|
||||
miles = 5
|
||||
assert distance_util.convert(miles, UnitOfLength.MILES, unit) == pytest.approx(
|
||||
expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("unit", "expected"),
|
||||
[
|
||||
(UnitOfLength.KILOMETERS, 0.0045720000000000005),
|
||||
(UnitOfLength.METERS, 4.572),
|
||||
(UnitOfLength.CENTIMETERS, 457.2),
|
||||
(UnitOfLength.MILLIMETERS, 4572),
|
||||
(UnitOfLength.MILES, 0.002840908212),
|
||||
(UnitOfLength.FEET, 15.00000048),
|
||||
(UnitOfLength.INCHES, 180.0000972),
|
||||
],
|
||||
)
|
||||
def test_convert_from_yards(unit, expected) -> None:
|
||||
"""Test conversion from yards to other units."""
|
||||
yards = 5
|
||||
assert distance_util.convert(yards, UnitOfLength.YARDS, unit) == pytest.approx(
|
||||
expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("unit", "expected"),
|
||||
[
|
||||
(UnitOfLength.KILOMETERS, 1.524),
|
||||
(UnitOfLength.METERS, 1524),
|
||||
(UnitOfLength.CENTIMETERS, 152400.0),
|
||||
(UnitOfLength.MILLIMETERS, 1524000.0),
|
||||
(UnitOfLength.MILES, 0.9469694040000001),
|
||||
(UnitOfLength.YARDS, 1666.66667),
|
||||
(UnitOfLength.INCHES, 60000.032400000004),
|
||||
],
|
||||
)
|
||||
def test_convert_from_feet(unit, expected) -> None:
|
||||
"""Test conversion from feet to other units."""
|
||||
feet = 5000
|
||||
assert distance_util.convert(feet, UnitOfLength.FEET, unit) == pytest.approx(
|
||||
expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("unit", "expected"),
|
||||
[
|
||||
(UnitOfLength.KILOMETERS, 0.127),
|
||||
(UnitOfLength.METERS, 127.0),
|
||||
(UnitOfLength.CENTIMETERS, 12700.0),
|
||||
(UnitOfLength.MILLIMETERS, 127000.0),
|
||||
(UnitOfLength.MILES, 0.078914117),
|
||||
(UnitOfLength.YARDS, 138.88889),
|
||||
(UnitOfLength.FEET, 416.66668),
|
||||
],
|
||||
)
|
||||
def test_convert_from_inches(unit, expected) -> None:
|
||||
"""Test conversion from inches to other units."""
|
||||
inches = 5000
|
||||
assert distance_util.convert(inches, UnitOfLength.INCHES, unit) == pytest.approx(
|
||||
expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("unit", "expected"),
|
||||
[
|
||||
(UnitOfLength.METERS, 5000),
|
||||
(UnitOfLength.CENTIMETERS, 500000),
|
||||
(UnitOfLength.MILLIMETERS, 5000000),
|
||||
(UnitOfLength.MILES, 3.106855),
|
||||
(UnitOfLength.YARDS, 5468.066),
|
||||
(UnitOfLength.FEET, 16404.2),
|
||||
(UnitOfLength.INCHES, 196850.5),
|
||||
],
|
||||
)
|
||||
def test_convert_from_kilometers(unit, expected) -> None:
|
||||
"""Test conversion from kilometers to other units."""
|
||||
km = 5
|
||||
assert distance_util.convert(km, UnitOfLength.KILOMETERS, unit) == pytest.approx(
|
||||
expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("unit", "expected"),
|
||||
[
|
||||
(UnitOfLength.KILOMETERS, 5),
|
||||
(UnitOfLength.CENTIMETERS, 500000),
|
||||
(UnitOfLength.MILLIMETERS, 5000000),
|
||||
(UnitOfLength.MILES, 3.106855),
|
||||
(UnitOfLength.YARDS, 5468.066),
|
||||
(UnitOfLength.FEET, 16404.2),
|
||||
(UnitOfLength.INCHES, 196850.5),
|
||||
],
|
||||
)
|
||||
def test_convert_from_meters(unit, expected) -> None:
|
||||
"""Test conversion from meters to other units."""
|
||||
m = 5000
|
||||
assert distance_util.convert(m, UnitOfLength.METERS, unit) == pytest.approx(
|
||||
expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("unit", "expected"),
|
||||
[
|
||||
(UnitOfLength.KILOMETERS, 5),
|
||||
(UnitOfLength.METERS, 5000),
|
||||
(UnitOfLength.MILLIMETERS, 5000000),
|
||||
(UnitOfLength.MILES, 3.106855),
|
||||
(UnitOfLength.YARDS, 5468.066),
|
||||
(UnitOfLength.FEET, 16404.2),
|
||||
(UnitOfLength.INCHES, 196850.5),
|
||||
],
|
||||
)
|
||||
def test_convert_from_centimeters(unit, expected) -> None:
|
||||
"""Test conversion from centimeters to other units."""
|
||||
cm = 500000
|
||||
assert distance_util.convert(cm, UnitOfLength.CENTIMETERS, unit) == pytest.approx(
|
||||
expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("unit", "expected"),
|
||||
[
|
||||
(UnitOfLength.KILOMETERS, 5),
|
||||
(UnitOfLength.METERS, 5000),
|
||||
(UnitOfLength.CENTIMETERS, 500000),
|
||||
(UnitOfLength.MILES, 3.106855),
|
||||
(UnitOfLength.YARDS, 5468.066),
|
||||
(UnitOfLength.FEET, 16404.2),
|
||||
(UnitOfLength.INCHES, 196850.5),
|
||||
],
|
||||
)
|
||||
def test_convert_from_millimeters(unit, expected) -> None:
|
||||
"""Test conversion from millimeters to other units."""
|
||||
mm = 5000000
|
||||
assert distance_util.convert(mm, UnitOfLength.MILLIMETERS, unit) == pytest.approx(
|
||||
expected
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user