Weather units: climacell (1.3/4) (#61472)
* climacell units * Fix climacell tests
This commit is contained in:
parent
ad8af5fc7a
commit
48057e1dfb
2 changed files with 77 additions and 114 deletions
|
@ -31,11 +31,8 @@ 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,
|
||||
|
@ -45,8 +42,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
|
||||
|
@ -117,6 +112,12 @@ async def async_setup_entry(
|
|||
class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
||||
"""Base ClimaCell weather entity."""
|
||||
|
||||
_attr_temperature_unit = TEMP_FAHRENHEIT
|
||||
_attr_pressure_unit = PRESSURE_INHG
|
||||
_attr_wind_speed_unit = SPEED_MILES_PER_HOUR
|
||||
_attr_visibility_unit = LENGTH_MILES
|
||||
_attr_precipitation_unit = LENGTH_INCHES
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
|
@ -160,21 +161,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,
|
||||
|
@ -223,55 +209,23 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity):
|
|||
|
||||
@property
|
||||
@abstractmethod
|
||||
def _pressure(self):
|
||||
def pressure(self):
|
||||
"""Return the raw pressure."""
|
||||
|
||||
@property
|
||||
def 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
|
||||
@abstractmethod
|
||||
def _wind_speed(self):
|
||||
def wind_speed(self):
|
||||
"""Return the raw wind speed."""
|
||||
|
||||
@property
|
||||
def 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
|
||||
@abstractmethod
|
||||
def _visibility(self):
|
||||
"""Return the raw visibility."""
|
||||
|
||||
@property
|
||||
def 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
|
||||
"""Return the raw visibility."""
|
||||
|
||||
|
||||
class ClimaCellWeatherEntity(BaseClimaCellWeatherEntity):
|
||||
"""Entity that talks to ClimaCell v4 API to retrieve weather data."""
|
||||
|
||||
_attr_temperature_unit = TEMP_FAHRENHEIT
|
||||
|
||||
@staticmethod
|
||||
def _translate_condition(
|
||||
condition: int | str | None, sun_is_up: bool = True
|
||||
|
@ -293,7 +247,7 @@ class ClimaCellWeatherEntity(BaseClimaCellWeatherEntity):
|
|||
return self._get_current_property(CC_ATTR_TEMPERATURE)
|
||||
|
||||
@property
|
||||
def _pressure(self):
|
||||
def pressure(self):
|
||||
"""Return the raw pressure."""
|
||||
return self._get_current_property(CC_ATTR_PRESSURE)
|
||||
|
||||
|
@ -321,7 +275,7 @@ class ClimaCellWeatherEntity(BaseClimaCellWeatherEntity):
|
|||
return PrecipitationType(precipitation_type).name.lower()
|
||||
|
||||
@property
|
||||
def _wind_speed(self):
|
||||
def wind_speed(self):
|
||||
"""Return the raw wind speed."""
|
||||
return self._get_current_property(CC_ATTR_WIND_SPEED)
|
||||
|
||||
|
@ -344,7 +298,7 @@ class ClimaCellWeatherEntity(BaseClimaCellWeatherEntity):
|
|||
)
|
||||
|
||||
@property
|
||||
def _visibility(self):
|
||||
def visibility(self):
|
||||
"""Return the raw visibility."""
|
||||
return self._get_current_property(CC_ATTR_VISIBILITY)
|
||||
|
||||
|
@ -418,8 +372,6 @@ class ClimaCellWeatherEntity(BaseClimaCellWeatherEntity):
|
|||
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
|
||||
|
@ -442,7 +394,7 @@ class ClimaCellV3WeatherEntity(BaseClimaCellWeatherEntity):
|
|||
)
|
||||
|
||||
@property
|
||||
def _pressure(self):
|
||||
def pressure(self):
|
||||
"""Return the raw pressure."""
|
||||
return self._get_cc_value(self.coordinator.data[CURRENT], CC_V3_ATTR_PRESSURE)
|
||||
|
||||
|
@ -471,7 +423,7 @@ class ClimaCellV3WeatherEntity(BaseClimaCellWeatherEntity):
|
|||
)
|
||||
|
||||
@property
|
||||
def _wind_speed(self):
|
||||
def wind_speed(self):
|
||||
"""Return the raw wind speed."""
|
||||
return self._get_cc_value(self.coordinator.data[CURRENT], CC_V3_ATTR_WIND_SPEED)
|
||||
|
||||
|
@ -496,7 +448,7 @@ class ClimaCellV3WeatherEntity(BaseClimaCellWeatherEntity):
|
|||
)
|
||||
|
||||
@property
|
||||
def _visibility(self):
|
||||
def visibility(self):
|
||||
"""Return the raw visibility."""
|
||||
return self._get_cc_value(self.coordinator.data[CURRENT], CC_V3_ATTR_VISIBILITY)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue