Fix tomorrow.io units... again... (#70029)

pull/70054/head
Raman Gupta 2022-04-14 12:58:16 -04:00 committed by Paulus Schoutsen
parent e01faa7a8f
commit 74d38e00e4
3 changed files with 28 additions and 34 deletions

View File

@ -202,6 +202,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.data[CONF_API_KEY],
entry.data[CONF_LOCATION][CONF_LATITUDE],
entry.data[CONF_LOCATION][CONF_LONGITUDE],
unit_system="metric",
session=async_get_clientsession(hass),
)

View File

@ -31,16 +31,14 @@ from homeassistant.const import (
LENGTH_MILES,
PERCENTAGE,
PRESSURE_HPA,
PRESSURE_INHG,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
TEMP_FAHRENHEIT,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import slugify
from homeassistant.util.distance import convert as distance_convert
from homeassistant.util.pressure import convert as pressure_convert
from . import TomorrowioDataUpdateCoordinator, TomorrowioEntity
from .const import (
@ -80,7 +78,7 @@ class TomorrowioSensorEntityDescription(SensorEntityDescription):
unit_imperial: str | None = None
unit_metric: str | None = None
multiplication_factor: Callable[[float], float] | float | None = None
metric_conversion: Callable[[float], float] | float | None = None
imperial_conversion: Callable[[float], float] | float | None = None
value_map: Any | None = None
def __post_init__(self) -> None:
@ -105,13 +103,13 @@ SENSOR_TYPES = (
TomorrowioSensorEntityDescription(
key=TMRW_ATTR_FEELS_LIKE,
name="Feels Like",
native_unit_of_measurement=TEMP_FAHRENHEIT,
native_unit_of_measurement=TEMP_CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
),
TomorrowioSensorEntityDescription(
key=TMRW_ATTR_DEW_POINT,
name="Dew Point",
native_unit_of_measurement=TEMP_FAHRENHEIT,
native_unit_of_measurement=TEMP_CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
),
# Data comes in as inHg
@ -119,9 +117,6 @@ SENSOR_TYPES = (
key=TMRW_ATTR_PRESSURE_SURFACE_LEVEL,
name="Pressure (Surface Level)",
native_unit_of_measurement=PRESSURE_HPA,
multiplication_factor=lambda val: pressure_convert(
val, PRESSURE_INHG, PRESSURE_HPA
),
device_class=SensorDeviceClass.PRESSURE,
),
# Data comes in as BTUs/(hr * ft^2)
@ -131,7 +126,7 @@ SENSOR_TYPES = (
name="Global Horizontal Irradiance",
unit_imperial=IRRADIATION_BTUS_PER_HOUR_SQUARE_FOOT,
unit_metric=IRRADIATION_WATTS_PER_SQUARE_METER,
metric_conversion=3.15459,
imperial_conversion=(1 / 3.15459),
),
# Data comes in as miles
TomorrowioSensorEntityDescription(
@ -139,8 +134,8 @@ SENSOR_TYPES = (
name="Cloud Base",
unit_imperial=LENGTH_MILES,
unit_metric=LENGTH_KILOMETERS,
metric_conversion=lambda val: distance_convert(
val, LENGTH_MILES, LENGTH_KILOMETERS
imperial_conversion=lambda val: distance_convert(
val, LENGTH_KILOMETERS, LENGTH_MILES
),
),
# Data comes in as miles
@ -149,8 +144,8 @@ SENSOR_TYPES = (
name="Cloud Ceiling",
unit_imperial=LENGTH_MILES,
unit_metric=LENGTH_KILOMETERS,
metric_conversion=lambda val: distance_convert(
val, LENGTH_MILES, LENGTH_KILOMETERS
imperial_conversion=lambda val: distance_convert(
val, LENGTH_KILOMETERS, LENGTH_MILES
),
),
TomorrowioSensorEntityDescription(
@ -164,8 +159,10 @@ SENSOR_TYPES = (
name="Wind Gust",
unit_imperial=SPEED_MILES_PER_HOUR,
unit_metric=SPEED_METERS_PER_SECOND,
metric_conversion=lambda val: distance_convert(val, LENGTH_MILES, LENGTH_METERS)
/ 3600,
imperial_conversion=lambda val: distance_convert(
val, LENGTH_METERS, LENGTH_MILES
)
* 3600,
),
TomorrowioSensorEntityDescription(
key=TMRW_ATTR_PRECIPITATION_TYPE,
@ -183,20 +180,16 @@ SENSOR_TYPES = (
multiplication_factor=convert_ppb_to_ugm3(48),
device_class=SensorDeviceClass.OZONE,
),
# Data comes in as ug/ft^3
TomorrowioSensorEntityDescription(
key=TMRW_ATTR_PARTICULATE_MATTER_25,
name="Particulate Matter < 2.5 μm",
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
multiplication_factor=3.2808399**3,
device_class=SensorDeviceClass.PM25,
),
# Data comes in as ug/ft^3
TomorrowioSensorEntityDescription(
key=TMRW_ATTR_PARTICULATE_MATTER_10,
name="Particulate Matter < 10 μm",
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
multiplication_factor=3.2808399**3,
device_class=SensorDeviceClass.PM10,
),
# Data comes in as ppb
@ -360,15 +353,15 @@ class BaseTomorrowioSensorEntity(TomorrowioEntity, SensorEntity):
if desc.multiplication_factor is not None:
state = handle_conversion(state, desc.multiplication_factor)
# If an imperial unit isn't provided, we always want to convert to metric since
# that is what the UI expects
# If there is an imperial conversion needed and the instance is using imperial,
# apply the conversion logic.
if (
desc.metric_conversion
desc.imperial_conversion
and desc.unit_imperial is not None
and desc.unit_imperial != desc.unit_metric
and self.hass.config.units.is_metric
and not self.hass.config.units.is_metric
):
return handle_conversion(state, desc.metric_conversion)
return handle_conversion(state, desc.imperial_conversion)
return state

View File

@ -146,8 +146,8 @@ async def test_v4_sensor(hass: HomeAssistant) -> None:
check_sensor_state(hass, CO, "0.0")
check_sensor_state(hass, NO2, "20.08")
check_sensor_state(hass, SO2, "4.32")
check_sensor_state(hass, PM25, "5.3")
check_sensor_state(hass, PM10, "20.13")
check_sensor_state(hass, PM25, "0.15")
check_sensor_state(hass, PM10, "0.57")
check_sensor_state(hass, MEP_AQI, "23")
check_sensor_state(hass, MEP_HEALTH_CONCERN, "good")
check_sensor_state(hass, MEP_PRIMARY_POLLUTANT, "pm10")
@ -158,14 +158,14 @@ async def test_v4_sensor(hass: HomeAssistant) -> None:
check_sensor_state(hass, GRASS_POLLEN, "none")
check_sensor_state(hass, WEED_POLLEN, "none")
check_sensor_state(hass, TREE_POLLEN, "none")
check_sensor_state(hass, FEELS_LIKE, "38.5")
check_sensor_state(hass, DEW_POINT, "22.68")
check_sensor_state(hass, PRESSURE_SURFACE_LEVEL, "997.97")
check_sensor_state(hass, GHI, "0.0")
check_sensor_state(hass, CLOUD_BASE, "1.19")
check_sensor_state(hass, FEELS_LIKE, "101.3")
check_sensor_state(hass, DEW_POINT, "72.82")
check_sensor_state(hass, PRESSURE_SURFACE_LEVEL, "29.47")
check_sensor_state(hass, GHI, "0")
check_sensor_state(hass, CLOUD_BASE, "0.74")
check_sensor_state(hass, CLOUD_COVER, "100")
check_sensor_state(hass, CLOUD_CEILING, "1.19")
check_sensor_state(hass, WIND_GUST, "5.65")
check_sensor_state(hass, CLOUD_CEILING, "0.74")
check_sensor_state(hass, WIND_GUST, "12.64")
check_sensor_state(hass, PRECIPITATION_TYPE, "rain")