Home Assistant Unofficial Reference 2024.12.1
weather.py
Go to the documentation of this file.
1 """Support for HomematicIP Cloud weather devices."""
2 
3 from __future__ import annotations
4 
5 from homematicip.aio.device import (
6  AsyncWeatherSensor,
7  AsyncWeatherSensorPlus,
8  AsyncWeatherSensorPro,
9 )
10 from homematicip.base.enums import WeatherCondition
11 
13  ATTR_CONDITION_CLOUDY,
14  ATTR_CONDITION_FOG,
15  ATTR_CONDITION_LIGHTNING,
16  ATTR_CONDITION_LIGHTNING_RAINY,
17  ATTR_CONDITION_PARTLYCLOUDY,
18  ATTR_CONDITION_RAINY,
19  ATTR_CONDITION_SNOWY,
20  ATTR_CONDITION_SNOWY_RAINY,
21  ATTR_CONDITION_SUNNY,
22  ATTR_CONDITION_WINDY,
23  WeatherEntity,
24 )
25 from homeassistant.config_entries import ConfigEntry
26 from homeassistant.const import UnitOfSpeed, UnitOfTemperature
27 from homeassistant.core import HomeAssistant
28 from homeassistant.helpers.entity_platform import AddEntitiesCallback
29 
30 from .const import DOMAIN
31 from .entity import HomematicipGenericEntity
32 from .hap import HomematicipHAP
33 
34 HOME_WEATHER_CONDITION = {
35  WeatherCondition.CLEAR: ATTR_CONDITION_SUNNY,
36  WeatherCondition.LIGHT_CLOUDY: ATTR_CONDITION_PARTLYCLOUDY,
37  WeatherCondition.CLOUDY: ATTR_CONDITION_CLOUDY,
38  WeatherCondition.CLOUDY_WITH_RAIN: ATTR_CONDITION_RAINY,
39  WeatherCondition.CLOUDY_WITH_SNOW_RAIN: ATTR_CONDITION_SNOWY_RAINY,
40  WeatherCondition.HEAVILY_CLOUDY: ATTR_CONDITION_CLOUDY,
41  WeatherCondition.HEAVILY_CLOUDY_WITH_RAIN: ATTR_CONDITION_RAINY,
42  WeatherCondition.HEAVILY_CLOUDY_WITH_STRONG_RAIN: ATTR_CONDITION_SNOWY_RAINY,
43  WeatherCondition.HEAVILY_CLOUDY_WITH_SNOW: ATTR_CONDITION_SNOWY,
44  WeatherCondition.HEAVILY_CLOUDY_WITH_SNOW_RAIN: ATTR_CONDITION_SNOWY_RAINY,
45  WeatherCondition.HEAVILY_CLOUDY_WITH_THUNDER: ATTR_CONDITION_LIGHTNING,
46  WeatherCondition.HEAVILY_CLOUDY_WITH_RAIN_AND_THUNDER: ATTR_CONDITION_LIGHTNING_RAINY,
47  WeatherCondition.FOGGY: ATTR_CONDITION_FOG,
48  WeatherCondition.STRONG_WIND: ATTR_CONDITION_WINDY,
49  WeatherCondition.UNKNOWN: "",
50 }
51 
52 
54  hass: HomeAssistant,
55  config_entry: ConfigEntry,
56  async_add_entities: AddEntitiesCallback,
57 ) -> None:
58  """Set up the HomematicIP weather sensor from a config entry."""
59  hap = hass.data[DOMAIN][config_entry.unique_id]
60  entities: list[HomematicipGenericEntity] = []
61  for device in hap.home.devices:
62  if isinstance(device, AsyncWeatherSensorPro):
63  entities.append(HomematicipWeatherSensorPro(hap, device))
64  elif isinstance(device, (AsyncWeatherSensor, AsyncWeatherSensorPlus)):
65  entities.append(HomematicipWeatherSensor(hap, device))
66 
67  entities.append(HomematicipHomeWeather(hap))
68 
69  async_add_entities(entities)
70 
71 
73  """Representation of the HomematicIP weather sensor plus & basic."""
74 
75  _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
76  _attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
77  _attr_attribution = "Powered by Homematic IP"
78 
79  def __init__(self, hap: HomematicipHAP, device) -> None:
80  """Initialize the weather sensor."""
81  super().__init__(hap, device)
82 
83  @property
84  def name(self) -> str:
85  """Return the name of the sensor."""
86  return self._device_device.label
87 
88  @property
89  def native_temperature(self) -> float:
90  """Return the platform temperature."""
91  return self._device_device.actualTemperature
92 
93  @property
94  def humidity(self) -> int:
95  """Return the humidity."""
96  return self._device_device.humidity
97 
98  @property
99  def native_wind_speed(self) -> float:
100  """Return the wind speed."""
101  return self._device_device.windSpeed
102 
103  @property
104  def condition(self) -> str:
105  """Return the current condition."""
106  if getattr(self._device_device, "raining", None):
107  return ATTR_CONDITION_RAINY
108  if self._device_device.storm:
109  return ATTR_CONDITION_WINDY
110  if self._device_device.sunshine:
111  return ATTR_CONDITION_SUNNY
112  return ""
113 
114 
116  """Representation of the HomematicIP weather sensor pro."""
117 
118  @property
119  def wind_bearing(self) -> float:
120  """Return the wind bearing."""
121  return self._device_device.windDirection
122 
123 
125  """Representation of the HomematicIP home weather."""
126 
127  _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
128  _attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
129  _attr_attribution = "Powered by Homematic IP"
130 
131  def __init__(self, hap: HomematicipHAP) -> None:
132  """Initialize the home weather."""
133  hap.home.modelType = "HmIP-Home-Weather"
134  super().__init__(hap, hap.home)
135 
136  @property
137  def available(self) -> bool:
138  """Return if weather entity is available."""
139  return self._home.connected
140 
141  @property
142  def name(self) -> str:
143  """Return the name of the sensor."""
144  return f"Weather {self._home.location.city}"
145 
146  @property
147  def native_temperature(self) -> float:
148  """Return the temperature."""
149  return self._device_device.weather.temperature
150 
151  @property
152  def humidity(self) -> int:
153  """Return the humidity."""
154  return self._device_device.weather.humidity
155 
156  @property
157  def native_wind_speed(self) -> float:
158  """Return the wind speed."""
159  return round(self._device_device.weather.windSpeed, 1)
160 
161  @property
162  def wind_bearing(self) -> float:
163  """Return the wind bearing."""
164  return self._device_device.weather.windDirection
165 
166  @property
167  def condition(self) -> str | None:
168  """Return the current condition."""
169  return HOME_WEATHER_CONDITION.get(self._device_device.weather.weatherCondition)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: weather.py:57