Home Assistant Unofficial Reference 2024.12.1
weather.py
Go to the documentation of this file.
1 """Support for the AEMET OpenData service."""
2 
3 from aemet_opendata.const import (
4  AOD_CONDITION,
5  AOD_FORECAST_DAILY,
6  AOD_FORECAST_HOURLY,
7  AOD_HUMIDITY,
8  AOD_PRESSURE,
9  AOD_TEMP,
10  AOD_WEATHER,
11  AOD_WIND_DIRECTION,
12  AOD_WIND_SPEED,
13  AOD_WIND_SPEED_MAX,
14 )
15 
17  Forecast,
18  SingleCoordinatorWeatherEntity,
19  WeatherEntityFeature,
20 )
21 from homeassistant.const import (
22  UnitOfPrecipitationDepth,
23  UnitOfPressure,
24  UnitOfSpeed,
25  UnitOfTemperature,
26 )
27 from homeassistant.core import HomeAssistant, callback
28 from homeassistant.helpers.entity_platform import AddEntitiesCallback
29 
30 from .const import CONDITIONS_MAP
31 from .coordinator import AemetConfigEntry, WeatherUpdateCoordinator
32 from .entity import AemetEntity
33 
34 
36  hass: HomeAssistant,
37  config_entry: AemetConfigEntry,
38  async_add_entities: AddEntitiesCallback,
39 ) -> None:
40  """Set up AEMET OpenData weather entity based on a config entry."""
41  domain_data = config_entry.runtime_data
42  name = domain_data.name
43  weather_coordinator = domain_data.coordinator
44 
45  unique_id = config_entry.unique_id
46  assert unique_id is not None
47 
48  async_add_entities([AemetWeather(name, unique_id, weather_coordinator)])
49 
50 
52  AemetEntity,
53  SingleCoordinatorWeatherEntity[WeatherUpdateCoordinator],
54 ):
55  """Implementation of an AEMET OpenData weather."""
56 
57  _attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS
58  _attr_native_pressure_unit = UnitOfPressure.HPA
59  _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
60  _attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
61  _attr_supported_features = (
62  WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY
63  )
64  _attr_name = None
65 
66  def __init__(
67  self,
68  name: str,
69  unique_id: str,
70  coordinator: WeatherUpdateCoordinator,
71  ) -> None:
72  """Initialize the sensor."""
73  super().__init__(coordinator, name, unique_id)
74  self._attr_unique_id_attr_unique_id = unique_id
75 
76  @property
77  def condition(self):
78  """Return the current condition."""
79  cond = self.get_aemet_valueget_aemet_value([AOD_WEATHER, AOD_CONDITION])
80  return CONDITIONS_MAP.get(cond)
81 
82  @callback
83  def _async_forecast_daily(self) -> list[Forecast]:
84  """Return the daily forecast in native units."""
85  return self.get_aemet_forecastget_aemet_forecast(AOD_FORECAST_DAILY)
86 
87  @callback
88  def _async_forecast_hourly(self) -> list[Forecast]:
89  """Return the hourly forecast in native units."""
90  return self.get_aemet_forecastget_aemet_forecast(AOD_FORECAST_HOURLY)
91 
92  @property
93  def humidity(self):
94  """Return the humidity."""
95  return self.get_aemet_valueget_aemet_value([AOD_WEATHER, AOD_HUMIDITY])
96 
97  @property
98  def native_pressure(self):
99  """Return the pressure."""
100  return self.get_aemet_valueget_aemet_value([AOD_WEATHER, AOD_PRESSURE])
101 
102  @property
104  """Return the temperature."""
105  return self.get_aemet_valueget_aemet_value([AOD_WEATHER, AOD_TEMP])
106 
107  @property
108  def wind_bearing(self):
109  """Return the wind bearing."""
110  return self.get_aemet_valueget_aemet_value([AOD_WEATHER, AOD_WIND_DIRECTION])
111 
112  @property
114  """Return the wind gust speed in native units."""
115  return self.get_aemet_valueget_aemet_value([AOD_WEATHER, AOD_WIND_SPEED_MAX])
116 
117  @property
118  def native_wind_speed(self):
119  """Return the wind speed."""
120  return self.get_aemet_valueget_aemet_value([AOD_WEATHER, AOD_WIND_SPEED])
list[Forecast] get_aemet_forecast(self, str forecast_mode)
Definition: entity.py:39
Any get_aemet_value(self, list[str] keys)
Definition: entity.py:43
None __init__(self, str name, str unique_id, WeatherUpdateCoordinator coordinator)
Definition: weather.py:71
None async_setup_entry(HomeAssistant hass, AemetConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: weather.py:39