Skip to content

Commit

Permalink
refactor: migrate from consts to UnitOf schema (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
IATkachenko authored Jan 6, 2024
1 parent 2055833 commit fcd9a78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
19 changes: 9 additions & 10 deletions custom_components/yandex_weather/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
PRESSURE_HPA,
PRESSURE_MMHG,
SPEED_METERS_PER_SECOND,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
TEMP_CELSIUS,
UnitOfPressure,
UnitOfSpeed,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
Expand Down Expand Up @@ -50,23 +49,23 @@
SensorEntityDescription(
key=ATTR_API_TEMPERATURE,
name="Temperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
),
SensorEntityDescription(
key=ATTR_API_FEELS_LIKE_TEMPERATURE,
name="Feels like temperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
),
SensorEntityDescription(
key=ATTR_API_WIND_SPEED,
name="Wind speed",
native_unit_of_measurement=SPEED_METERS_PER_SECOND,
native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
icon="mdi:weather-windy",
Expand All @@ -89,7 +88,7 @@
SensorEntityDescription(
key=ATTR_API_PRESSURE,
name="Pressure",
native_unit_of_measurement=PRESSURE_HPA,
native_unit_of_measurement=UnitOfPressure.HPA,
device_class=SensorDeviceClass.PRESSURE,
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
Expand Down Expand Up @@ -117,7 +116,7 @@
SensorEntityDescription(
key=ATTR_API_PRESSURE_MMHG,
name="Pressure mmHg",
native_unit_of_measurement=PRESSURE_MMHG,
native_unit_of_measurement=UnitOfPressure.MMHG,
icon="mdi:gauge",
# should not define device_class, because HA will try to convert pressure to system units.
state_class=SensorStateClass.MEASUREMENT,
Expand All @@ -126,7 +125,7 @@
SensorEntityDescription(
key=ATTR_MIN_FORECAST_TEMPERATURE,
name="Minimal forecast temperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
Expand Down
16 changes: 8 additions & 8 deletions custom_components/yandex_weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
LENGTH_MILLIMETERS,
PRESSURE_HPA,
SPEED_METERS_PER_SECOND,
STATE_UNAVAILABLE,
TEMP_CELSIUS,
UnitOfPrecipitationDepth,
UnitOfPressure,
UnitOfSpeed,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -75,10 +75,10 @@ class YandexWeather(WeatherEntity, CoordinatorEntity, RestoreEntity):
"""Yandex.Weather entry."""

_attr_attribution = ATTRIBUTION
_attr_native_wind_speed_unit = SPEED_METERS_PER_SECOND
_attr_native_pressure_unit = PRESSURE_HPA
_attr_native_temperature_unit = TEMP_CELSIUS
_attr_native_precipitation_unit = LENGTH_MILLIMETERS
_attr_native_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
_attr_native_pressure_unit = UnitOfPressure.HPA
_attr_native_temperature_unit = UnitOfTemperature.CELSIUS
_attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS
_twice_daily_forecast: list[Forecast] | None
coordinator: WeatherUpdater

Expand Down

0 comments on commit fcd9a78

Please sign in to comment.