mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Migrate climacell to native_* (#74039)
This commit is contained in:
parent
79b3865b60
commit
39c7056be5
@ -23,13 +23,9 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_API_VERSION,
|
CONF_API_VERSION,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
LENGTH_FEET,
|
LENGTH_INCHES,
|
||||||
LENGTH_KILOMETERS,
|
|
||||||
LENGTH_METERS,
|
|
||||||
LENGTH_MILES,
|
LENGTH_MILES,
|
||||||
PRESSURE_HPA,
|
|
||||||
PRESSURE_INHG,
|
PRESSURE_INHG,
|
||||||
SPEED_KILOMETERS_PER_HOUR,
|
|
||||||
SPEED_MILES_PER_HOUR,
|
SPEED_MILES_PER_HOUR,
|
||||||
TEMP_FAHRENHEIT,
|
TEMP_FAHRENHEIT,
|
||||||
)
|
)
|
||||||
@ -37,8 +33,6 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.sun import is_up
|
from homeassistant.helpers.sun import is_up
|
||||||
from homeassistant.util import dt as dt_util
|
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 homeassistant.util.speed import convert as speed_convert
|
||||||
|
|
||||||
from . import ClimaCellDataUpdateCoordinator, ClimaCellEntity
|
from . import ClimaCellDataUpdateCoordinator, ClimaCellEntity
|
||||||
@ -89,6 +83,12 @@ async def async_setup_entry(
|
|||||||
class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
||||||
"""Base ClimaCell weather entity."""
|
"""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__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
@ -132,21 +132,6 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
|||||||
else:
|
else:
|
||||||
translated_condition = self._translate_condition(condition, True)
|
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 = {
|
data = {
|
||||||
ATTR_FORECAST_TIME: forecast_dt.isoformat(),
|
ATTR_FORECAST_TIME: forecast_dt.isoformat(),
|
||||||
ATTR_FORECAST_CONDITION: translated_condition,
|
ATTR_FORECAST_CONDITION: translated_condition,
|
||||||
@ -164,11 +149,8 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
|||||||
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
||||||
"""Return additional state attributes."""
|
"""Return additional state attributes."""
|
||||||
wind_gust = self.wind_gust
|
wind_gust = self.wind_gust
|
||||||
if wind_gust and self.hass.config.units.is_metric:
|
|
||||||
wind_gust = round(
|
wind_gust = round(
|
||||||
speed_convert(
|
speed_convert(self.wind_gust, SPEED_MILES_PER_HOUR, self._wind_speed_unit),
|
||||||
self.wind_gust, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR
|
|
||||||
),
|
|
||||||
4,
|
4,
|
||||||
)
|
)
|
||||||
cloud_cover = self.cloud_cover
|
cloud_cover = self.cloud_cover
|
||||||
@ -199,12 +181,8 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
|||||||
"""Return the raw pressure."""
|
"""Return the raw pressure."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def pressure(self):
|
def native_pressure(self):
|
||||||
"""Return the pressure."""
|
"""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
|
return self._pressure
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -213,15 +191,8 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
|||||||
"""Return the raw wind speed."""
|
"""Return the raw wind speed."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def wind_speed(self):
|
def native_wind_speed(self):
|
||||||
"""Return the wind speed."""
|
"""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
|
return self._wind_speed
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -230,20 +201,14 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
|||||||
"""Return the raw visibility."""
|
"""Return the raw visibility."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def visibility(self):
|
def native_visibility(self):
|
||||||
"""Return the visibility."""
|
"""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
|
return self._visibility
|
||||||
|
|
||||||
|
|
||||||
class ClimaCellV3WeatherEntity(BaseClimaCellWeatherEntity):
|
class ClimaCellV3WeatherEntity(BaseClimaCellWeatherEntity):
|
||||||
"""Entity that talks to ClimaCell v3 API to retrieve weather data."""
|
"""Entity that talks to ClimaCell v3 API to retrieve weather data."""
|
||||||
|
|
||||||
_attr_temperature_unit = TEMP_FAHRENHEIT
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _translate_condition(
|
def _translate_condition(
|
||||||
condition: int | str | None, sun_is_up: bool = True
|
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_CONDITION: ATTR_CONDITION_SNOWY,
|
||||||
ATTR_FORECAST_TIME: "2021-03-15T00:00:00-07:00", # DST starts
|
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_PRECIPITATION_PROBABILITY: 95,
|
||||||
ATTR_FORECAST_TEMP: 1.2,
|
ATTR_FORECAST_TEMP: 1.2,
|
||||||
ATTR_FORECAST_TEMP_LOW: 0.2,
|
ATTR_FORECAST_TEMP_LOW: 0.2,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user