Migrate climacell to native_* (#74039)
This commit is contained in:
parent
79b3865b60
commit
39c7056be5
2 changed files with 15 additions and 50 deletions
|
@ -23,13 +23,9 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.const import (
|
||||
CONF_API_VERSION,
|
||||
CONF_NAME,
|
||||
LENGTH_FEET,
|
||||
LENGTH_KILOMETERS,
|
||||
LENGTH_METERS,
|
||||
LENGTH_INCHES,
|
||||
LENGTH_MILES,
|
||||
PRESSURE_HPA,
|
||||
PRESSURE_INHG,
|
||||
SPEED_KILOMETERS_PER_HOUR,
|
||||
SPEED_MILES_PER_HOUR,
|
||||
TEMP_FAHRENHEIT,
|
||||
)
|
||||
|
@ -37,8 +33,6 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.sun import is_up
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.distance import convert as distance_convert
|
||||
from homeassistant.util.pressure import convert as pressure_convert
|
||||
from homeassistant.util.speed import convert as speed_convert
|
||||
|
||||
from . import ClimaCellDataUpdateCoordinator, ClimaCellEntity
|
||||
|
@ -89,6 +83,12 @@ async def async_setup_entry(
|
|||
class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
||||
"""Base ClimaCell weather entity."""
|
||||
|
||||
_attr_native_precipitation_unit = LENGTH_INCHES
|
||||
_attr_native_pressure_unit = PRESSURE_INHG
|
||||
_attr_native_temperature_unit = TEMP_FAHRENHEIT
|
||||
_attr_native_visibility_unit = LENGTH_MILES
|
||||
_attr_native_wind_speed_unit = SPEED_MILES_PER_HOUR
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
|
@ -132,21 +132,6 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
|||
else:
|
||||
translated_condition = self._translate_condition(condition, True)
|
||||
|
||||
if self.hass.config.units.is_metric:
|
||||
if precipitation:
|
||||
precipitation = round(
|
||||
distance_convert(precipitation / 12, LENGTH_FEET, LENGTH_METERS)
|
||||
* 1000,
|
||||
4,
|
||||
)
|
||||
if wind_speed:
|
||||
wind_speed = round(
|
||||
speed_convert(
|
||||
wind_speed, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR
|
||||
),
|
||||
4,
|
||||
)
|
||||
|
||||
data = {
|
||||
ATTR_FORECAST_TIME: forecast_dt.isoformat(),
|
||||
ATTR_FORECAST_CONDITION: translated_condition,
|
||||
|
@ -164,13 +149,10 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
|||
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
||||
"""Return additional state attributes."""
|
||||
wind_gust = self.wind_gust
|
||||
if wind_gust and self.hass.config.units.is_metric:
|
||||
wind_gust = round(
|
||||
speed_convert(
|
||||
self.wind_gust, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR
|
||||
),
|
||||
4,
|
||||
)
|
||||
wind_gust = round(
|
||||
speed_convert(self.wind_gust, SPEED_MILES_PER_HOUR, self._wind_speed_unit),
|
||||
4,
|
||||
)
|
||||
cloud_cover = self.cloud_cover
|
||||
return {
|
||||
ATTR_CLOUD_COVER: cloud_cover,
|
||||
|
@ -199,12 +181,8 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
|||
"""Return the raw pressure."""
|
||||
|
||||
@property
|
||||
def pressure(self):
|
||||
def native_pressure(self):
|
||||
"""Return the pressure."""
|
||||
if self.hass.config.units.is_metric and self._pressure:
|
||||
return round(
|
||||
pressure_convert(self._pressure, PRESSURE_INHG, PRESSURE_HPA), 4
|
||||
)
|
||||
return self._pressure
|
||||
|
||||
@property
|
||||
|
@ -213,15 +191,8 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
|||
"""Return the raw wind speed."""
|
||||
|
||||
@property
|
||||
def wind_speed(self):
|
||||
def native_wind_speed(self):
|
||||
"""Return the wind speed."""
|
||||
if self.hass.config.units.is_metric and self._wind_speed:
|
||||
return round(
|
||||
speed_convert(
|
||||
self._wind_speed, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR
|
||||
),
|
||||
4,
|
||||
)
|
||||
return self._wind_speed
|
||||
|
||||
@property
|
||||
|
@ -230,20 +201,14 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
|||
"""Return the raw visibility."""
|
||||
|
||||
@property
|
||||
def visibility(self):
|
||||
def native_visibility(self):
|
||||
"""Return the visibility."""
|
||||
if self.hass.config.units.is_metric and self._visibility:
|
||||
return round(
|
||||
distance_convert(self._visibility, LENGTH_MILES, LENGTH_KILOMETERS), 4
|
||||
)
|
||||
return self._visibility
|
||||
|
||||
|
||||
class ClimaCellV3WeatherEntity(BaseClimaCellWeatherEntity):
|
||||
"""Entity that talks to ClimaCell v3 API to retrieve weather data."""
|
||||
|
||||
_attr_temperature_unit = TEMP_FAHRENHEIT
|
||||
|
||||
@staticmethod
|
||||
def _translate_condition(
|
||||
condition: int | str | None, sun_is_up: bool = True
|
||||
|
|
|
@ -156,7 +156,7 @@ async def test_v3_weather(
|
|||
{
|
||||
ATTR_FORECAST_CONDITION: ATTR_CONDITION_SNOWY,
|
||||
ATTR_FORECAST_TIME: "2021-03-15T00:00:00-07:00", # DST starts
|
||||
ATTR_FORECAST_PRECIPITATION: 7.3,
|
||||
ATTR_FORECAST_PRECIPITATION: 7.31,
|
||||
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 95,
|
||||
ATTR_FORECAST_TEMP: 1.2,
|
||||
ATTR_FORECAST_TEMP_LOW: 0.2,
|
||||
|
|
Loading…
Add table
Reference in a new issue