From 6fd60024cce9c5cc00313f60aa1d433d0d710fa0 Mon Sep 17 00:00:00 2001 From: Meow Date: Thu, 3 Aug 2023 21:12:01 +0200 Subject: [PATCH] Refactored deprecated UNITS (#97368) --- .../specific_devices/test_arlo_baby.py | 4 +- .../specific_devices/test_ecobee3.py | 4 +- .../specific_devices/test_eve_degree.py | 9 +- .../specific_devices/test_mysa_living.py | 4 +- .../specific_devices/test_velux_gateway.py | 4 +- tests/helpers/test_temperature.py | 11 +- tests/helpers/test_template.py | 16 +- tests/util/test_distance.py | 179 ++++++++++-------- tests/util/test_temperature.py | 62 ++++-- 9 files changed, 165 insertions(+), 128 deletions(-) diff --git a/tests/components/homekit_controller/specific_devices/test_arlo_baby.py b/tests/components/homekit_controller/specific_devices/test_arlo_baby.py index c90ff20c593..ae44f7f774f 100644 --- a/tests/components/homekit_controller/specific_devices/test_arlo_baby.py +++ b/tests/components/homekit_controller/specific_devices/test_arlo_baby.py @@ -1,6 +1,6 @@ """Make sure that an Arlo Baby can be setup.""" from homeassistant.components.sensor import SensorStateClass -from homeassistant.const import PERCENTAGE, TEMP_CELSIUS, EntityCategory +from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTemperature from homeassistant.core import HomeAssistant from ..common import ( @@ -64,7 +64,7 @@ async def test_arlo_baby_setup(hass: HomeAssistant) -> None: unique_id="00:00:00:00:00:00_1_1000", friendly_name="ArloBabyA0 Temperature", capabilities={"state_class": SensorStateClass.MEASUREMENT}, - unit_of_measurement=TEMP_CELSIUS, + unit_of_measurement=UnitOfTemperature.CELSIUS, state="24.0", ), EntityTestInfo( diff --git a/tests/components/homekit_controller/specific_devices/test_ecobee3.py b/tests/components/homekit_controller/specific_devices/test_ecobee3.py index d9af858de9f..1cdd4ccb907 100644 --- a/tests/components/homekit_controller/specific_devices/test_ecobee3.py +++ b/tests/components/homekit_controller/specific_devices/test_ecobee3.py @@ -15,7 +15,7 @@ from homeassistant.components.climate import ( ) from homeassistant.components.sensor import SensorStateClass from homeassistant.config_entries import ConfigEntryState -from homeassistant.const import TEMP_CELSIUS +from homeassistant.const import UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -126,7 +126,7 @@ async def test_ecobee3_setup(hass: HomeAssistant) -> None: friendly_name="HomeW Current Temperature", unique_id="00:00:00:00:00:00_1_16_19", capabilities={"state_class": SensorStateClass.MEASUREMENT}, - unit_of_measurement=TEMP_CELSIUS, + unit_of_measurement=UnitOfTemperature.CELSIUS, state="21.8", ), EntityTestInfo( diff --git a/tests/components/homekit_controller/specific_devices/test_eve_degree.py b/tests/components/homekit_controller/specific_devices/test_eve_degree.py index e866069ef16..10fcd8ede8e 100644 --- a/tests/components/homekit_controller/specific_devices/test_eve_degree.py +++ b/tests/components/homekit_controller/specific_devices/test_eve_degree.py @@ -1,7 +1,12 @@ """Make sure that Eve Degree (via Eve Extend) is enumerated properly.""" from homeassistant.components.number import NumberMode from homeassistant.components.sensor import SensorStateClass -from homeassistant.const import PERCENTAGE, TEMP_CELSIUS, EntityCategory, UnitOfPressure +from homeassistant.const import ( + PERCENTAGE, + EntityCategory, + UnitOfPressure, + UnitOfTemperature, +) from homeassistant.core import HomeAssistant from ..common import ( @@ -36,7 +41,7 @@ async def test_eve_degree_setup(hass: HomeAssistant) -> None: unique_id="00:00:00:00:00:00_1_22", friendly_name="Eve Degree AA11 Temperature", capabilities={"state_class": SensorStateClass.MEASUREMENT}, - unit_of_measurement=TEMP_CELSIUS, + unit_of_measurement=UnitOfTemperature.CELSIUS, state="22.7719116210938", ), EntityTestInfo( diff --git a/tests/components/homekit_controller/specific_devices/test_mysa_living.py b/tests/components/homekit_controller/specific_devices/test_mysa_living.py index ca9c6cecde5..48828a2a6ad 100644 --- a/tests/components/homekit_controller/specific_devices/test_mysa_living.py +++ b/tests/components/homekit_controller/specific_devices/test_mysa_living.py @@ -1,7 +1,7 @@ """Make sure that Mysa Living is enumerated properly.""" from homeassistant.components.climate import ClimateEntityFeature from homeassistant.components.sensor import SensorStateClass -from homeassistant.const import PERCENTAGE, TEMP_CELSIUS +from homeassistant.const import PERCENTAGE, UnitOfTemperature from homeassistant.core import HomeAssistant from ..common import ( @@ -55,7 +55,7 @@ async def test_mysa_living_setup(hass: HomeAssistant) -> None: entity_id="sensor.mysa_85dda9_current_temperature", friendly_name="Mysa-85dda9 Current Temperature", unique_id="00:00:00:00:00:00_1_20_25", - unit_of_measurement=TEMP_CELSIUS, + unit_of_measurement=UnitOfTemperature.CELSIUS, capabilities={"state_class": SensorStateClass.MEASUREMENT}, state="24.1", ), diff --git a/tests/components/homekit_controller/specific_devices/test_velux_gateway.py b/tests/components/homekit_controller/specific_devices/test_velux_gateway.py index 384b1d49d78..854de4b89d8 100644 --- a/tests/components/homekit_controller/specific_devices/test_velux_gateway.py +++ b/tests/components/homekit_controller/specific_devices/test_velux_gateway.py @@ -7,7 +7,7 @@ from homeassistant.components.sensor import SensorStateClass from homeassistant.const import ( CONCENTRATION_PARTS_PER_MILLION, PERCENTAGE, - TEMP_CELSIUS, + UnitOfTemperature, ) from homeassistant.core import HomeAssistant @@ -73,7 +73,7 @@ async def test_velux_cover_setup(hass: HomeAssistant) -> None: friendly_name="VELUX Sensor Temperature sensor", capabilities={"state_class": SensorStateClass.MEASUREMENT}, unique_id="00:00:00:00:00:00_2_8", - unit_of_measurement=TEMP_CELSIUS, + unit_of_measurement=UnitOfTemperature.CELSIUS, state="18.9", ), EntityTestInfo( diff --git a/tests/helpers/test_temperature.py b/tests/helpers/test_temperature.py index c4ab540f9d6..ceb4f7bdef2 100644 --- a/tests/helpers/test_temperature.py +++ b/tests/helpers/test_temperature.py @@ -5,8 +5,7 @@ from homeassistant.const import ( PRECISION_HALVES, PRECISION_TENTHS, PRECISION_WHOLE, - TEMP_CELSIUS, - TEMP_FAHRENHEIT, + UnitOfTemperature, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.temperature import display_temp @@ -18,21 +17,21 @@ def test_temperature_not_a_number(hass: HomeAssistant) -> None: """Test that temperature is a number.""" temp = "Temperature" with pytest.raises(Exception) as exception: - display_temp(hass, temp, TEMP_CELSIUS, PRECISION_HALVES) + display_temp(hass, temp, UnitOfTemperature.CELSIUS, PRECISION_HALVES) assert f"Temperature is not a number: {temp}" in str(exception.value) def test_celsius_halves(hass: HomeAssistant) -> None: """Test temperature to celsius rounding to halves.""" - assert display_temp(hass, TEMP, TEMP_CELSIUS, PRECISION_HALVES) == 24.5 + assert display_temp(hass, TEMP, UnitOfTemperature.CELSIUS, PRECISION_HALVES) == 24.5 def test_celsius_tenths(hass: HomeAssistant) -> None: """Test temperature to celsius rounding to tenths.""" - assert display_temp(hass, TEMP, TEMP_CELSIUS, PRECISION_TENTHS) == 24.6 + assert display_temp(hass, TEMP, UnitOfTemperature.CELSIUS, PRECISION_TENTHS) == 24.6 def test_fahrenheit_wholes(hass: HomeAssistant) -> None: """Test temperature to fahrenheit rounding to wholes.""" - assert display_temp(hass, TEMP, TEMP_FAHRENHEIT, PRECISION_WHOLE) == -4 + assert display_temp(hass, TEMP, UnitOfTemperature.FAHRENHEIT, PRECISION_WHOLE) == -4 diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 0c3f0e4469a..851ba7fb79b 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -19,15 +19,15 @@ from homeassistant.components import group from homeassistant.config import async_process_ha_core_config from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, - LENGTH_METERS, - LENGTH_MILLIMETERS, - MASS_GRAMS, STATE_ON, STATE_UNAVAILABLE, - TEMP_CELSIUS, VOLUME_LITERS, + UnitOfLength, + UnitOfMass, + UnitOfPrecipitationDepth, UnitOfPressure, UnitOfSpeed, + UnitOfTemperature, ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import TemplateError @@ -52,12 +52,12 @@ def _set_up_units(hass: HomeAssistant) -> None: """Set up the tests.""" hass.config.units = UnitSystem( "custom", - accumulated_precipitation=LENGTH_MILLIMETERS, + accumulated_precipitation=UnitOfPrecipitationDepth.MILLIMETERS, conversions={}, - length=LENGTH_METERS, - mass=MASS_GRAMS, + length=UnitOfLength.METERS, + mass=UnitOfMass.GRAMS, pressure=UnitOfPressure.PA, - temperature=TEMP_CELSIUS, + temperature=UnitOfTemperature.CELSIUS, volume=VOLUME_LITERS, wind_speed=UnitOfSpeed.KILOMETERS_PER_HOUR, ) diff --git a/tests/util/test_distance.py b/tests/util/test_distance.py index d0b4bbc1ebe..90c2238bb63 100644 --- a/tests/util/test_distance.py +++ b/tests/util/test_distance.py @@ -3,38 +3,39 @@ import pytest from homeassistant.const import ( - LENGTH_CENTIMETERS, - LENGTH_FEET, - LENGTH_INCHES, - LENGTH_KILOMETERS, - LENGTH_METERS, - LENGTH_MILES, - LENGTH_MILLIMETERS, - LENGTH_YARD, + UnitOfLength, ) from homeassistant.exceptions import HomeAssistantError import homeassistant.util.distance as distance_util INVALID_SYMBOL = "bob" -VALID_SYMBOL = LENGTH_KILOMETERS +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, LENGTH_METERS, LENGTH_METERS) == 2 + 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, LENGTH_KILOMETERS, LENGTH_KILOMETERS) == 5 - assert distance_util.convert(2, LENGTH_METERS, LENGTH_METERS) == 2 - assert distance_util.convert(6, LENGTH_CENTIMETERS, LENGTH_CENTIMETERS) == 6 - assert distance_util.convert(3, LENGTH_MILLIMETERS, LENGTH_MILLIMETERS) == 3 - assert distance_util.convert(10, LENGTH_MILES, LENGTH_MILES) == 10 - assert distance_util.convert(9, LENGTH_YARD, LENGTH_YARD) == 9 - assert distance_util.convert(8, LENGTH_FEET, LENGTH_FEET) == 8 - assert distance_util.convert(7, LENGTH_INCHES, LENGTH_INCHES) == 7 + 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: @@ -49,133 +50,145 @@ def test_convert_invalid_unit() -> None: def test_convert_nonnumeric_value() -> None: """Test exception is thrown for nonnumeric type.""" with pytest.raises(TypeError): - distance_util.convert("a", LENGTH_KILOMETERS, LENGTH_METERS) + distance_util.convert("a", UnitOfLength.KILOMETERS, UnitOfLength.METERS) @pytest.mark.parametrize( ("unit", "expected"), [ - (LENGTH_KILOMETERS, 8.04672), - (LENGTH_METERS, 8046.72), - (LENGTH_CENTIMETERS, 804672.0), - (LENGTH_MILLIMETERS, 8046720.0), - (LENGTH_YARD, 8800.0), - (LENGTH_FEET, 26400.0008448), - (LENGTH_INCHES, 316800.171072), + (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, LENGTH_MILES, unit) == pytest.approx(expected) + assert distance_util.convert(miles, UnitOfLength.MILES, unit) == pytest.approx( + expected + ) @pytest.mark.parametrize( ("unit", "expected"), [ - (LENGTH_KILOMETERS, 0.0045720000000000005), - (LENGTH_METERS, 4.572), - (LENGTH_CENTIMETERS, 457.2), - (LENGTH_MILLIMETERS, 4572), - (LENGTH_MILES, 0.002840908212), - (LENGTH_FEET, 15.00000048), - (LENGTH_INCHES, 180.0000972), + (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, LENGTH_YARD, unit) == pytest.approx(expected) + assert distance_util.convert(yards, UnitOfLength.YARDS, unit) == pytest.approx( + expected + ) @pytest.mark.parametrize( ("unit", "expected"), [ - (LENGTH_KILOMETERS, 1.524), - (LENGTH_METERS, 1524), - (LENGTH_CENTIMETERS, 152400.0), - (LENGTH_MILLIMETERS, 1524000.0), - (LENGTH_MILES, 0.9469694040000001), - (LENGTH_YARD, 1666.66667), - (LENGTH_INCHES, 60000.032400000004), + (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, LENGTH_FEET, unit) == pytest.approx(expected) + assert distance_util.convert(feet, UnitOfLength.FEET, unit) == pytest.approx( + expected + ) @pytest.mark.parametrize( ("unit", "expected"), [ - (LENGTH_KILOMETERS, 0.127), - (LENGTH_METERS, 127.0), - (LENGTH_CENTIMETERS, 12700.0), - (LENGTH_MILLIMETERS, 127000.0), - (LENGTH_MILES, 0.078914117), - (LENGTH_YARD, 138.88889), - (LENGTH_FEET, 416.66668), + (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, LENGTH_INCHES, unit) == pytest.approx(expected) + assert distance_util.convert(inches, UnitOfLength.INCHES, unit) == pytest.approx( + expected + ) @pytest.mark.parametrize( ("unit", "expected"), [ - (LENGTH_METERS, 5000), - (LENGTH_CENTIMETERS, 500000), - (LENGTH_MILLIMETERS, 5000000), - (LENGTH_MILES, 3.106855), - (LENGTH_YARD, 5468.066), - (LENGTH_FEET, 16404.2), - (LENGTH_INCHES, 196850.5), + (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, LENGTH_KILOMETERS, unit) == pytest.approx(expected) + assert distance_util.convert(km, UnitOfLength.KILOMETERS, unit) == pytest.approx( + expected + ) @pytest.mark.parametrize( ("unit", "expected"), [ - (LENGTH_KILOMETERS, 5), - (LENGTH_CENTIMETERS, 500000), - (LENGTH_MILLIMETERS, 5000000), - (LENGTH_MILES, 3.106855), - (LENGTH_YARD, 5468.066), - (LENGTH_FEET, 16404.2), - (LENGTH_INCHES, 196850.5), + (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, LENGTH_METERS, unit) == pytest.approx(expected) + assert distance_util.convert(m, UnitOfLength.METERS, unit) == pytest.approx( + expected + ) @pytest.mark.parametrize( ("unit", "expected"), [ - (LENGTH_KILOMETERS, 5), - (LENGTH_METERS, 5000), - (LENGTH_MILLIMETERS, 5000000), - (LENGTH_MILES, 3.106855), - (LENGTH_YARD, 5468.066), - (LENGTH_FEET, 16404.2), - (LENGTH_INCHES, 196850.5), + (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, LENGTH_CENTIMETERS, unit) == pytest.approx( + assert distance_util.convert(cm, UnitOfLength.CENTIMETERS, unit) == pytest.approx( expected ) @@ -183,18 +196,18 @@ def test_convert_from_centimeters(unit, expected) -> None: @pytest.mark.parametrize( ("unit", "expected"), [ - (LENGTH_KILOMETERS, 5), - (LENGTH_METERS, 5000), - (LENGTH_CENTIMETERS, 500000), - (LENGTH_MILES, 3.106855), - (LENGTH_YARD, 5468.066), - (LENGTH_FEET, 16404.2), - (LENGTH_INCHES, 196850.5), + (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, LENGTH_MILLIMETERS, unit) == pytest.approx( + assert distance_util.convert(mm, UnitOfLength.MILLIMETERS, unit) == pytest.approx( expected ) diff --git a/tests/util/test_temperature.py b/tests/util/test_temperature.py index d1b1a9fbb11..93edb8f7393 100644 --- a/tests/util/test_temperature.py +++ b/tests/util/test_temperature.py @@ -1,17 +1,22 @@ """Test Home Assistant temperature utility functions.""" import pytest -from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT, TEMP_KELVIN +from homeassistant.const import UnitOfTemperature from homeassistant.exceptions import HomeAssistantError import homeassistant.util.temperature as temperature_util INVALID_SYMBOL = "bob" -VALID_SYMBOL = TEMP_CELSIUS +VALID_SYMBOL = UnitOfTemperature.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 ( + temperature_util.convert( + 2, UnitOfTemperature.CELSIUS, UnitOfTemperature.CELSIUS + ) + == 2 + ) assert "use unit_conversion.TemperatureConverter instead" in caplog.text @@ -34,9 +39,22 @@ def test_deprecated_functions( def test_convert_same_unit() -> None: """Test conversion from any unit to same unit.""" - assert temperature_util.convert(2, TEMP_CELSIUS, TEMP_CELSIUS) == 2 - assert temperature_util.convert(3, TEMP_FAHRENHEIT, TEMP_FAHRENHEIT) == 3 - assert temperature_util.convert(4, TEMP_KELVIN, TEMP_KELVIN) == 4 + assert ( + temperature_util.convert( + 2, UnitOfTemperature.CELSIUS, UnitOfTemperature.CELSIUS + ) + == 2 + ) + assert ( + temperature_util.convert( + 3, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.FAHRENHEIT + ) + == 3 + ) + assert ( + temperature_util.convert(4, UnitOfTemperature.KELVIN, UnitOfTemperature.KELVIN) + == 4 + ) def test_convert_invalid_unit() -> None: @@ -51,24 +69,26 @@ def test_convert_invalid_unit() -> None: def test_convert_nonnumeric_value() -> None: """Test exception is thrown for nonnumeric type.""" with pytest.raises(TypeError): - temperature_util.convert("a", TEMP_CELSIUS, TEMP_FAHRENHEIT) + temperature_util.convert( + "a", UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT + ) def test_convert_from_celsius() -> None: """Test conversion from C to other units.""" celsius = 100 assert temperature_util.convert( - celsius, TEMP_CELSIUS, TEMP_FAHRENHEIT + celsius, UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT ) == pytest.approx(212.0) assert temperature_util.convert( - celsius, TEMP_CELSIUS, TEMP_KELVIN + celsius, UnitOfTemperature.CELSIUS, UnitOfTemperature.KELVIN ) == pytest.approx(373.15) # Interval assert temperature_util.convert( - celsius, TEMP_CELSIUS, TEMP_FAHRENHEIT, True + celsius, UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT, True ) == pytest.approx(180.0) assert temperature_util.convert( - celsius, TEMP_CELSIUS, TEMP_KELVIN, True + celsius, UnitOfTemperature.CELSIUS, UnitOfTemperature.KELVIN, True ) == pytest.approx(100) @@ -76,33 +96,33 @@ def test_convert_from_fahrenheit() -> None: """Test conversion from F to other units.""" fahrenheit = 100 assert temperature_util.convert( - fahrenheit, TEMP_FAHRENHEIT, TEMP_CELSIUS + fahrenheit, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.CELSIUS ) == pytest.approx(37.77777777777778) assert temperature_util.convert( - fahrenheit, TEMP_FAHRENHEIT, TEMP_KELVIN + fahrenheit, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.KELVIN ) == pytest.approx(310.92777777777775) # Interval assert temperature_util.convert( - fahrenheit, TEMP_FAHRENHEIT, TEMP_CELSIUS, True + fahrenheit, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.CELSIUS, True ) == pytest.approx(55.55555555555556) assert temperature_util.convert( - fahrenheit, TEMP_FAHRENHEIT, TEMP_KELVIN, True + fahrenheit, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.KELVIN, True ) == pytest.approx(55.55555555555556) def test_convert_from_kelvin() -> None: """Test conversion from K to other units.""" kelvin = 100 - assert temperature_util.convert(kelvin, TEMP_KELVIN, TEMP_CELSIUS) == pytest.approx( - -173.15 - ) assert temperature_util.convert( - kelvin, TEMP_KELVIN, TEMP_FAHRENHEIT + kelvin, UnitOfTemperature.KELVIN, UnitOfTemperature.CELSIUS + ) == pytest.approx(-173.15) + assert temperature_util.convert( + kelvin, UnitOfTemperature.KELVIN, UnitOfTemperature.FAHRENHEIT ) == pytest.approx(-279.66999999999996) # Interval assert temperature_util.convert( - kelvin, TEMP_KELVIN, TEMP_FAHRENHEIT, True + kelvin, UnitOfTemperature.KELVIN, UnitOfTemperature.FAHRENHEIT, True ) == pytest.approx(180.0) assert temperature_util.convert( - kelvin, TEMP_KELVIN, TEMP_KELVIN, True + kelvin, UnitOfTemperature.KELVIN, UnitOfTemperature.KELVIN, True ) == pytest.approx(100)