Home Assistant Unofficial Reference 2024.12.1
weather.py
Go to the documentation of this file.
1 """Support for KNX/IP weather station."""
2 
3 from __future__ import annotations
4 
5 from xknx import XKNX
6 from xknx.devices import Weather as XknxWeather
7 
8 from homeassistant import config_entries
9 from homeassistant.components.weather import WeatherEntity
10 from homeassistant.const import (
11  CONF_ENTITY_CATEGORY,
12  CONF_NAME,
13  Platform,
14  UnitOfPressure,
15  UnitOfSpeed,
16  UnitOfTemperature,
17 )
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.helpers.typing import ConfigType
21 
22 from . import KNXModule
23 from .const import KNX_MODULE_KEY
24 from .entity import KnxYamlEntity
25 from .schema import WeatherSchema
26 
27 
29  hass: HomeAssistant,
30  config_entry: config_entries.ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up switch(es) for KNX platform."""
34  knx_module = hass.data[KNX_MODULE_KEY]
35  config: list[ConfigType] = knx_module.config_yaml[Platform.WEATHER]
36 
38  KNXWeather(knx_module, entity_config) for entity_config in config
39  )
40 
41 
42 def _create_weather(xknx: XKNX, config: ConfigType) -> XknxWeather:
43  """Return a KNX weather device to be used within XKNX."""
44  return XknxWeather(
45  xknx,
46  name=config[CONF_NAME],
47  sync_state=config[WeatherSchema.CONF_SYNC_STATE],
48  group_address_temperature=config[WeatherSchema.CONF_KNX_TEMPERATURE_ADDRESS],
49  group_address_brightness_south=config.get(
50  WeatherSchema.CONF_KNX_BRIGHTNESS_SOUTH_ADDRESS
51  ),
52  group_address_brightness_east=config.get(
53  WeatherSchema.CONF_KNX_BRIGHTNESS_EAST_ADDRESS
54  ),
55  group_address_brightness_west=config.get(
56  WeatherSchema.CONF_KNX_BRIGHTNESS_WEST_ADDRESS
57  ),
58  group_address_brightness_north=config.get(
59  WeatherSchema.CONF_KNX_BRIGHTNESS_NORTH_ADDRESS
60  ),
61  group_address_wind_speed=config.get(WeatherSchema.CONF_KNX_WIND_SPEED_ADDRESS),
62  group_address_wind_bearing=config.get(
63  WeatherSchema.CONF_KNX_WIND_BEARING_ADDRESS
64  ),
65  group_address_rain_alarm=config.get(WeatherSchema.CONF_KNX_RAIN_ALARM_ADDRESS),
66  group_address_frost_alarm=config.get(
67  WeatherSchema.CONF_KNX_FROST_ALARM_ADDRESS
68  ),
69  group_address_wind_alarm=config.get(WeatherSchema.CONF_KNX_WIND_ALARM_ADDRESS),
70  group_address_day_night=config.get(WeatherSchema.CONF_KNX_DAY_NIGHT_ADDRESS),
71  group_address_air_pressure=config.get(
72  WeatherSchema.CONF_KNX_AIR_PRESSURE_ADDRESS
73  ),
74  group_address_humidity=config.get(WeatherSchema.CONF_KNX_HUMIDITY_ADDRESS),
75  )
76 
77 
79  """Representation of a KNX weather device."""
80 
81  _device: XknxWeather
82  _attr_native_pressure_unit = UnitOfPressure.PA
83  _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
84  _attr_native_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
85 
86  def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
87  """Initialize of a KNX sensor."""
88  super().__init__(
89  knx_module=knx_module,
90  device=_create_weather(knx_module.xknx, config),
91  )
92  self._attr_unique_id_attr_unique_id = str(self._device_device._temperature.group_address_state) # noqa: SLF001
93  self._attr_entity_category_attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
94 
95  @property
96  def native_temperature(self) -> float | None:
97  """Return current temperature in C."""
98  return self._device_device.temperature
99 
100  @property
101  def native_pressure(self) -> float | None:
102  """Return current air pressure in Pa."""
103  return self._device_device.air_pressure
104 
105  @property
106  def condition(self) -> str:
107  """Return current weather condition."""
108  return self._device_device.ha_current_state().value
109 
110  @property
111  def humidity(self) -> float | None:
112  """Return current humidity."""
113  return self._device_device.humidity
114 
115  @property
116  def wind_bearing(self) -> int | None:
117  """Return current wind bearing in degrees."""
118  return self._device_device.wind_bearing
119 
120  @property
121  def native_wind_speed(self) -> float | None:
122  """Return current wind speed in m/s."""
123  return self._device_device.wind_speed
None __init__(self, KNXModule knx_module, ConfigType config)
Definition: weather.py:86
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: weather.py:32
XknxWeather _create_weather(XKNX xknx, ConfigType config)
Definition: weather.py:42