Home Assistant Unofficial Reference 2024.12.1
weather.py
Go to the documentation of this file.
1 """Sensor for the zamg integration."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.weather import WeatherEntity
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import (
8  UnitOfPrecipitationDepth,
9  UnitOfPressure,
10  UnitOfSpeed,
11  UnitOfTemperature,
12 )
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 from homeassistant.helpers.update_coordinator import CoordinatorEntity
17 
18 from .const import ATTRIBUTION, CONF_STATION_ID, DOMAIN, MANUFACTURER_URL
19 from .coordinator import ZamgDataUpdateCoordinator
20 
21 
23  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
24 ) -> None:
25  """Set up the ZAMG weather platform."""
26  coordinator = hass.data[DOMAIN][entry.entry_id]
28  [ZamgWeather(coordinator, entry.title, entry.data[CONF_STATION_ID])]
29  )
30 
31 
33  """Representation of a weather condition."""
34 
35  _attr_attribution = ATTRIBUTION
36  _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
37  _attr_native_pressure_unit = UnitOfPressure.HPA
38  _attr_native_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
39  _attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS
40 
41  def __init__(
42  self, coordinator: ZamgDataUpdateCoordinator, name: str, station_id: str
43  ) -> None:
44  """Initialise the platform with a data instance and station name."""
45  super().__init__(coordinator)
46  self._attr_unique_id_attr_unique_id = station_id
47  self._attr_name_attr_name = name
48  self.station_idstation_id = f"{station_id}"
49  self._attr_device_info_attr_device_info = DeviceInfo(
50  entry_type=DeviceEntryType.SERVICE,
51  identifiers={(DOMAIN, station_id)},
52  manufacturer=ATTRIBUTION,
53  configuration_url=MANUFACTURER_URL,
54  name=name,
55  )
56 
57  @property
58  def native_temperature(self) -> float | None:
59  """Return the platform temperature."""
60  try:
61  if (
62  value := self.coordinator.data[self.station_idstation_id]["TLAM"]["data"]
63  ) is not None:
64  return float(value)
65  if (
66  value := self.coordinator.data[self.station_idstation_id]["TL"]["data"]
67  ) is not None:
68  return float(value)
69  except (KeyError, ValueError, TypeError):
70  return None
71  return None
72 
73  @property
74  def native_pressure(self) -> float | None:
75  """Return the pressure."""
76  try:
77  return float(self.coordinator.data[self.station_idstation_id]["P"]["data"])
78  except (KeyError, ValueError, TypeError):
79  return None
80 
81  @property
82  def humidity(self) -> float | None:
83  """Return the humidity."""
84  try:
85  return float(self.coordinator.data[self.station_idstation_id]["RFAM"]["data"])
86  except (KeyError, ValueError, TypeError):
87  return None
88 
89  @property
90  def native_wind_speed(self) -> float | None:
91  """Return the wind speed."""
92  try:
93  if (
94  value := self.coordinator.data[self.station_idstation_id]["FFAM"]["data"]
95  ) is not None:
96  return float(value)
97  if (
98  value := self.coordinator.data[self.station_idstation_id]["FFX"]["data"]
99  ) is not None:
100  return float(value)
101  except (KeyError, ValueError, TypeError):
102  return None
103  return None
104 
105  @property
106  def wind_bearing(self) -> float | None:
107  """Return the wind bearing."""
108  try:
109  if (
110  value := self.coordinator.data[self.station_idstation_id]["DD"]["data"]
111  ) is not None:
112  return float(value)
113  if (
114  value := self.coordinator.data[self.station_idstation_id]["DDX"]["data"]
115  ) is not None:
116  return float(value)
117  except (KeyError, ValueError, TypeError):
118  return None
119  return None
None __init__(self, ZamgDataUpdateCoordinator coordinator, str name, str station_id)
Definition: weather.py:43
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: weather.py:24