Make temperature conversions private (#79349)

This commit is contained in:
epenet 2022-09-30 11:07:10 +02:00 committed by GitHub
parent fb7079c62c
commit ac7b4e7569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 19 deletions

View File

@ -22,6 +22,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.unit_conversion import TemperatureConverter
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -218,7 +219,7 @@ class MoldIndicator(SensorEntity):
# convert to celsius if necessary # convert to celsius if necessary
if unit == TEMP_FAHRENHEIT: if unit == TEMP_FAHRENHEIT:
return util.temperature.fahrenheit_to_celsius(temp) return TemperatureConverter.convert(temp, TEMP_FAHRENHEIT, TEMP_CELSIUS)
if unit == TEMP_CELSIUS: if unit == TEMP_CELSIUS:
return temp return temp
_LOGGER.error( _LOGGER.error(
@ -385,13 +386,13 @@ class MoldIndicator(SensorEntity):
} }
dewpoint = ( dewpoint = (
util.temperature.celsius_to_fahrenheit(self._dewpoint) TemperatureConverter.convert(self._dewpoint, TEMP_CELSIUS, TEMP_FAHRENHEIT)
if self._dewpoint is not None if self._dewpoint is not None
else None else None
) )
crit_temp = ( crit_temp = (
util.temperature.celsius_to_fahrenheit(self._crit_temp) TemperatureConverter.convert(self._crit_temp, TEMP_CELSIUS, TEMP_FAHRENHEIT)
if self._crit_temp is not None if self._crit_temp is not None
else None else None
) )

View File

@ -9,6 +9,7 @@ from homeassistant.components.weather import (
ATTR_CONDITION_CLEAR_NIGHT, ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_SUNNY, ATTR_CONDITION_SUNNY,
) )
from homeassistant.const import TEMP_CELSIUS, TEMP_KELVIN
from homeassistant.helpers import sun from homeassistant.helpers import sun
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util import dt from homeassistant.util import dt
@ -191,7 +192,9 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
def _fmt_dewpoint(dewpoint): def _fmt_dewpoint(dewpoint):
"""Format the dewpoint data.""" """Format the dewpoint data."""
if dewpoint is not None: if dewpoint is not None:
return round(TemperatureConverter.kelvin_to_celsius(dewpoint), 1) return round(
TemperatureConverter.convert(dewpoint, TEMP_KELVIN, TEMP_CELSIUS), 1
)
return None return None
@staticmethod @staticmethod

View File

@ -348,7 +348,9 @@ class PrometheusMetrics:
with suppress(ValueError): with suppress(ValueError):
value = self.state_as_number(state) value = self.state_as_number(state)
if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_FAHRENHEIT: if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_FAHRENHEIT:
value = TemperatureConverter.fahrenheit_to_celsius(value) value = TemperatureConverter.convert(
value, TEMP_FAHRENHEIT, TEMP_CELSIUS
)
metric.labels(**self._labels(state)).set(value) metric.labels(**self._labels(state)).set(value)
def _handle_device_tracker(self, state): def _handle_device_tracker(self, state):
@ -394,7 +396,7 @@ class PrometheusMetrics:
def _handle_climate_temp(self, state, attr, metric_name, metric_description): def _handle_climate_temp(self, state, attr, metric_name, metric_description):
if temp := state.attributes.get(attr): if temp := state.attributes.get(attr):
if self._climate_units == TEMP_FAHRENHEIT: if self._climate_units == TEMP_FAHRENHEIT:
temp = TemperatureConverter.fahrenheit_to_celsius(temp) temp = TemperatureConverter.convert(temp, TEMP_FAHRENHEIT, TEMP_CELSIUS)
metric = self._metric( metric = self._metric(
metric_name, metric_name,
self.prometheus_cli.Gauge, self.prometheus_cli.Gauge,
@ -507,7 +509,9 @@ class PrometheusMetrics:
try: try:
value = self.state_as_number(state) value = self.state_as_number(state)
if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_FAHRENHEIT: if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_FAHRENHEIT:
value = TemperatureConverter.fahrenheit_to_celsius(value) value = TemperatureConverter.convert(
value, TEMP_FAHRENHEIT, TEMP_CELSIUS
)
_metric.labels(**self._labels(state)).set(value) _metric.labels(**self._labels(state)).set(value)
except ValueError: except ValueError:
pass pass

View File

@ -297,19 +297,19 @@ class TemperatureConverter(BaseUnitConverter):
if from_unit == TEMP_CELSIUS: if from_unit == TEMP_CELSIUS:
if to_unit == TEMP_FAHRENHEIT: if to_unit == TEMP_FAHRENHEIT:
return cls.celsius_to_fahrenheit(value, interval) return cls._celsius_to_fahrenheit(value, interval)
if to_unit == TEMP_KELVIN: if to_unit == TEMP_KELVIN:
return cls.celsius_to_kelvin(value, interval) return cls._celsius_to_kelvin(value, interval)
raise HomeAssistantError( raise HomeAssistantError(
UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, cls.UNIT_CLASS) UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, cls.UNIT_CLASS)
) )
if from_unit == TEMP_FAHRENHEIT: if from_unit == TEMP_FAHRENHEIT:
if to_unit == TEMP_CELSIUS: if to_unit == TEMP_CELSIUS:
return cls.fahrenheit_to_celsius(value, interval) return cls._fahrenheit_to_celsius(value, interval)
if to_unit == TEMP_KELVIN: if to_unit == TEMP_KELVIN:
return cls.celsius_to_kelvin( return cls._celsius_to_kelvin(
cls.fahrenheit_to_celsius(value, interval), interval cls._fahrenheit_to_celsius(value, interval), interval
) )
raise HomeAssistantError( raise HomeAssistantError(
UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, cls.UNIT_CLASS) UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, cls.UNIT_CLASS)
@ -317,10 +317,10 @@ class TemperatureConverter(BaseUnitConverter):
if from_unit == TEMP_KELVIN: if from_unit == TEMP_KELVIN:
if to_unit == TEMP_CELSIUS: if to_unit == TEMP_CELSIUS:
return cls.kelvin_to_celsius(value, interval) return cls._kelvin_to_celsius(value, interval)
if to_unit == TEMP_FAHRENHEIT: if to_unit == TEMP_FAHRENHEIT:
return cls.celsius_to_fahrenheit( return cls._celsius_to_fahrenheit(
cls.kelvin_to_celsius(value, interval), interval cls._kelvin_to_celsius(value, interval), interval
) )
raise HomeAssistantError( raise HomeAssistantError(
UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, cls.UNIT_CLASS) UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, cls.UNIT_CLASS)
@ -330,28 +330,28 @@ class TemperatureConverter(BaseUnitConverter):
) )
@classmethod @classmethod
def fahrenheit_to_celsius(cls, fahrenheit: float, interval: bool = False) -> float: def _fahrenheit_to_celsius(cls, fahrenheit: float, interval: bool = False) -> float:
"""Convert a temperature in Fahrenheit to Celsius.""" """Convert a temperature in Fahrenheit to Celsius."""
if interval: if interval:
return fahrenheit / 1.8 return fahrenheit / 1.8
return (fahrenheit - 32.0) / 1.8 return (fahrenheit - 32.0) / 1.8
@classmethod @classmethod
def kelvin_to_celsius(cls, kelvin: float, interval: bool = False) -> float: def _kelvin_to_celsius(cls, kelvin: float, interval: bool = False) -> float:
"""Convert a temperature in Kelvin to Celsius.""" """Convert a temperature in Kelvin to Celsius."""
if interval: if interval:
return kelvin return kelvin
return kelvin - 273.15 return kelvin - 273.15
@classmethod @classmethod
def celsius_to_fahrenheit(cls, celsius: float, interval: bool = False) -> float: def _celsius_to_fahrenheit(cls, celsius: float, interval: bool = False) -> float:
"""Convert a temperature in Celsius to Fahrenheit.""" """Convert a temperature in Celsius to Fahrenheit."""
if interval: if interval:
return celsius * 1.8 return celsius * 1.8
return celsius * 1.8 + 32.0 return celsius * 1.8 + 32.0
@classmethod @classmethod
def celsius_to_kelvin(cls, celsius: float, interval: bool = False) -> float: def _celsius_to_kelvin(cls, celsius: float, interval: bool = False) -> float:
"""Convert a temperature in Celsius to Kelvin.""" """Convert a temperature in Celsius to Kelvin."""
if interval: if interval:
return celsius return celsius