Use new unit enums in alexa (#83409)

* Use new unit enums in alexa

* Adjust for mypy
This commit is contained in:
epenet 2022-12-06 17:31:40 +01:00 committed by GitHub
parent d715aa6867
commit 91d6d620c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 17 deletions

View File

@ -2,7 +2,7 @@
from collections import OrderedDict from collections import OrderedDict
from homeassistant.components import climate from homeassistant.components import climate
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.const import UnitOfTemperature
DOMAIN = "alexa" DOMAIN = "alexa"
EVENT_ALEXA_SMART_HOME = "alexa_smart_home" EVENT_ALEXA_SMART_HOME = "alexa_smart_home"
@ -61,7 +61,10 @@ CONF_SUPPORTED_LOCALES = (
"pt-BR", "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 # 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 # reverse mapping of this dict and we want to map the first occurrence of OFF

View File

@ -37,8 +37,7 @@ from homeassistant.const import (
CLOUD_NEVER_EXPOSED_ENTITIES, CLOUD_NEVER_EXPOSED_ENTITIES,
CONF_DESCRIPTION, CONF_DESCRIPTION,
CONF_NAME, CONF_NAME,
TEMP_CELSIUS, UnitOfTemperature,
TEMP_FAHRENHEIT,
__version__, __version__,
) )
from homeassistant.core import HomeAssistant, State, callback from homeassistant.core import HomeAssistant, State, callback
@ -745,7 +744,10 @@ class SensorCapabilities(AlexaEntity):
def interfaces(self): def interfaces(self):
"""Yield the supported interfaces.""" """Yield the supported interfaces."""
attrs = self.entity.attributes 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 AlexaTemperatureSensor(self.hass, self.entity)
yield AlexaEndpointHealth(self.hass, self.entity) yield AlexaEndpointHealth(self.hass, self.entity)
yield Alexa(self.hass) yield Alexa(self.hass)

View File

@ -47,8 +47,7 @@ from homeassistant.const import (
SERVICE_VOLUME_SET, SERVICE_VOLUME_SET,
SERVICE_VOLUME_UP, SERVICE_VOLUME_UP,
STATE_ALARM_DISARMED, STATE_ALARM_DISARMED,
TEMP_CELSIUS, UnitOfTemperature,
TEMP_FAHRENHEIT,
) )
from homeassistant.helpers import network from homeassistant.helpers import network
from homeassistant.util import color as color_util, dt as dt_util 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): def temperature_from_object(hass, temp_obj, interval=False):
"""Get temperature from Temperature object in requested unit.""" """Get temperature from Temperature object in requested unit."""
to_unit = hass.config.units.temperature_unit to_unit = hass.config.units.temperature_unit
from_unit = TEMP_CELSIUS from_unit = UnitOfTemperature.CELSIUS
temp = float(temp_obj["value"]) temp = float(temp_obj["value"])
if temp_obj["scale"] == "FAHRENHEIT": if temp_obj["scale"] == "FAHRENHEIT":
from_unit = TEMP_FAHRENHEIT from_unit = UnitOfTemperature.FAHRENHEIT
elif temp_obj["scale"] == "KELVIN" and not interval: elif temp_obj["scale"] == "KELVIN" and not interval:
# convert to Celsius if absolute temperature # convert to Celsius if absolute temperature
temp -= 273.15 temp -= 273.15

View File

@ -179,7 +179,7 @@ async def async_get_trigger_capabilities(
} }
if trigger_type == "current_temperature_changed": if trigger_type == "current_temperature_changed":
unit_of_measurement = hass.config.units.temperature_unit unit_of_measurement: str = hass.config.units.temperature_unit
else: else:
unit_of_measurement = PERCENTAGE unit_of_measurement = PERCENTAGE

View File

@ -84,14 +84,14 @@ class UnitSystem:
self, self,
name: str, name: str,
*, *,
accumulated_precipitation: str, accumulated_precipitation: UnitOfPrecipitationDepth,
conversions: dict[tuple[SensorDeviceClass | str | None, str | None], str], conversions: dict[tuple[SensorDeviceClass | str | None, str | None], str],
length: str, length: UnitOfLength,
mass: str, mass: UnitOfMass,
pressure: str, pressure: UnitOfPressure,
temperature: str, temperature: UnitOfTemperature,
volume: str, volume: UnitOfVolume,
wind_speed: str, wind_speed: UnitOfSpeed,
) -> None: ) -> None:
"""Initialize the unit system object.""" """Initialize the unit system object."""
errors: str = ", ".join( errors: str = ", ".join(