2016-03-07 22:20:48 +00:00
|
|
|
"""Temperature util functions."""
|
2022-12-21 14:24:11 +00:00
|
|
|
# pylint: disable-next=unused-import,hass-deprecated-import
|
|
|
|
from homeassistant.const import ( # noqa: F401
|
2019-07-31 19:25:30 +00:00
|
|
|
TEMP_CELSIUS,
|
|
|
|
TEMP_FAHRENHEIT,
|
2021-06-30 12:17:58 +00:00
|
|
|
TEMP_KELVIN,
|
2019-07-31 19:25:30 +00:00
|
|
|
TEMPERATURE,
|
2019-12-09 15:42:10 +00:00
|
|
|
UNIT_NOT_RECOGNIZED_TEMPLATE,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-09-28 17:39:44 +00:00
|
|
|
from homeassistant.helpers.frame import report
|
2015-08-17 04:36:33 +00:00
|
|
|
|
2022-09-22 16:31:50 +00:00
|
|
|
from .unit_conversion import TemperatureConverter
|
2022-03-30 13:43:04 +00:00
|
|
|
|
2022-09-22 16:31:50 +00:00
|
|
|
VALID_UNITS = TemperatureConverter.VALID_UNITS
|
2022-09-22 06:50:08 +00:00
|
|
|
|
2016-04-20 03:30:44 +00:00
|
|
|
|
2018-03-30 06:49:08 +00:00
|
|
|
def fahrenheit_to_celsius(fahrenheit: float, interval: bool = False) -> float:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Convert a temperature in Fahrenheit to Celsius."""
|
2022-09-28 17:39:44 +00:00
|
|
|
return convert(fahrenheit, TEMP_FAHRENHEIT, TEMP_CELSIUS, interval)
|
2015-08-17 04:36:33 +00:00
|
|
|
|
|
|
|
|
2021-06-30 12:17:58 +00:00
|
|
|
def kelvin_to_celsius(kelvin: float, interval: bool = False) -> float:
|
|
|
|
"""Convert a temperature in Kelvin to Celsius."""
|
2022-09-28 17:39:44 +00:00
|
|
|
return convert(kelvin, TEMP_KELVIN, TEMP_CELSIUS, interval)
|
2021-06-30 12:17:58 +00:00
|
|
|
|
|
|
|
|
2018-03-30 06:49:08 +00:00
|
|
|
def celsius_to_fahrenheit(celsius: float, interval: bool = False) -> float:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Convert a temperature in Celsius to Fahrenheit."""
|
2022-09-28 17:39:44 +00:00
|
|
|
return convert(celsius, TEMP_CELSIUS, TEMP_FAHRENHEIT, interval)
|
2016-07-31 20:24:49 +00:00
|
|
|
|
|
|
|
|
2021-06-30 12:17:58 +00:00
|
|
|
def celsius_to_kelvin(celsius: float, interval: bool = False) -> float:
|
|
|
|
"""Convert a temperature in Celsius to Fahrenheit."""
|
2022-09-28 17:39:44 +00:00
|
|
|
return convert(celsius, TEMP_CELSIUS, TEMP_KELVIN, interval)
|
2021-06-30 12:17:58 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def convert(
|
2021-07-20 12:13:51 +00:00
|
|
|
temperature: float, from_unit: str, to_unit: str, interval: bool = False
|
2019-07-31 19:25:30 +00:00
|
|
|
) -> float:
|
2016-07-31 20:24:49 +00:00
|
|
|
"""Convert a temperature from one unit to another."""
|
2022-09-28 17:39:44 +00:00
|
|
|
report(
|
|
|
|
"uses temperature utility. This is deprecated since 2022.10 and will "
|
2022-10-12 22:06:23 +00:00
|
|
|
"stop working in Home Assistant 2023.4, it should be updated to use "
|
2022-09-28 17:39:44 +00:00
|
|
|
"unit_conversion.TemperatureConverter instead",
|
|
|
|
error_if_core=False,
|
|
|
|
)
|
2022-10-03 08:09:55 +00:00
|
|
|
if interval:
|
|
|
|
return TemperatureConverter.convert_interval(temperature, from_unit, to_unit)
|
|
|
|
return TemperatureConverter.convert(temperature, from_unit, to_unit)
|