Home Assistant Unofficial Reference 2024.12.1
weather.py
Go to the documentation of this file.
1 """Support for WeatherFlow Forecast weather service."""
2 
3 from __future__ import annotations
4 
5 from weatherflow4py.models.rest.unified import WeatherFlowDataREST
6 
8  Forecast,
9  SingleCoordinatorWeatherEntity,
10  WeatherEntityFeature,
11 )
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import (
14  UnitOfPrecipitationDepth,
15  UnitOfPressure,
16  UnitOfSpeed,
17  UnitOfTemperature,
18 )
19 from homeassistant.core import HomeAssistant, callback
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from .const import DOMAIN, STATE_MAP
23 from .coordinator import WeatherFlowCloudDataUpdateCoordinator
24 from .entity import WeatherFlowCloudEntity
25 
26 
28  hass: HomeAssistant,
29  config_entry: ConfigEntry,
30  async_add_entities: AddEntitiesCallback,
31 ) -> None:
32  """Add a weather entity from a config_entry."""
33  coordinator: WeatherFlowCloudDataUpdateCoordinator = hass.data[DOMAIN][
34  config_entry.entry_id
35  ]
36 
38  [
39  WeatherFlowWeather(coordinator, station_id=station_id)
40  for station_id, data in coordinator.data.items()
41  ]
42  )
43 
44 
46  WeatherFlowCloudEntity,
47  SingleCoordinatorWeatherEntity[WeatherFlowCloudDataUpdateCoordinator],
48 ):
49  """Implementation of a WeatherFlow weather condition."""
50 
51  _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
52  _attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS
53  _attr_native_pressure_unit = UnitOfPressure.MBAR
54  _attr_native_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
55  _attr_supported_features = (
56  WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY
57  )
58  _attr_name = None
59 
60  def __init__(
61  self,
62  coordinator: WeatherFlowCloudDataUpdateCoordinator,
63  station_id: int,
64  ) -> None:
65  """Initialise the platform with a data instance and station."""
66  super().__init__(coordinator, station_id)
67  self._attr_unique_id_attr_unique_id = f"weatherflow_forecast_{station_id}"
68 
69  @property
70  def local_data(self) -> WeatherFlowDataREST:
71  """Return the local weather data object for this station."""
72  return self.coordinator.data[self.station_idstation_id]
73 
74  @property
75  def condition(self) -> str | None:
76  """Return current condition - required property."""
77  return STATE_MAP[self.local_datalocal_data.weather.current_conditions.icon.value]
78 
79  @property
80  def native_temperature(self) -> float | None:
81  """Return the temperature."""
82  return self.local_datalocal_data.weather.current_conditions.air_temperature
83 
84  @property
85  def native_pressure(self) -> float | None:
86  """Return the Air Pressure @ Station."""
87  return self.local_datalocal_data.weather.current_conditions.station_pressure
88 
89  @property
90  def humidity(self) -> float | None:
91  """Return the humidity."""
92  return self.local_datalocal_data.weather.current_conditions.relative_humidity
93 
94  @property
95  def native_wind_speed(self) -> float | None:
96  """Return the wind speed."""
97  return self.local_datalocal_data.weather.current_conditions.wind_avg
98 
99  @property
100  def wind_bearing(self) -> float | str | None:
101  """Return the wind direction."""
102  return self.local_datalocal_data.weather.current_conditions.wind_direction
103 
104  @property
105  def native_wind_gust_speed(self) -> float | None:
106  """Return the wind gust speed in native units."""
107  return self.local_datalocal_data.weather.current_conditions.wind_gust
108 
109  @property
110  def native_dew_point(self) -> float | None:
111  """Return dew point."""
112  return self.local_datalocal_data.weather.current_conditions.dew_point
113 
114  @property
115  def uv_index(self) -> float | None:
116  """Return UV Index."""
117  return self.local_datalocal_data.weather.current_conditions.uv
118 
119  @callback
120  def _async_forecast_daily(self) -> list[Forecast] | None:
121  """Return the daily forecast in native units."""
122  return [x.ha_forecast for x in self.local_datalocal_data.weather.forecast.daily]
123 
124  @callback
125  def _async_forecast_hourly(self) -> list[Forecast] | None:
126  """Return the hourly forecast in native units."""
127  return [x.ha_forecast for x in self.local_datalocal_data.weather.forecast.hourly]
None __init__(self, WeatherFlowCloudDataUpdateCoordinator coordinator, int station_id)
Definition: weather.py:64
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: weather.py:31