From ac7b4e7569ee755fc294f6a8ddc0fa8c8d4b80e7 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 30 Sep 2022 11:07:10 +0200 Subject: [PATCH] Make temperature conversions private (#79349) --- .../components/mold_indicator/sensor.py | 7 +++--- .../weather_update_coordinator.py | 5 +++- .../components/prometheus/__init__.py | 10 +++++--- homeassistant/util/unit_conversion.py | 24 +++++++++---------- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/mold_indicator/sensor.py b/homeassistant/components/mold_indicator/sensor.py index 23c5e639d7f..5685e76fac0 100644 --- a/homeassistant/components/mold_indicator/sensor.py +++ b/homeassistant/components/mold_indicator/sensor.py @@ -22,6 +22,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from homeassistant.util.unit_conversion import TemperatureConverter _LOGGER = logging.getLogger(__name__) @@ -218,7 +219,7 @@ class MoldIndicator(SensorEntity): # convert to celsius if necessary if unit == TEMP_FAHRENHEIT: - return util.temperature.fahrenheit_to_celsius(temp) + return TemperatureConverter.convert(temp, TEMP_FAHRENHEIT, TEMP_CELSIUS) if unit == TEMP_CELSIUS: return temp _LOGGER.error( @@ -385,13 +386,13 @@ class MoldIndicator(SensorEntity): } dewpoint = ( - util.temperature.celsius_to_fahrenheit(self._dewpoint) + TemperatureConverter.convert(self._dewpoint, TEMP_CELSIUS, TEMP_FAHRENHEIT) if self._dewpoint is not None else None ) 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 else None ) diff --git a/homeassistant/components/openweathermap/weather_update_coordinator.py b/homeassistant/components/openweathermap/weather_update_coordinator.py index b5435c92680..630250b4701 100644 --- a/homeassistant/components/openweathermap/weather_update_coordinator.py +++ b/homeassistant/components/openweathermap/weather_update_coordinator.py @@ -9,6 +9,7 @@ from homeassistant.components.weather import ( ATTR_CONDITION_CLEAR_NIGHT, ATTR_CONDITION_SUNNY, ) +from homeassistant.const import TEMP_CELSIUS, TEMP_KELVIN from homeassistant.helpers import sun from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.util import dt @@ -191,7 +192,9 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): def _fmt_dewpoint(dewpoint): """Format the dewpoint data.""" 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 @staticmethod diff --git a/homeassistant/components/prometheus/__init__.py b/homeassistant/components/prometheus/__init__.py index e5dcebd3ca9..c1573755e11 100644 --- a/homeassistant/components/prometheus/__init__.py +++ b/homeassistant/components/prometheus/__init__.py @@ -348,7 +348,9 @@ class PrometheusMetrics: with suppress(ValueError): value = self.state_as_number(state) 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) def _handle_device_tracker(self, state): @@ -394,7 +396,7 @@ class PrometheusMetrics: def _handle_climate_temp(self, state, attr, metric_name, metric_description): if temp := state.attributes.get(attr): if self._climate_units == TEMP_FAHRENHEIT: - temp = TemperatureConverter.fahrenheit_to_celsius(temp) + temp = TemperatureConverter.convert(temp, TEMP_FAHRENHEIT, TEMP_CELSIUS) metric = self._metric( metric_name, self.prometheus_cli.Gauge, @@ -507,7 +509,9 @@ class PrometheusMetrics: try: value = self.state_as_number(state) 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) except ValueError: pass diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index 84a42487498..cb066901b37 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -297,19 +297,19 @@ class TemperatureConverter(BaseUnitConverter): if from_unit == TEMP_CELSIUS: if to_unit == TEMP_FAHRENHEIT: - return cls.celsius_to_fahrenheit(value, interval) + return cls._celsius_to_fahrenheit(value, interval) if to_unit == TEMP_KELVIN: - return cls.celsius_to_kelvin(value, interval) + return cls._celsius_to_kelvin(value, interval) raise HomeAssistantError( UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, cls.UNIT_CLASS) ) if from_unit == TEMP_FAHRENHEIT: if to_unit == TEMP_CELSIUS: - return cls.fahrenheit_to_celsius(value, interval) + return cls._fahrenheit_to_celsius(value, interval) if to_unit == TEMP_KELVIN: - return cls.celsius_to_kelvin( - cls.fahrenheit_to_celsius(value, interval), interval + return cls._celsius_to_kelvin( + cls._fahrenheit_to_celsius(value, interval), interval ) raise HomeAssistantError( UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, cls.UNIT_CLASS) @@ -317,10 +317,10 @@ class TemperatureConverter(BaseUnitConverter): if from_unit == TEMP_KELVIN: if to_unit == TEMP_CELSIUS: - return cls.kelvin_to_celsius(value, interval) + return cls._kelvin_to_celsius(value, interval) if to_unit == TEMP_FAHRENHEIT: - return cls.celsius_to_fahrenheit( - cls.kelvin_to_celsius(value, interval), interval + return cls._celsius_to_fahrenheit( + cls._kelvin_to_celsius(value, interval), interval ) raise HomeAssistantError( UNIT_NOT_RECOGNIZED_TEMPLATE.format(to_unit, cls.UNIT_CLASS) @@ -330,28 +330,28 @@ class TemperatureConverter(BaseUnitConverter): ) @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.""" if interval: return fahrenheit / 1.8 return (fahrenheit - 32.0) / 1.8 @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.""" if interval: return kelvin return kelvin - 273.15 @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.""" if interval: return celsius * 1.8 return celsius * 1.8 + 32.0 @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.""" if interval: return celsius