1
mirror of https://github.com/home-assistant/core synced 2024-07-21 14:24:50 +02:00

Refactored deprecated UNITS (#97368)

This commit is contained in:
Meow 2023-08-03 21:12:01 +02:00 committed by GitHub
parent e7e68907fa
commit 6fd60024cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 165 additions and 128 deletions

View File

@ -1,6 +1,6 @@
"""Make sure that an Arlo Baby can be setup.""" """Make sure that an Arlo Baby can be setup."""
from homeassistant.components.sensor import SensorStateClass 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 homeassistant.core import HomeAssistant
from ..common import ( 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", unique_id="00:00:00:00:00:00_1_1000",
friendly_name="ArloBabyA0 Temperature", friendly_name="ArloBabyA0 Temperature",
capabilities={"state_class": SensorStateClass.MEASUREMENT}, capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=TEMP_CELSIUS, unit_of_measurement=UnitOfTemperature.CELSIUS,
state="24.0", state="24.0",
), ),
EntityTestInfo( EntityTestInfo(

View File

@ -15,7 +15,7 @@ from homeassistant.components.climate import (
) )
from homeassistant.components.sensor import SensorStateClass from homeassistant.components.sensor import SensorStateClass
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import TEMP_CELSIUS from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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", friendly_name="HomeW Current Temperature",
unique_id="00:00:00:00:00:00_1_16_19", unique_id="00:00:00:00:00:00_1_16_19",
capabilities={"state_class": SensorStateClass.MEASUREMENT}, capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=TEMP_CELSIUS, unit_of_measurement=UnitOfTemperature.CELSIUS,
state="21.8", state="21.8",
), ),
EntityTestInfo( EntityTestInfo(

View File

@ -1,7 +1,12 @@
"""Make sure that Eve Degree (via Eve Extend) is enumerated properly.""" """Make sure that Eve Degree (via Eve Extend) is enumerated properly."""
from homeassistant.components.number import NumberMode from homeassistant.components.number import NumberMode
from homeassistant.components.sensor import SensorStateClass 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 homeassistant.core import HomeAssistant
from ..common import ( 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", unique_id="00:00:00:00:00:00_1_22",
friendly_name="Eve Degree AA11 Temperature", friendly_name="Eve Degree AA11 Temperature",
capabilities={"state_class": SensorStateClass.MEASUREMENT}, capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=TEMP_CELSIUS, unit_of_measurement=UnitOfTemperature.CELSIUS,
state="22.7719116210938", state="22.7719116210938",
), ),
EntityTestInfo( EntityTestInfo(

View File

@ -1,7 +1,7 @@
"""Make sure that Mysa Living is enumerated properly.""" """Make sure that Mysa Living is enumerated properly."""
from homeassistant.components.climate import ClimateEntityFeature from homeassistant.components.climate import ClimateEntityFeature
from homeassistant.components.sensor import SensorStateClass 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 homeassistant.core import HomeAssistant
from ..common import ( from ..common import (
@ -55,7 +55,7 @@ async def test_mysa_living_setup(hass: HomeAssistant) -> None:
entity_id="sensor.mysa_85dda9_current_temperature", entity_id="sensor.mysa_85dda9_current_temperature",
friendly_name="Mysa-85dda9 Current Temperature", friendly_name="Mysa-85dda9 Current Temperature",
unique_id="00:00:00:00:00:00_1_20_25", 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}, capabilities={"state_class": SensorStateClass.MEASUREMENT},
state="24.1", state="24.1",
), ),

View File

@ -7,7 +7,7 @@ from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import ( from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE, PERCENTAGE,
TEMP_CELSIUS, UnitOfTemperature,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -73,7 +73,7 @@ async def test_velux_cover_setup(hass: HomeAssistant) -> None:
friendly_name="VELUX Sensor Temperature sensor", friendly_name="VELUX Sensor Temperature sensor",
capabilities={"state_class": SensorStateClass.MEASUREMENT}, capabilities={"state_class": SensorStateClass.MEASUREMENT},
unique_id="00:00:00:00:00:00_2_8", unique_id="00:00:00:00:00:00_2_8",
unit_of_measurement=TEMP_CELSIUS, unit_of_measurement=UnitOfTemperature.CELSIUS,
state="18.9", state="18.9",
), ),
EntityTestInfo( EntityTestInfo(

View File

@ -5,8 +5,7 @@ from homeassistant.const import (
PRECISION_HALVES, PRECISION_HALVES,
PRECISION_TENTHS, PRECISION_TENTHS,
PRECISION_WHOLE, PRECISION_WHOLE,
TEMP_CELSIUS, UnitOfTemperature,
TEMP_FAHRENHEIT,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.temperature import display_temp 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.""" """Test that temperature is a number."""
temp = "Temperature" temp = "Temperature"
with pytest.raises(Exception) as exception: 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) assert f"Temperature is not a number: {temp}" in str(exception.value)
def test_celsius_halves(hass: HomeAssistant) -> None: def test_celsius_halves(hass: HomeAssistant) -> None:
"""Test temperature to celsius rounding to halves.""" """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: def test_celsius_tenths(hass: HomeAssistant) -> None:
"""Test temperature to celsius rounding to tenths.""" """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: def test_fahrenheit_wholes(hass: HomeAssistant) -> None:
"""Test temperature to fahrenheit rounding to wholes.""" """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

View File

@ -19,15 +19,15 @@ from homeassistant.components import group
from homeassistant.config import async_process_ha_core_config from homeassistant.config import async_process_ha_core_config
from homeassistant.const import ( from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, ATTR_UNIT_OF_MEASUREMENT,
LENGTH_METERS,
LENGTH_MILLIMETERS,
MASS_GRAMS,
STATE_ON, STATE_ON,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
TEMP_CELSIUS,
VOLUME_LITERS, VOLUME_LITERS,
UnitOfLength,
UnitOfMass,
UnitOfPrecipitationDepth,
UnitOfPressure, UnitOfPressure,
UnitOfSpeed, UnitOfSpeed,
UnitOfTemperature,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
@ -52,12 +52,12 @@ def _set_up_units(hass: HomeAssistant) -> None:
"""Set up the tests.""" """Set up the tests."""
hass.config.units = UnitSystem( hass.config.units = UnitSystem(
"custom", "custom",
accumulated_precipitation=LENGTH_MILLIMETERS, accumulated_precipitation=UnitOfPrecipitationDepth.MILLIMETERS,
conversions={}, conversions={},
length=LENGTH_METERS, length=UnitOfLength.METERS,
mass=MASS_GRAMS, mass=UnitOfMass.GRAMS,
pressure=UnitOfPressure.PA, pressure=UnitOfPressure.PA,
temperature=TEMP_CELSIUS, temperature=UnitOfTemperature.CELSIUS,
volume=VOLUME_LITERS, volume=VOLUME_LITERS,
wind_speed=UnitOfSpeed.KILOMETERS_PER_HOUR, wind_speed=UnitOfSpeed.KILOMETERS_PER_HOUR,
) )

View File

@ -3,38 +3,39 @@
import pytest import pytest
from homeassistant.const import ( from homeassistant.const import (
LENGTH_CENTIMETERS, UnitOfLength,
LENGTH_FEET,
LENGTH_INCHES,
LENGTH_KILOMETERS,
LENGTH_METERS,
LENGTH_MILES,
LENGTH_MILLIMETERS,
LENGTH_YARD,
) )
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
import homeassistant.util.distance as distance_util import homeassistant.util.distance as distance_util
INVALID_SYMBOL = "bob" INVALID_SYMBOL = "bob"
VALID_SYMBOL = LENGTH_KILOMETERS VALID_SYMBOL = UnitOfLength.KILOMETERS
def test_raise_deprecation_warning(caplog: pytest.LogCaptureFixture) -> None: def test_raise_deprecation_warning(caplog: pytest.LogCaptureFixture) -> None:
"""Ensure that a warning is raised on use of convert.""" """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 assert "use unit_conversion.DistanceConverter instead" in caplog.text
def test_convert_same_unit() -> None: def test_convert_same_unit() -> None:
"""Test conversion from any unit to same unit.""" """Test conversion from any unit to same unit."""
assert distance_util.convert(5, LENGTH_KILOMETERS, LENGTH_KILOMETERS) == 5 assert (
assert distance_util.convert(2, LENGTH_METERS, LENGTH_METERS) == 2 distance_util.convert(5, UnitOfLength.KILOMETERS, UnitOfLength.KILOMETERS) == 5
assert distance_util.convert(6, LENGTH_CENTIMETERS, LENGTH_CENTIMETERS) == 6 )
assert distance_util.convert(3, LENGTH_MILLIMETERS, LENGTH_MILLIMETERS) == 3 assert distance_util.convert(2, UnitOfLength.METERS, UnitOfLength.METERS) == 2
assert distance_util.convert(10, LENGTH_MILES, LENGTH_MILES) == 10 assert (
assert distance_util.convert(9, LENGTH_YARD, LENGTH_YARD) == 9 distance_util.convert(6, UnitOfLength.CENTIMETERS, UnitOfLength.CENTIMETERS)
assert distance_util.convert(8, LENGTH_FEET, LENGTH_FEET) == 8 == 6
assert distance_util.convert(7, LENGTH_INCHES, LENGTH_INCHES) == 7 )
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: def test_convert_invalid_unit() -> None:
@ -49,133 +50,145 @@ def test_convert_invalid_unit() -> None:
def test_convert_nonnumeric_value() -> None: def test_convert_nonnumeric_value() -> None:
"""Test exception is thrown for nonnumeric type.""" """Test exception is thrown for nonnumeric type."""
with pytest.raises(TypeError): with pytest.raises(TypeError):
distance_util.convert("a", LENGTH_KILOMETERS, LENGTH_METERS) distance_util.convert("a", UnitOfLength.KILOMETERS, UnitOfLength.METERS)
@pytest.mark.parametrize( @pytest.mark.parametrize(
("unit", "expected"), ("unit", "expected"),
[ [
(LENGTH_KILOMETERS, 8.04672), (UnitOfLength.KILOMETERS, 8.04672),
(LENGTH_METERS, 8046.72), (UnitOfLength.METERS, 8046.72),
(LENGTH_CENTIMETERS, 804672.0), (UnitOfLength.CENTIMETERS, 804672.0),
(LENGTH_MILLIMETERS, 8046720.0), (UnitOfLength.MILLIMETERS, 8046720.0),
(LENGTH_YARD, 8800.0), (UnitOfLength.YARDS, 8800.0),
(LENGTH_FEET, 26400.0008448), (UnitOfLength.FEET, 26400.0008448),
(LENGTH_INCHES, 316800.171072), (UnitOfLength.INCHES, 316800.171072),
], ],
) )
def test_convert_from_miles(unit, expected) -> None: def test_convert_from_miles(unit, expected) -> None:
"""Test conversion from miles to other units.""" """Test conversion from miles to other units."""
miles = 5 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( @pytest.mark.parametrize(
("unit", "expected"), ("unit", "expected"),
[ [
(LENGTH_KILOMETERS, 0.0045720000000000005), (UnitOfLength.KILOMETERS, 0.0045720000000000005),
(LENGTH_METERS, 4.572), (UnitOfLength.METERS, 4.572),
(LENGTH_CENTIMETERS, 457.2), (UnitOfLength.CENTIMETERS, 457.2),
(LENGTH_MILLIMETERS, 4572), (UnitOfLength.MILLIMETERS, 4572),
(LENGTH_MILES, 0.002840908212), (UnitOfLength.MILES, 0.002840908212),
(LENGTH_FEET, 15.00000048), (UnitOfLength.FEET, 15.00000048),
(LENGTH_INCHES, 180.0000972), (UnitOfLength.INCHES, 180.0000972),
], ],
) )
def test_convert_from_yards(unit, expected) -> None: def test_convert_from_yards(unit, expected) -> None:
"""Test conversion from yards to other units.""" """Test conversion from yards to other units."""
yards = 5 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( @pytest.mark.parametrize(
("unit", "expected"), ("unit", "expected"),
[ [
(LENGTH_KILOMETERS, 1.524), (UnitOfLength.KILOMETERS, 1.524),
(LENGTH_METERS, 1524), (UnitOfLength.METERS, 1524),
(LENGTH_CENTIMETERS, 152400.0), (UnitOfLength.CENTIMETERS, 152400.0),
(LENGTH_MILLIMETERS, 1524000.0), (UnitOfLength.MILLIMETERS, 1524000.0),
(LENGTH_MILES, 0.9469694040000001), (UnitOfLength.MILES, 0.9469694040000001),
(LENGTH_YARD, 1666.66667), (UnitOfLength.YARDS, 1666.66667),
(LENGTH_INCHES, 60000.032400000004), (UnitOfLength.INCHES, 60000.032400000004),
], ],
) )
def test_convert_from_feet(unit, expected) -> None: def test_convert_from_feet(unit, expected) -> None:
"""Test conversion from feet to other units.""" """Test conversion from feet to other units."""
feet = 5000 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( @pytest.mark.parametrize(
("unit", "expected"), ("unit", "expected"),
[ [
(LENGTH_KILOMETERS, 0.127), (UnitOfLength.KILOMETERS, 0.127),
(LENGTH_METERS, 127.0), (UnitOfLength.METERS, 127.0),
(LENGTH_CENTIMETERS, 12700.0), (UnitOfLength.CENTIMETERS, 12700.0),
(LENGTH_MILLIMETERS, 127000.0), (UnitOfLength.MILLIMETERS, 127000.0),
(LENGTH_MILES, 0.078914117), (UnitOfLength.MILES, 0.078914117),
(LENGTH_YARD, 138.88889), (UnitOfLength.YARDS, 138.88889),
(LENGTH_FEET, 416.66668), (UnitOfLength.FEET, 416.66668),
], ],
) )
def test_convert_from_inches(unit, expected) -> None: def test_convert_from_inches(unit, expected) -> None:
"""Test conversion from inches to other units.""" """Test conversion from inches to other units."""
inches = 5000 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( @pytest.mark.parametrize(
("unit", "expected"), ("unit", "expected"),
[ [
(LENGTH_METERS, 5000), (UnitOfLength.METERS, 5000),
(LENGTH_CENTIMETERS, 500000), (UnitOfLength.CENTIMETERS, 500000),
(LENGTH_MILLIMETERS, 5000000), (UnitOfLength.MILLIMETERS, 5000000),
(LENGTH_MILES, 3.106855), (UnitOfLength.MILES, 3.106855),
(LENGTH_YARD, 5468.066), (UnitOfLength.YARDS, 5468.066),
(LENGTH_FEET, 16404.2), (UnitOfLength.FEET, 16404.2),
(LENGTH_INCHES, 196850.5), (UnitOfLength.INCHES, 196850.5),
], ],
) )
def test_convert_from_kilometers(unit, expected) -> None: def test_convert_from_kilometers(unit, expected) -> None:
"""Test conversion from kilometers to other units.""" """Test conversion from kilometers to other units."""
km = 5 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( @pytest.mark.parametrize(
("unit", "expected"), ("unit", "expected"),
[ [
(LENGTH_KILOMETERS, 5), (UnitOfLength.KILOMETERS, 5),
(LENGTH_CENTIMETERS, 500000), (UnitOfLength.CENTIMETERS, 500000),
(LENGTH_MILLIMETERS, 5000000), (UnitOfLength.MILLIMETERS, 5000000),
(LENGTH_MILES, 3.106855), (UnitOfLength.MILES, 3.106855),
(LENGTH_YARD, 5468.066), (UnitOfLength.YARDS, 5468.066),
(LENGTH_FEET, 16404.2), (UnitOfLength.FEET, 16404.2),
(LENGTH_INCHES, 196850.5), (UnitOfLength.INCHES, 196850.5),
], ],
) )
def test_convert_from_meters(unit, expected) -> None: def test_convert_from_meters(unit, expected) -> None:
"""Test conversion from meters to other units.""" """Test conversion from meters to other units."""
m = 5000 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( @pytest.mark.parametrize(
("unit", "expected"), ("unit", "expected"),
[ [
(LENGTH_KILOMETERS, 5), (UnitOfLength.KILOMETERS, 5),
(LENGTH_METERS, 5000), (UnitOfLength.METERS, 5000),
(LENGTH_MILLIMETERS, 5000000), (UnitOfLength.MILLIMETERS, 5000000),
(LENGTH_MILES, 3.106855), (UnitOfLength.MILES, 3.106855),
(LENGTH_YARD, 5468.066), (UnitOfLength.YARDS, 5468.066),
(LENGTH_FEET, 16404.2), (UnitOfLength.FEET, 16404.2),
(LENGTH_INCHES, 196850.5), (UnitOfLength.INCHES, 196850.5),
], ],
) )
def test_convert_from_centimeters(unit, expected) -> None: def test_convert_from_centimeters(unit, expected) -> None:
"""Test conversion from centimeters to other units.""" """Test conversion from centimeters to other units."""
cm = 500000 cm = 500000
assert distance_util.convert(cm, LENGTH_CENTIMETERS, unit) == pytest.approx( assert distance_util.convert(cm, UnitOfLength.CENTIMETERS, unit) == pytest.approx(
expected expected
) )
@ -183,18 +196,18 @@ def test_convert_from_centimeters(unit, expected) -> None:
@pytest.mark.parametrize( @pytest.mark.parametrize(
("unit", "expected"), ("unit", "expected"),
[ [
(LENGTH_KILOMETERS, 5), (UnitOfLength.KILOMETERS, 5),
(LENGTH_METERS, 5000), (UnitOfLength.METERS, 5000),
(LENGTH_CENTIMETERS, 500000), (UnitOfLength.CENTIMETERS, 500000),
(LENGTH_MILES, 3.106855), (UnitOfLength.MILES, 3.106855),
(LENGTH_YARD, 5468.066), (UnitOfLength.YARDS, 5468.066),
(LENGTH_FEET, 16404.2), (UnitOfLength.FEET, 16404.2),
(LENGTH_INCHES, 196850.5), (UnitOfLength.INCHES, 196850.5),
], ],
) )
def test_convert_from_millimeters(unit, expected) -> None: def test_convert_from_millimeters(unit, expected) -> None:
"""Test conversion from millimeters to other units.""" """Test conversion from millimeters to other units."""
mm = 5000000 mm = 5000000
assert distance_util.convert(mm, LENGTH_MILLIMETERS, unit) == pytest.approx( assert distance_util.convert(mm, UnitOfLength.MILLIMETERS, unit) == pytest.approx(
expected expected
) )

View File

@ -1,17 +1,22 @@
"""Test Home Assistant temperature utility functions.""" """Test Home Assistant temperature utility functions."""
import pytest import pytest
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT, TEMP_KELVIN from homeassistant.const import UnitOfTemperature
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
import homeassistant.util.temperature as temperature_util import homeassistant.util.temperature as temperature_util
INVALID_SYMBOL = "bob" INVALID_SYMBOL = "bob"
VALID_SYMBOL = TEMP_CELSIUS VALID_SYMBOL = UnitOfTemperature.CELSIUS
def test_raise_deprecation_warning(caplog: pytest.LogCaptureFixture) -> None: def test_raise_deprecation_warning(caplog: pytest.LogCaptureFixture) -> None:
"""Ensure that a warning is raised on use of convert.""" """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 assert "use unit_conversion.TemperatureConverter instead" in caplog.text
@ -34,9 +39,22 @@ def test_deprecated_functions(
def test_convert_same_unit() -> None: def test_convert_same_unit() -> None:
"""Test conversion from any unit to same unit.""" """Test conversion from any unit to same unit."""
assert temperature_util.convert(2, TEMP_CELSIUS, TEMP_CELSIUS) == 2 assert (
assert temperature_util.convert(3, TEMP_FAHRENHEIT, TEMP_FAHRENHEIT) == 3 temperature_util.convert(
assert temperature_util.convert(4, TEMP_KELVIN, TEMP_KELVIN) == 4 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: def test_convert_invalid_unit() -> None:
@ -51,24 +69,26 @@ def test_convert_invalid_unit() -> None:
def test_convert_nonnumeric_value() -> None: def test_convert_nonnumeric_value() -> None:
"""Test exception is thrown for nonnumeric type.""" """Test exception is thrown for nonnumeric type."""
with pytest.raises(TypeError): 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: def test_convert_from_celsius() -> None:
"""Test conversion from C to other units.""" """Test conversion from C to other units."""
celsius = 100 celsius = 100
assert temperature_util.convert( assert temperature_util.convert(
celsius, TEMP_CELSIUS, TEMP_FAHRENHEIT celsius, UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT
) == pytest.approx(212.0) ) == pytest.approx(212.0)
assert temperature_util.convert( assert temperature_util.convert(
celsius, TEMP_CELSIUS, TEMP_KELVIN celsius, UnitOfTemperature.CELSIUS, UnitOfTemperature.KELVIN
) == pytest.approx(373.15) ) == pytest.approx(373.15)
# Interval # Interval
assert temperature_util.convert( assert temperature_util.convert(
celsius, TEMP_CELSIUS, TEMP_FAHRENHEIT, True celsius, UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT, True
) == pytest.approx(180.0) ) == pytest.approx(180.0)
assert temperature_util.convert( assert temperature_util.convert(
celsius, TEMP_CELSIUS, TEMP_KELVIN, True celsius, UnitOfTemperature.CELSIUS, UnitOfTemperature.KELVIN, True
) == pytest.approx(100) ) == pytest.approx(100)
@ -76,33 +96,33 @@ def test_convert_from_fahrenheit() -> None:
"""Test conversion from F to other units.""" """Test conversion from F to other units."""
fahrenheit = 100 fahrenheit = 100
assert temperature_util.convert( assert temperature_util.convert(
fahrenheit, TEMP_FAHRENHEIT, TEMP_CELSIUS fahrenheit, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.CELSIUS
) == pytest.approx(37.77777777777778) ) == pytest.approx(37.77777777777778)
assert temperature_util.convert( assert temperature_util.convert(
fahrenheit, TEMP_FAHRENHEIT, TEMP_KELVIN fahrenheit, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.KELVIN
) == pytest.approx(310.92777777777775) ) == pytest.approx(310.92777777777775)
# Interval # Interval
assert temperature_util.convert( assert temperature_util.convert(
fahrenheit, TEMP_FAHRENHEIT, TEMP_CELSIUS, True fahrenheit, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.CELSIUS, True
) == pytest.approx(55.55555555555556) ) == pytest.approx(55.55555555555556)
assert temperature_util.convert( assert temperature_util.convert(
fahrenheit, TEMP_FAHRENHEIT, TEMP_KELVIN, True fahrenheit, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.KELVIN, True
) == pytest.approx(55.55555555555556) ) == pytest.approx(55.55555555555556)
def test_convert_from_kelvin() -> None: def test_convert_from_kelvin() -> None:
"""Test conversion from K to other units.""" """Test conversion from K to other units."""
kelvin = 100 kelvin = 100
assert temperature_util.convert(kelvin, TEMP_KELVIN, TEMP_CELSIUS) == pytest.approx(
-173.15
)
assert temperature_util.convert( 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) ) == pytest.approx(-279.66999999999996)
# Interval # Interval
assert temperature_util.convert( assert temperature_util.convert(
kelvin, TEMP_KELVIN, TEMP_FAHRENHEIT, True kelvin, UnitOfTemperature.KELVIN, UnitOfTemperature.FAHRENHEIT, True
) == pytest.approx(180.0) ) == pytest.approx(180.0)
assert temperature_util.convert( assert temperature_util.convert(
kelvin, TEMP_KELVIN, TEMP_KELVIN, True kelvin, UnitOfTemperature.KELVIN, UnitOfTemperature.KELVIN, True
) == pytest.approx(100) ) == pytest.approx(100)