From 9c3b40dad116bccc349b19f50c024b85590e33d2 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 28 Sep 2022 19:39:44 +0200 Subject: [PATCH] Deprecate conversion utilities (#78957) * Deprecate utilities * Deprecate distance and speed * Add tests * Adjust pylint * Simplify temperature deprecation --- homeassistant/util/distance.py | 7 +++++++ homeassistant/util/pressure.py | 8 +++++++- homeassistant/util/speed.py | 7 +++++++ homeassistant/util/temperature.py | 20 +++++++++++--------- homeassistant/util/volume.py | 26 +++++++++++--------------- pylint/plugins/hass_imports.py | 6 ++++++ tests/util/test_distance.py | 6 ++++++ tests/util/test_pressure.py | 6 ++++++ tests/util/test_speed.py | 6 ++++++ tests/util/test_temperature.py | 6 ++++++ tests/util/test_volume.py | 6 ++++++ 11 files changed, 79 insertions(+), 25 deletions(-) diff --git a/homeassistant/util/distance.py b/homeassistant/util/distance.py index 7eaa1d23fd9..09cd55a9cee 100644 --- a/homeassistant/util/distance.py +++ b/homeassistant/util/distance.py @@ -13,6 +13,7 @@ from homeassistant.const import ( # pylint: disable=unused-import # noqa: F401 LENGTH_YARD, UNIT_NOT_RECOGNIZED_TEMPLATE, ) +from homeassistant.helpers.frame import report from .unit_conversion import DistanceConverter @@ -21,4 +22,10 @@ VALID_UNITS = DistanceConverter.VALID_UNITS 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 2022.4, it should be updated to use " + "unit_conversion.DistanceConverter instead", + error_if_core=False, + ) return DistanceConverter.convert(value, from_unit, to_unit) diff --git a/homeassistant/util/pressure.py b/homeassistant/util/pressure.py index 2b0f28fcc03..2a8e20ed025 100644 --- a/homeassistant/util/pressure.py +++ b/homeassistant/util/pressure.py @@ -14,6 +14,7 @@ from homeassistant.const import ( # pylint: disable=unused-import # noqa: F401 PRESSURE_PSI, UNIT_NOT_RECOGNIZED_TEMPLATE, ) +from homeassistant.helpers.frame import report from .unit_conversion import PressureConverter @@ -24,5 +25,10 @@ VALID_UNITS = PressureConverter.VALID_UNITS def convert(value: float, from_unit: str, to_unit: str) -> float: """Convert one unit of measurement to another.""" - # Need to add warning when core migration finished + report( + "uses pressure utility. This is deprecated since 2022.10 and will " + "stop working in Home Assistant 2022.4, it should be updated to use " + "unit_conversion.PressureConverter instead", + error_if_core=False, + ) return PressureConverter.convert(value, from_unit, to_unit) diff --git a/homeassistant/util/speed.py b/homeassistant/util/speed.py index 7e81337f7c5..18410272a7f 100644 --- a/homeassistant/util/speed.py +++ b/homeassistant/util/speed.py @@ -13,6 +13,7 @@ from homeassistant.const import ( # pylint: disable=unused-import # noqa: F401 SPEED_MILLIMETERS_PER_DAY, UNIT_NOT_RECOGNIZED_TEMPLATE, ) +from homeassistant.helpers.frame import report from .unit_conversion import ( # pylint: disable=unused-import # noqa: F401 _FOOT_TO_M as FOOT_TO_M, @@ -31,4 +32,10 @@ VALID_UNITS = SpeedConverter.VALID_UNITS def convert(value: float, from_unit: str, to_unit: str) -> float: """Convert one unit of measurement to another.""" + report( + "uses speed utility. This is deprecated since 2022.10 and will " + "stop working in Home Assistant 2022.4, it should be updated to use " + "unit_conversion.SpeedConverter instead", + error_if_core=False, + ) return SpeedConverter.convert(value, from_unit, to_unit) diff --git a/homeassistant/util/temperature.py b/homeassistant/util/temperature.py index 26ce3863519..e08e1207e06 100644 --- a/homeassistant/util/temperature.py +++ b/homeassistant/util/temperature.py @@ -6,6 +6,7 @@ from homeassistant.const import ( # pylint: disable=unused-import # noqa: F401 TEMPERATURE, UNIT_NOT_RECOGNIZED_TEMPLATE, ) +from homeassistant.helpers.frame import report from .unit_conversion import TemperatureConverter @@ -14,33 +15,34 @@ VALID_UNITS = TemperatureConverter.VALID_UNITS def fahrenheit_to_celsius(fahrenheit: float, interval: bool = False) -> float: """Convert a temperature in Fahrenheit to Celsius.""" - # Need to add warning when core migration finished - return TemperatureConverter.fahrenheit_to_celsius(fahrenheit, interval) + return convert(fahrenheit, TEMP_FAHRENHEIT, TEMP_CELSIUS, interval) def kelvin_to_celsius(kelvin: float, interval: bool = False) -> float: """Convert a temperature in Kelvin to Celsius.""" - # Need to add warning when core migration finished - return TemperatureConverter.kelvin_to_celsius(kelvin, interval) + return convert(kelvin, TEMP_KELVIN, TEMP_CELSIUS, interval) def celsius_to_fahrenheit(celsius: float, interval: bool = False) -> float: """Convert a temperature in Celsius to Fahrenheit.""" - # Need to add warning when core migration finished - return TemperatureConverter.celsius_to_fahrenheit(celsius, interval) + return convert(celsius, TEMP_CELSIUS, TEMP_FAHRENHEIT, interval) def celsius_to_kelvin(celsius: float, interval: bool = False) -> float: """Convert a temperature in Celsius to Fahrenheit.""" - # Need to add warning when core migration finished - return TemperatureConverter.celsius_to_kelvin(celsius, interval) + return convert(celsius, TEMP_CELSIUS, TEMP_KELVIN, interval) def convert( temperature: float, from_unit: str, to_unit: str, interval: bool = False ) -> float: """Convert a temperature from one unit to another.""" - # Need to add warning when core migration finished + report( + "uses temperature utility. This is deprecated since 2022.10 and will " + "stop working in Home Assistant 2022.4, it should be updated to use " + "unit_conversion.TemperatureConverter instead", + error_if_core=False, + ) return TemperatureConverter.convert( temperature, from_unit, to_unit, interval=interval ) diff --git a/homeassistant/util/volume.py b/homeassistant/util/volume.py index 5a314ec240c..f63f7de2cf3 100644 --- a/homeassistant/util/volume.py +++ b/homeassistant/util/volume.py @@ -11,6 +11,7 @@ from homeassistant.const import ( # pylint: disable=unused-import # noqa: F401 VOLUME_LITERS, VOLUME_MILLILITERS, ) +from homeassistant.helpers.frame import report from .unit_conversion import VolumeConverter @@ -21,35 +22,30 @@ VALID_UNITS = VolumeConverter.VALID_UNITS def liter_to_gallon(liter: float) -> float: """Convert a volume measurement in Liter to Gallon.""" - # Need to add warning when core migration finished - return _convert(liter, VOLUME_LITERS, VOLUME_GALLONS) + return convert(liter, VOLUME_LITERS, VOLUME_GALLONS) def gallon_to_liter(gallon: float) -> float: """Convert a volume measurement in Gallon to Liter.""" - # Need to add warning when core migration finished - return _convert(gallon, VOLUME_GALLONS, VOLUME_LITERS) + return convert(gallon, VOLUME_GALLONS, VOLUME_LITERS) def cubic_meter_to_cubic_feet(cubic_meter: float) -> float: """Convert a volume measurement in cubic meter to cubic feet.""" - # Need to add warning when core migration finished - return _convert(cubic_meter, VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET) + return convert(cubic_meter, VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET) def cubic_feet_to_cubic_meter(cubic_feet: float) -> float: """Convert a volume measurement in cubic feet to cubic meter.""" - # Need to add warning when core migration finished - return _convert(cubic_feet, VOLUME_CUBIC_FEET, VOLUME_CUBIC_METERS) + return convert(cubic_feet, VOLUME_CUBIC_FEET, VOLUME_CUBIC_METERS) def convert(volume: float, from_unit: str, to_unit: str) -> float: """Convert a volume from one unit to another.""" - # Need to add warning when core migration finished + report( + "uses volume utility. This is deprecated since 2022.10 and will " + "stop working in Home Assistant 2022.4, it should be updated to use " + "unit_conversion.VolumeConverter instead", + error_if_core=False, + ) return VolumeConverter.convert(volume, from_unit, to_unit) - - -def _convert(volume: float, from_unit: str, to_unit: str) -> float: - """Convert a volume from one unit to another, bypassing checks.""" - cubic_meter = volume / UNIT_CONVERSION[from_unit] - return cubic_meter * UNIT_CONVERSION[to_unit] diff --git a/pylint/plugins/hass_imports.py b/pylint/plugins/hass_imports.py index 718f9550312..45deecfc02e 100644 --- a/pylint/plugins/hass_imports.py +++ b/pylint/plugins/hass_imports.py @@ -286,6 +286,12 @@ _OBSOLETE_IMPORT: dict[str, list[ObsoleteImportMatch]] = { constant=re.compile(r"^DISABLED_(\w*)$"), ), ], + "homeassistant.util": [ + ObsoleteImportMatch( + reason="replaced by unit_conversion.***Converter", + constant=re.compile(r"^(distance|pressure|speed|temperature|volume)$"), + ), + ], } diff --git a/tests/util/test_distance.py b/tests/util/test_distance.py index 652f95d7bf0..c5a404e28f1 100644 --- a/tests/util/test_distance.py +++ b/tests/util/test_distance.py @@ -19,6 +19,12 @@ INVALID_SYMBOL = "bob" VALID_SYMBOL = LENGTH_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, LENGTH_METERS, LENGTH_METERS) == 2 + assert "use unit_conversion.DistanceConverter instead" in caplog.text + + def test_convert_same_unit(): """Test conversion from any unit to same unit.""" assert distance_util.convert(5, LENGTH_KILOMETERS, LENGTH_KILOMETERS) == 5 diff --git a/tests/util/test_pressure.py b/tests/util/test_pressure.py index 13c1a6d1225..769c5aaf801 100644 --- a/tests/util/test_pressure.py +++ b/tests/util/test_pressure.py @@ -18,6 +18,12 @@ INVALID_SYMBOL = "bob" VALID_SYMBOL = PRESSURE_PA +def test_raise_deprecation_warning(caplog: pytest.LogCaptureFixture) -> None: + """Ensure that a warning is raised on use of convert.""" + assert pressure_util.convert(2, PRESSURE_PA, PRESSURE_PA) == 2 + assert "use unit_conversion.PressureConverter instead" in caplog.text + + def test_convert_same_unit(): """Test conversion from any unit to same unit.""" assert pressure_util.convert(2, PRESSURE_PA, PRESSURE_PA) == 2 diff --git a/tests/util/test_speed.py b/tests/util/test_speed.py index f944f0a5d8c..55458899911 100644 --- a/tests/util/test_speed.py +++ b/tests/util/test_speed.py @@ -18,6 +18,12 @@ INVALID_SYMBOL = "bob" VALID_SYMBOL = SPEED_KILOMETERS_PER_HOUR +def test_raise_deprecation_warning(caplog: pytest.LogCaptureFixture) -> None: + """Ensure that a warning is raised on use of convert.""" + assert speed_util.convert(2, SPEED_INCHES_PER_DAY, SPEED_INCHES_PER_DAY) == 2 + assert "use unit_conversion.SpeedConverter instead" in caplog.text + + 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 diff --git a/tests/util/test_temperature.py b/tests/util/test_temperature.py index 7151d6ed816..6f92e12a386 100644 --- a/tests/util/test_temperature.py +++ b/tests/util/test_temperature.py @@ -9,6 +9,12 @@ INVALID_SYMBOL = "bob" VALID_SYMBOL = TEMP_CELSIUS +def test_raise_deprecation_warning(caplog: pytest.LogCaptureFixture) -> None: + """Ensure that a warning is raised on use of convert.""" + assert temperature_util.convert(2, TEMP_CELSIUS, TEMP_CELSIUS) == 2 + assert "use unit_conversion.TemperatureConverter instead" in caplog.text + + @pytest.mark.parametrize( "function_name, value, expected", [ diff --git a/tests/util/test_volume.py b/tests/util/test_volume.py index c556a3b073d..87f7d47821f 100644 --- a/tests/util/test_volume.py +++ b/tests/util/test_volume.py @@ -17,6 +17,12 @@ INVALID_SYMBOL = "bob" VALID_SYMBOL = VOLUME_LITERS +def test_raise_deprecation_warning(caplog: pytest.LogCaptureFixture) -> None: + """Ensure that a warning is raised on use of convert.""" + assert volume_util.convert(2, VOLUME_LITERS, VOLUME_LITERS) == 2 + assert "use unit_conversion.VolumeConverter instead" in caplog.text + + @pytest.mark.parametrize( "function_name, value, expected", [