From 91d6d620c28531352f067a65a6456711318decde Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 6 Dec 2022 17:31:40 +0100 Subject: [PATCH] Use new unit enums in alexa (#83409) * Use new unit enums in alexa * Adjust for mypy --- homeassistant/components/alexa/const.py | 7 +++++-- homeassistant/components/alexa/entities.py | 8 +++++--- homeassistant/components/alexa/handlers.py | 7 +++---- homeassistant/components/climate/device_trigger.py | 2 +- homeassistant/util/unit_system.py | 14 +++++++------- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/alexa/const.py b/homeassistant/components/alexa/const.py index a34355b7ddb..9e1c9e589c1 100644 --- a/homeassistant/components/alexa/const.py +++ b/homeassistant/components/alexa/const.py @@ -2,7 +2,7 @@ from collections import OrderedDict from homeassistant.components import climate -from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT +from homeassistant.const import UnitOfTemperature DOMAIN = "alexa" EVENT_ALEXA_SMART_HOME = "alexa_smart_home" @@ -61,7 +61,10 @@ CONF_SUPPORTED_LOCALES = ( "pt-BR", ) -API_TEMP_UNITS = {TEMP_FAHRENHEIT: "FAHRENHEIT", TEMP_CELSIUS: "CELSIUS"} +API_TEMP_UNITS = { + UnitOfTemperature.FAHRENHEIT: "FAHRENHEIT", + UnitOfTemperature.CELSIUS: "CELSIUS", +} # Needs to be ordered dict for `async_api_set_thermostat_mode` which does a # reverse mapping of this dict and we want to map the first occurrence of OFF diff --git a/homeassistant/components/alexa/entities.py b/homeassistant/components/alexa/entities.py index 35313573b19..77d35a1582c 100644 --- a/homeassistant/components/alexa/entities.py +++ b/homeassistant/components/alexa/entities.py @@ -37,8 +37,7 @@ from homeassistant.const import ( CLOUD_NEVER_EXPOSED_ENTITIES, CONF_DESCRIPTION, CONF_NAME, - TEMP_CELSIUS, - TEMP_FAHRENHEIT, + UnitOfTemperature, __version__, ) from homeassistant.core import HomeAssistant, State, callback @@ -745,7 +744,10 @@ class SensorCapabilities(AlexaEntity): def interfaces(self): """Yield the supported interfaces.""" attrs = self.entity.attributes - if attrs.get(ATTR_UNIT_OF_MEASUREMENT) in (TEMP_FAHRENHEIT, TEMP_CELSIUS): + if attrs.get(ATTR_UNIT_OF_MEASUREMENT) in { + UnitOfTemperature.FAHRENHEIT, + UnitOfTemperature.CELSIUS, + }: yield AlexaTemperatureSensor(self.hass, self.entity) yield AlexaEndpointHealth(self.hass, self.entity) yield Alexa(self.hass) diff --git a/homeassistant/components/alexa/handlers.py b/homeassistant/components/alexa/handlers.py index d9a2e7016e9..220036e5296 100644 --- a/homeassistant/components/alexa/handlers.py +++ b/homeassistant/components/alexa/handlers.py @@ -47,8 +47,7 @@ from homeassistant.const import ( SERVICE_VOLUME_SET, SERVICE_VOLUME_UP, STATE_ALARM_DISARMED, - TEMP_CELSIUS, - TEMP_FAHRENHEIT, + UnitOfTemperature, ) from homeassistant.helpers import network from homeassistant.util import color as color_util, dt as dt_util @@ -758,11 +757,11 @@ async def async_api_previous( def temperature_from_object(hass, temp_obj, interval=False): """Get temperature from Temperature object in requested unit.""" to_unit = hass.config.units.temperature_unit - from_unit = TEMP_CELSIUS + from_unit = UnitOfTemperature.CELSIUS temp = float(temp_obj["value"]) if temp_obj["scale"] == "FAHRENHEIT": - from_unit = TEMP_FAHRENHEIT + from_unit = UnitOfTemperature.FAHRENHEIT elif temp_obj["scale"] == "KELVIN" and not interval: # convert to Celsius if absolute temperature temp -= 273.15 diff --git a/homeassistant/components/climate/device_trigger.py b/homeassistant/components/climate/device_trigger.py index a7b723eb41a..16f7aca3c28 100644 --- a/homeassistant/components/climate/device_trigger.py +++ b/homeassistant/components/climate/device_trigger.py @@ -179,7 +179,7 @@ async def async_get_trigger_capabilities( } if trigger_type == "current_temperature_changed": - unit_of_measurement = hass.config.units.temperature_unit + unit_of_measurement: str = hass.config.units.temperature_unit else: unit_of_measurement = PERCENTAGE diff --git a/homeassistant/util/unit_system.py b/homeassistant/util/unit_system.py index 9c6b6045cf9..49a19792645 100644 --- a/homeassistant/util/unit_system.py +++ b/homeassistant/util/unit_system.py @@ -84,14 +84,14 @@ class UnitSystem: self, name: str, *, - accumulated_precipitation: str, + accumulated_precipitation: UnitOfPrecipitationDepth, conversions: dict[tuple[SensorDeviceClass | str | None, str | None], str], - length: str, - mass: str, - pressure: str, - temperature: str, - volume: str, - wind_speed: str, + length: UnitOfLength, + mass: UnitOfMass, + pressure: UnitOfPressure, + temperature: UnitOfTemperature, + volume: UnitOfVolume, + wind_speed: UnitOfSpeed, ) -> None: """Initialize the unit system object.""" errors: str = ", ".join(