2016-03-07 22:20:48 +00:00
|
|
|
"""Temperature util functions."""
|
2016-07-31 20:24:49 +00:00
|
|
|
from homeassistant.const import (
|
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
|
|
|
)
|
2015-08-17 04:36:33 +00:00
|
|
|
|
2022-03-30 13:43:04 +00:00
|
|
|
VALID_UNITS: tuple[str, ...] = (
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
TEMP_KELVIN,
|
|
|
|
)
|
|
|
|
|
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."""
|
2018-03-30 06:49:08 +00:00
|
|
|
if interval:
|
|
|
|
return fahrenheit / 1.8
|
2015-08-17 04:36:33 +00:00
|
|
|
return (fahrenheit - 32.0) / 1.8
|
|
|
|
|
|
|
|
|
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."""
|
|
|
|
if interval:
|
|
|
|
return kelvin
|
|
|
|
return kelvin - 273.15
|
|
|
|
|
|
|
|
|
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."""
|
2018-03-30 06:49:08 +00:00
|
|
|
if interval:
|
|
|
|
return celsius * 1.8
|
2016-04-20 03:30:44 +00:00
|
|
|
return celsius * 1.8 + 32.0
|
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."""
|
|
|
|
if interval:
|
|
|
|
return celsius
|
|
|
|
return celsius + 273.15
|
|
|
|
|
|
|
|
|
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."""
|
2021-06-30 12:17:58 +00:00
|
|
|
if from_unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT, TEMP_KELVIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(from_unit, TEMPERATURE))
|
2021-06-30 12:17:58 +00:00
|
|
|
if to_unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT, TEMP_KELVIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise ValueError(UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, TEMPERATURE))
|
2016-07-31 20:24:49 +00:00
|
|
|
|
|
|
|
if from_unit == to_unit:
|
|
|
|
return temperature
|
2021-06-30 12:17:58 +00:00
|
|
|
|
2018-07-23 08:16:05 +00:00
|
|
|
if from_unit == TEMP_CELSIUS:
|
2021-06-30 12:17:58 +00:00
|
|
|
if to_unit == TEMP_FAHRENHEIT:
|
|
|
|
return celsius_to_fahrenheit(temperature, interval)
|
|
|
|
# kelvin
|
|
|
|
return celsius_to_kelvin(temperature, interval)
|
|
|
|
|
|
|
|
if from_unit == TEMP_FAHRENHEIT:
|
|
|
|
if to_unit == TEMP_CELSIUS:
|
|
|
|
return fahrenheit_to_celsius(temperature, interval)
|
|
|
|
# kelvin
|
|
|
|
return celsius_to_kelvin(fahrenheit_to_celsius(temperature, interval), interval)
|
|
|
|
|
|
|
|
# from_unit == kelvin
|
|
|
|
if to_unit == TEMP_CELSIUS:
|
|
|
|
return kelvin_to_celsius(temperature, interval)
|
|
|
|
# fahrenheit
|
|
|
|
return celsius_to_fahrenheit(kelvin_to_celsius(temperature, interval), interval)
|