Home Assistant Unofficial Reference 2024.12.1
weather.py
Go to the documentation of this file.
1 """Support for the OpenWeatherMap (OWM) service."""
2 
3 from __future__ import annotations
4 
6  Forecast,
7  SingleCoordinatorWeatherEntity,
8  WeatherEntityFeature,
9 )
10 from homeassistant.const import (
11  UnitOfLength,
12  UnitOfPrecipitationDepth,
13  UnitOfPressure,
14  UnitOfSpeed,
15  UnitOfTemperature,
16 )
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import OpenweathermapConfigEntry
22 from .const import (
23  ATTR_API_CLOUDS,
24  ATTR_API_CONDITION,
25  ATTR_API_CURRENT,
26  ATTR_API_DAILY_FORECAST,
27  ATTR_API_DEW_POINT,
28  ATTR_API_FEELS_LIKE_TEMPERATURE,
29  ATTR_API_HOURLY_FORECAST,
30  ATTR_API_HUMIDITY,
31  ATTR_API_PRESSURE,
32  ATTR_API_TEMPERATURE,
33  ATTR_API_VISIBILITY_DISTANCE,
34  ATTR_API_WIND_BEARING,
35  ATTR_API_WIND_GUST,
36  ATTR_API_WIND_SPEED,
37  ATTRIBUTION,
38  DEFAULT_NAME,
39  DOMAIN,
40  MANUFACTURER,
41  OWM_MODE_FREE_FORECAST,
42  OWM_MODE_V25,
43  OWM_MODE_V30,
44 )
45 from .coordinator import WeatherUpdateCoordinator
46 
47 
49  hass: HomeAssistant,
50  config_entry: OpenweathermapConfigEntry,
51  async_add_entities: AddEntitiesCallback,
52 ) -> None:
53  """Set up OpenWeatherMap weather entity based on a config entry."""
54  domain_data = config_entry.runtime_data
55  name = domain_data.name
56  mode = domain_data.mode
57  weather_coordinator = domain_data.coordinator
58 
59  unique_id = f"{config_entry.unique_id}"
60  owm_weather = OpenWeatherMapWeather(name, unique_id, mode, weather_coordinator)
61 
62  async_add_entities([owm_weather], False)
63 
64 
65 class OpenWeatherMapWeather(SingleCoordinatorWeatherEntity[WeatherUpdateCoordinator]):
66  """Implementation of an OpenWeatherMap sensor."""
67 
68  _attr_attribution = ATTRIBUTION
69  _attr_should_poll = False
70 
71  _attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS
72  _attr_native_pressure_unit = UnitOfPressure.HPA
73  _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
74  _attr_native_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
75  _attr_native_visibility_unit = UnitOfLength.METERS
76 
77  def __init__(
78  self,
79  name: str,
80  unique_id: str,
81  mode: str,
82  weather_coordinator: WeatherUpdateCoordinator,
83  ) -> None:
84  """Initialize the sensor."""
85  super().__init__(weather_coordinator)
86  self._attr_name_attr_name = name
87  self._attr_unique_id_attr_unique_id = unique_id
88  self._attr_device_info_attr_device_info = DeviceInfo(
89  entry_type=DeviceEntryType.SERVICE,
90  identifiers={(DOMAIN, unique_id)},
91  manufacturer=MANUFACTURER,
92  name=DEFAULT_NAME,
93  )
94 
95  if mode in (OWM_MODE_V30, OWM_MODE_V25):
96  self._attr_supported_features_attr_supported_features = (
97  WeatherEntityFeature.FORECAST_DAILY
98  | WeatherEntityFeature.FORECAST_HOURLY
99  )
100  elif mode == OWM_MODE_FREE_FORECAST:
101  self._attr_supported_features_attr_supported_features = WeatherEntityFeature.FORECAST_HOURLY
102 
103  @property
104  def condition(self) -> str | None:
105  """Return the current condition."""
106  return self.coordinator.data[ATTR_API_CURRENT].get(ATTR_API_CONDITION)
107 
108  @property
109  def cloud_coverage(self) -> float | None:
110  """Return the Cloud coverage in %."""
111  return self.coordinator.data[ATTR_API_CURRENT].get(ATTR_API_CLOUDS)
112 
113  @property
114  def native_apparent_temperature(self) -> float | None:
115  """Return the apparent temperature."""
116  return self.coordinator.data[ATTR_API_CURRENT].get(
117  ATTR_API_FEELS_LIKE_TEMPERATURE
118  )
119 
120  @property
121  def native_temperature(self) -> float | None:
122  """Return the temperature."""
123  return self.coordinator.data[ATTR_API_CURRENT].get(ATTR_API_TEMPERATURE)
124 
125  @property
126  def native_pressure(self) -> float | None:
127  """Return the pressure."""
128  return self.coordinator.data[ATTR_API_CURRENT].get(ATTR_API_PRESSURE)
129 
130  @property
131  def humidity(self) -> float | None:
132  """Return the humidity."""
133  return self.coordinator.data[ATTR_API_CURRENT].get(ATTR_API_HUMIDITY)
134 
135  @property
136  def native_dew_point(self) -> float | None:
137  """Return the dew point."""
138  return self.coordinator.data[ATTR_API_CURRENT].get(ATTR_API_DEW_POINT)
139 
140  @property
141  def native_wind_gust_speed(self) -> float | None:
142  """Return the wind gust speed."""
143  return self.coordinator.data[ATTR_API_CURRENT].get(ATTR_API_WIND_GUST)
144 
145  @property
146  def native_wind_speed(self) -> float | None:
147  """Return the wind speed."""
148  return self.coordinator.data[ATTR_API_CURRENT].get(ATTR_API_WIND_SPEED)
149 
150  @property
151  def wind_bearing(self) -> float | str | None:
152  """Return the wind bearing."""
153  return self.coordinator.data[ATTR_API_CURRENT].get(ATTR_API_WIND_BEARING)
154 
155  @property
156  def visibility(self) -> float | str | None:
157  """Return visibility."""
158  return self.coordinator.data[ATTR_API_CURRENT].get(ATTR_API_VISIBILITY_DISTANCE)
159 
160  @callback
161  def _async_forecast_daily(self) -> list[Forecast] | None:
162  """Return the daily forecast in native units."""
163  return self.coordinator.data[ATTR_API_DAILY_FORECAST]
164 
165  @callback
166  def _async_forecast_hourly(self) -> list[Forecast] | None:
167  """Return the hourly forecast in native units."""
168  return self.coordinator.data[ATTR_API_HOURLY_FORECAST]
None __init__(self, str name, str unique_id, str mode, WeatherUpdateCoordinator weather_coordinator)
Definition: weather.py:83
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, OpenweathermapConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: weather.py:52