Remove deprecated temperature conversion functions (#101204)
This commit is contained in:
parent
efca5ba554
commit
8626a4888c
3 changed files with 0 additions and 187 deletions
|
@ -110,14 +110,6 @@ homeassistant.util.ssl
|
|||
:undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
homeassistant.util.temperature
|
||||
------------------------------
|
||||
|
||||
.. automodule:: homeassistant.util.temperature
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
homeassistant.util.unit\_system
|
||||
-------------------------------
|
||||
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
"""Temperature util functions."""
|
||||
# pylint: disable-next=hass-deprecated-import
|
||||
from homeassistant.const import ( # noqa: F401
|
||||
TEMP_CELSIUS,
|
||||
TEMP_FAHRENHEIT,
|
||||
TEMP_KELVIN,
|
||||
TEMPERATURE,
|
||||
UNIT_NOT_RECOGNIZED_TEMPLATE,
|
||||
)
|
||||
from homeassistant.helpers.frame import report
|
||||
|
||||
from .unit_conversion import TemperatureConverter
|
||||
|
||||
VALID_UNITS = TemperatureConverter.VALID_UNITS
|
||||
|
||||
|
||||
def fahrenheit_to_celsius(fahrenheit: float, interval: bool = False) -> float:
|
||||
"""Convert a temperature in Fahrenheit to Celsius."""
|
||||
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."""
|
||||
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."""
|
||||
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."""
|
||||
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."""
|
||||
report(
|
||||
(
|
||||
"uses temperature utility. This is deprecated since 2022.10 and will "
|
||||
"stop working in Home Assistant 2023.4, it should be updated to use "
|
||||
"unit_conversion.TemperatureConverter instead"
|
||||
),
|
||||
error_if_core=False,
|
||||
)
|
||||
if interval:
|
||||
return TemperatureConverter.convert_interval(temperature, from_unit, to_unit)
|
||||
return TemperatureConverter.convert(temperature, from_unit, to_unit)
|
|
@ -1,128 +0,0 @@
|
|||
"""Test Home Assistant temperature utility functions."""
|
||||
import pytest
|
||||
|
||||
from homeassistant.const import UnitOfTemperature
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
import homeassistant.util.temperature as temperature_util
|
||||
|
||||
INVALID_SYMBOL = "bob"
|
||||
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, UnitOfTemperature.CELSIUS, UnitOfTemperature.CELSIUS
|
||||
)
|
||||
== 2
|
||||
)
|
||||
assert "use unit_conversion.TemperatureConverter instead" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("function_name", "value", "expected"),
|
||||
[
|
||||
("fahrenheit_to_celsius", 75.2, 24),
|
||||
("kelvin_to_celsius", 297.65, 24.5),
|
||||
("celsius_to_fahrenheit", 23, 73.4),
|
||||
("celsius_to_kelvin", 23, 296.15),
|
||||
],
|
||||
)
|
||||
def test_deprecated_functions(
|
||||
function_name: str, value: float, expected: float
|
||||
) -> None:
|
||||
"""Test that deprecated function still work."""
|
||||
convert = getattr(temperature_util, function_name)
|
||||
assert convert(value) == expected
|
||||
|
||||
|
||||
def test_convert_same_unit() -> None:
|
||||
"""Test conversion from any unit to same unit."""
|
||||
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:
|
||||
"""Test exception is thrown for invalid units."""
|
||||
with pytest.raises(HomeAssistantError, match="is not a recognized .* unit"):
|
||||
temperature_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL)
|
||||
|
||||
with pytest.raises(HomeAssistantError, match="is not a recognized .* unit"):
|
||||
temperature_util.convert(5, VALID_SYMBOL, INVALID_SYMBOL)
|
||||
|
||||
|
||||
def test_convert_nonnumeric_value() -> None:
|
||||
"""Test exception is thrown for nonnumeric type."""
|
||||
with pytest.raises(TypeError):
|
||||
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, UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT
|
||||
) == pytest.approx(212.0)
|
||||
assert temperature_util.convert(
|
||||
celsius, UnitOfTemperature.CELSIUS, UnitOfTemperature.KELVIN
|
||||
) == pytest.approx(373.15)
|
||||
# Interval
|
||||
assert temperature_util.convert(
|
||||
celsius, UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT, True
|
||||
) == pytest.approx(180.0)
|
||||
assert temperature_util.convert(
|
||||
celsius, UnitOfTemperature.CELSIUS, UnitOfTemperature.KELVIN, True
|
||||
) == pytest.approx(100)
|
||||
|
||||
|
||||
def test_convert_from_fahrenheit() -> None:
|
||||
"""Test conversion from F to other units."""
|
||||
fahrenheit = 100
|
||||
assert temperature_util.convert(
|
||||
fahrenheit, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.CELSIUS
|
||||
) == pytest.approx(37.77777777777778)
|
||||
assert temperature_util.convert(
|
||||
fahrenheit, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.KELVIN
|
||||
) == pytest.approx(310.92777777777775)
|
||||
# Interval
|
||||
assert temperature_util.convert(
|
||||
fahrenheit, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.CELSIUS, True
|
||||
) == pytest.approx(55.55555555555556)
|
||||
assert temperature_util.convert(
|
||||
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, 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, UnitOfTemperature.KELVIN, UnitOfTemperature.FAHRENHEIT, True
|
||||
) == pytest.approx(180.0)
|
||||
assert temperature_util.convert(
|
||||
kelvin, UnitOfTemperature.KELVIN, UnitOfTemperature.KELVIN, True
|
||||
) == pytest.approx(100)
|
Loading…
Add table
Reference in a new issue