Use unit_conversion in components (#78991)
This commit is contained in:
parent
62022a2657
commit
dd7a06b9dc
5 changed files with 39 additions and 35 deletions
|
@ -68,11 +68,12 @@ from homeassistant.const import (
|
||||||
)
|
)
|
||||||
from homeassistant.core import DOMAIN as HA_DOMAIN
|
from homeassistant.core import DOMAIN as HA_DOMAIN
|
||||||
from homeassistant.helpers.network import get_url
|
from homeassistant.helpers.network import get_url
|
||||||
from homeassistant.util import color as color_util, dt, temperature as temp_util
|
from homeassistant.util import color as color_util, dt
|
||||||
from homeassistant.util.percentage import (
|
from homeassistant.util.percentage import (
|
||||||
ordered_list_item_to_percentage,
|
ordered_list_item_to_percentage,
|
||||||
percentage_to_ordered_list_item,
|
percentage_to_ordered_list_item,
|
||||||
)
|
)
|
||||||
|
from homeassistant.util.unit_conversion import TemperatureConverter
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CHALLENGE_ACK_NEEDED,
|
CHALLENGE_ACK_NEEDED,
|
||||||
|
@ -843,7 +844,9 @@ class TemperatureControlTrait(_Trait):
|
||||||
unit = self.hass.config.units.temperature_unit
|
unit = self.hass.config.units.temperature_unit
|
||||||
current_temp = self.state.state
|
current_temp = self.state.state
|
||||||
if current_temp not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
|
if current_temp not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
|
||||||
temp = round(temp_util.convert(float(current_temp), unit, TEMP_CELSIUS), 1)
|
temp = round(
|
||||||
|
TemperatureConverter.convert(float(current_temp), unit, TEMP_CELSIUS), 1
|
||||||
|
)
|
||||||
response["temperatureSetpointCelsius"] = temp
|
response["temperatureSetpointCelsius"] = temp
|
||||||
response["temperatureAmbientCelsius"] = temp
|
response["temperatureAmbientCelsius"] = temp
|
||||||
|
|
||||||
|
@ -948,7 +951,7 @@ class TemperatureSettingTrait(_Trait):
|
||||||
current_temp = attrs.get(climate.ATTR_CURRENT_TEMPERATURE)
|
current_temp = attrs.get(climate.ATTR_CURRENT_TEMPERATURE)
|
||||||
if current_temp is not None:
|
if current_temp is not None:
|
||||||
response["thermostatTemperatureAmbient"] = round(
|
response["thermostatTemperatureAmbient"] = round(
|
||||||
temp_util.convert(current_temp, unit, TEMP_CELSIUS), 1
|
TemperatureConverter.convert(current_temp, unit, TEMP_CELSIUS), 1
|
||||||
)
|
)
|
||||||
|
|
||||||
current_humidity = attrs.get(climate.ATTR_CURRENT_HUMIDITY)
|
current_humidity = attrs.get(climate.ATTR_CURRENT_HUMIDITY)
|
||||||
|
@ -958,13 +961,13 @@ class TemperatureSettingTrait(_Trait):
|
||||||
if operation in (climate.HVACMode.AUTO, climate.HVACMode.HEAT_COOL):
|
if operation in (climate.HVACMode.AUTO, climate.HVACMode.HEAT_COOL):
|
||||||
if supported & climate.SUPPORT_TARGET_TEMPERATURE_RANGE:
|
if supported & climate.SUPPORT_TARGET_TEMPERATURE_RANGE:
|
||||||
response["thermostatTemperatureSetpointHigh"] = round(
|
response["thermostatTemperatureSetpointHigh"] = round(
|
||||||
temp_util.convert(
|
TemperatureConverter.convert(
|
||||||
attrs[climate.ATTR_TARGET_TEMP_HIGH], unit, TEMP_CELSIUS
|
attrs[climate.ATTR_TARGET_TEMP_HIGH], unit, TEMP_CELSIUS
|
||||||
),
|
),
|
||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
response["thermostatTemperatureSetpointLow"] = round(
|
response["thermostatTemperatureSetpointLow"] = round(
|
||||||
temp_util.convert(
|
TemperatureConverter.convert(
|
||||||
attrs[climate.ATTR_TARGET_TEMP_LOW], unit, TEMP_CELSIUS
|
attrs[climate.ATTR_TARGET_TEMP_LOW], unit, TEMP_CELSIUS
|
||||||
),
|
),
|
||||||
1,
|
1,
|
||||||
|
@ -972,14 +975,14 @@ class TemperatureSettingTrait(_Trait):
|
||||||
else:
|
else:
|
||||||
if (target_temp := attrs.get(ATTR_TEMPERATURE)) is not None:
|
if (target_temp := attrs.get(ATTR_TEMPERATURE)) is not None:
|
||||||
target_temp = round(
|
target_temp = round(
|
||||||
temp_util.convert(target_temp, unit, TEMP_CELSIUS), 1
|
TemperatureConverter.convert(target_temp, unit, TEMP_CELSIUS), 1
|
||||||
)
|
)
|
||||||
response["thermostatTemperatureSetpointHigh"] = target_temp
|
response["thermostatTemperatureSetpointHigh"] = target_temp
|
||||||
response["thermostatTemperatureSetpointLow"] = target_temp
|
response["thermostatTemperatureSetpointLow"] = target_temp
|
||||||
else:
|
else:
|
||||||
if (target_temp := attrs.get(ATTR_TEMPERATURE)) is not None:
|
if (target_temp := attrs.get(ATTR_TEMPERATURE)) is not None:
|
||||||
response["thermostatTemperatureSetpoint"] = round(
|
response["thermostatTemperatureSetpoint"] = round(
|
||||||
temp_util.convert(target_temp, unit, TEMP_CELSIUS), 1
|
TemperatureConverter.convert(target_temp, unit, TEMP_CELSIUS), 1
|
||||||
)
|
)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
@ -992,7 +995,7 @@ class TemperatureSettingTrait(_Trait):
|
||||||
max_temp = self.state.attributes[climate.ATTR_MAX_TEMP]
|
max_temp = self.state.attributes[climate.ATTR_MAX_TEMP]
|
||||||
|
|
||||||
if command == COMMAND_THERMOSTAT_TEMPERATURE_SETPOINT:
|
if command == COMMAND_THERMOSTAT_TEMPERATURE_SETPOINT:
|
||||||
temp = temp_util.convert(
|
temp = TemperatureConverter.convert(
|
||||||
params["thermostatTemperatureSetpoint"], TEMP_CELSIUS, unit
|
params["thermostatTemperatureSetpoint"], TEMP_CELSIUS, unit
|
||||||
)
|
)
|
||||||
if unit == TEMP_FAHRENHEIT:
|
if unit == TEMP_FAHRENHEIT:
|
||||||
|
@ -1013,7 +1016,7 @@ class TemperatureSettingTrait(_Trait):
|
||||||
)
|
)
|
||||||
|
|
||||||
elif command == COMMAND_THERMOSTAT_TEMPERATURE_SET_RANGE:
|
elif command == COMMAND_THERMOSTAT_TEMPERATURE_SET_RANGE:
|
||||||
temp_high = temp_util.convert(
|
temp_high = TemperatureConverter.convert(
|
||||||
params["thermostatTemperatureSetpointHigh"], TEMP_CELSIUS, unit
|
params["thermostatTemperatureSetpointHigh"], TEMP_CELSIUS, unit
|
||||||
)
|
)
|
||||||
if unit == TEMP_FAHRENHEIT:
|
if unit == TEMP_FAHRENHEIT:
|
||||||
|
@ -1028,7 +1031,7 @@ class TemperatureSettingTrait(_Trait):
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
temp_low = temp_util.convert(
|
temp_low = TemperatureConverter.convert(
|
||||||
params["thermostatTemperatureSetpointLow"], TEMP_CELSIUS, unit
|
params["thermostatTemperatureSetpointLow"], TEMP_CELSIUS, unit
|
||||||
)
|
)
|
||||||
if unit == TEMP_FAHRENHEIT:
|
if unit == TEMP_FAHRENHEIT:
|
||||||
|
|
|
@ -40,7 +40,7 @@ from homeassistant.const import (
|
||||||
from homeassistant.core import Event, HomeAssistant, State, callback, split_entity_id
|
from homeassistant.core import Event, HomeAssistant, State, callback, split_entity_id
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.storage import STORAGE_DIR
|
from homeassistant.helpers.storage import STORAGE_DIR
|
||||||
import homeassistant.util.temperature as temp_util
|
from homeassistant.util.unit_conversion import TemperatureConverter
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
AUDIO_CODEC_COPY,
|
AUDIO_CODEC_COPY,
|
||||||
|
@ -391,12 +391,12 @@ def cleanup_name_for_homekit(name: str | None) -> str:
|
||||||
|
|
||||||
def temperature_to_homekit(temperature: float | int, unit: str) -> float:
|
def temperature_to_homekit(temperature: float | int, unit: str) -> float:
|
||||||
"""Convert temperature to Celsius for HomeKit."""
|
"""Convert temperature to Celsius for HomeKit."""
|
||||||
return round(temp_util.convert(temperature, unit, TEMP_CELSIUS), 1)
|
return round(TemperatureConverter.convert(temperature, unit, TEMP_CELSIUS), 1)
|
||||||
|
|
||||||
|
|
||||||
def temperature_to_states(temperature: float | int, unit: str) -> float:
|
def temperature_to_states(temperature: float | int, unit: str) -> float:
|
||||||
"""Convert temperature back from Celsius to Home Assistant unit."""
|
"""Convert temperature back from Celsius to Home Assistant unit."""
|
||||||
return round(temp_util.convert(temperature, TEMP_CELSIUS, unit) * 2) / 2
|
return round(TemperatureConverter.convert(temperature, TEMP_CELSIUS, unit) * 2) / 2
|
||||||
|
|
||||||
|
|
||||||
def density_to_air_quality(density: float) -> int:
|
def density_to_air_quality(density: float) -> int:
|
||||||
|
|
|
@ -57,7 +57,8 @@ from homeassistant.helpers.device_registry import DeviceEntryType
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.event import async_call_later
|
from homeassistant.helpers.event import async_call_later
|
||||||
from homeassistant.util import Throttle, slugify, speed as speed_util
|
from homeassistant.util import Throttle, slugify
|
||||||
|
from homeassistant.util.unit_conversion import SpeedConverter
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_SMHI_CLOUDINESS,
|
ATTR_SMHI_CLOUDINESS,
|
||||||
|
@ -155,7 +156,7 @@ class SmhiWeather(WeatherEntity):
|
||||||
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
||||||
"""Return additional attributes."""
|
"""Return additional attributes."""
|
||||||
if self._forecasts:
|
if self._forecasts:
|
||||||
wind_gust = speed_util.convert(
|
wind_gust = SpeedConverter.convert(
|
||||||
self._forecasts[0].wind_gust,
|
self._forecasts[0].wind_gust,
|
||||||
SPEED_METERS_PER_SECOND,
|
SPEED_METERS_PER_SECOND,
|
||||||
self._wind_speed_unit,
|
self._wind_speed_unit,
|
||||||
|
|
|
@ -30,11 +30,11 @@ from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
|
||||||
from homeassistant.helpers.entity import async_generate_entity_id
|
from homeassistant.helpers.entity import async_generate_entity_id
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
from homeassistant.util import (
|
from homeassistant.util.unit_conversion import (
|
||||||
distance as distance_util,
|
DistanceConverter,
|
||||||
pressure as pressure_util,
|
PressureConverter,
|
||||||
speed as speed_util,
|
SpeedConverter,
|
||||||
temperature as temp_util,
|
TemperatureConverter,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .template_entity import TemplateEntity, rewrite_common_legacy_to_modern_conf
|
from .template_entity import TemplateEntity, rewrite_common_legacy_to_modern_conf
|
||||||
|
@ -87,11 +87,11 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
vol.Optional(CONF_VISIBILITY_TEMPLATE): cv.template,
|
vol.Optional(CONF_VISIBILITY_TEMPLATE): cv.template,
|
||||||
vol.Optional(CONF_FORECAST_TEMPLATE): cv.template,
|
vol.Optional(CONF_FORECAST_TEMPLATE): cv.template,
|
||||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||||
vol.Optional(CONF_TEMPERATURE_UNIT): vol.In(temp_util.VALID_UNITS),
|
vol.Optional(CONF_TEMPERATURE_UNIT): vol.In(TemperatureConverter.VALID_UNITS),
|
||||||
vol.Optional(CONF_PRESSURE_UNIT): vol.In(pressure_util.VALID_UNITS),
|
vol.Optional(CONF_PRESSURE_UNIT): vol.In(PressureConverter.VALID_UNITS),
|
||||||
vol.Optional(CONF_WIND_SPEED_UNIT): vol.In(speed_util.VALID_UNITS),
|
vol.Optional(CONF_WIND_SPEED_UNIT): vol.In(SpeedConverter.VALID_UNITS),
|
||||||
vol.Optional(CONF_VISIBILITY_UNIT): vol.In(distance_util.VALID_UNITS),
|
vol.Optional(CONF_VISIBILITY_UNIT): vol.In(DistanceConverter.VALID_UNITS),
|
||||||
vol.Optional(CONF_PRECIPITATION_UNIT): vol.In(distance_util.VALID_UNITS),
|
vol.Optional(CONF_PRECIPITATION_UNIT): vol.In(DistanceConverter.VALID_UNITS),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -38,11 +38,11 @@ from homeassistant.helpers.config_validation import ( # noqa: F401
|
||||||
from homeassistant.helpers.entity import Entity, EntityDescription
|
from homeassistant.helpers.entity import Entity, EntityDescription
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.util import (
|
from homeassistant.util.unit_conversion import (
|
||||||
distance as distance_util,
|
DistanceConverter,
|
||||||
pressure as pressure_util,
|
PressureConverter,
|
||||||
speed as speed_util,
|
SpeedConverter,
|
||||||
temperature as temperature_util,
|
TemperatureConverter,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -126,11 +126,11 @@ VALID_UNITS_WIND_SPEED: tuple[str, ...] = (
|
||||||
)
|
)
|
||||||
|
|
||||||
UNIT_CONVERSIONS: dict[str, Callable[[float, str, str], float]] = {
|
UNIT_CONVERSIONS: dict[str, Callable[[float, str, str], float]] = {
|
||||||
ATTR_WEATHER_PRESSURE_UNIT: pressure_util.convert,
|
ATTR_WEATHER_PRESSURE_UNIT: PressureConverter.convert,
|
||||||
ATTR_WEATHER_TEMPERATURE_UNIT: temperature_util.convert,
|
ATTR_WEATHER_TEMPERATURE_UNIT: TemperatureConverter.convert,
|
||||||
ATTR_WEATHER_VISIBILITY_UNIT: distance_util.convert,
|
ATTR_WEATHER_VISIBILITY_UNIT: DistanceConverter.convert,
|
||||||
ATTR_WEATHER_PRECIPITATION_UNIT: distance_util.convert,
|
ATTR_WEATHER_PRECIPITATION_UNIT: DistanceConverter.convert,
|
||||||
ATTR_WEATHER_WIND_SPEED_UNIT: speed_util.convert,
|
ATTR_WEATHER_WIND_SPEED_UNIT: SpeedConverter.convert,
|
||||||
}
|
}
|
||||||
|
|
||||||
VALID_UNITS: dict[str, tuple[str, ...]] = {
|
VALID_UNITS: dict[str, tuple[str, ...]] = {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue