Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor for the zamg integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from dataclasses import dataclass
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12  SensorStateClass,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import (
16  DEGREE,
17  PERCENTAGE,
18  UnitOfPrecipitationDepth,
19  UnitOfPressure,
20  UnitOfSpeed,
21  UnitOfTemperature,
22  UnitOfTime,
23 )
24 from homeassistant.core import HomeAssistant
25 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 from homeassistant.helpers.typing import StateType
28 from homeassistant.helpers.update_coordinator import CoordinatorEntity
29 
30 from .const import (
31  ATTR_STATION,
32  ATTR_UPDATED,
33  ATTRIBUTION,
34  CONF_STATION_ID,
35  DOMAIN,
36  MANUFACTURER_URL,
37 )
38 from .coordinator import ZamgDataUpdateCoordinator
39 
40 
41 @dataclass(frozen=True, kw_only=True)
43  """Describes Zamg sensor entity."""
44 
45  para_name: str
46 
47 
48 SENSOR_TYPES: tuple[ZamgSensorEntityDescription, ...] = (
50  key="pressure",
51  name="Pressure",
52  native_unit_of_measurement=UnitOfPressure.HPA,
53  device_class=SensorDeviceClass.PRESSURE,
54  state_class=SensorStateClass.MEASUREMENT,
55  para_name="P",
56  ),
58  key="pressure_sealevel",
59  name="Pressure at Sea Level",
60  native_unit_of_measurement=UnitOfPressure.HPA,
61  device_class=SensorDeviceClass.PRESSURE,
62  state_class=SensorStateClass.MEASUREMENT,
63  para_name="PRED",
64  ),
66  key="humidity",
67  name="Humidity",
68  native_unit_of_measurement=PERCENTAGE,
69  device_class=SensorDeviceClass.HUMIDITY,
70  state_class=SensorStateClass.MEASUREMENT,
71  para_name="RFAM",
72  ),
74  key="wind_speed",
75  name="Wind Speed",
76  native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
77  device_class=SensorDeviceClass.WIND_SPEED,
78  state_class=SensorStateClass.MEASUREMENT,
79  para_name="FFAM",
80  ),
82  key="wind_bearing",
83  name="Wind Bearing",
84  native_unit_of_measurement=DEGREE,
85  state_class=SensorStateClass.MEASUREMENT,
86  para_name="DD",
87  ),
89  key="wind_max_speed",
90  name="Top Wind Speed",
91  native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
92  device_class=SensorDeviceClass.WIND_SPEED,
93  state_class=SensorStateClass.MEASUREMENT,
94  para_name="FFX",
95  ),
97  key="wind_max_bearing",
98  name="Top Wind Bearing",
99  native_unit_of_measurement=DEGREE,
100  state_class=SensorStateClass.MEASUREMENT,
101  para_name="DDX",
102  ),
104  key="sun_last_10min",
105  name="Sun Last 10 Minutes",
106  native_unit_of_measurement=UnitOfTime.SECONDS,
107  state_class=SensorStateClass.MEASUREMENT,
108  para_name="SO",
109  ),
111  key="temperature",
112  name="Temperature",
113  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
114  device_class=SensorDeviceClass.TEMPERATURE,
115  state_class=SensorStateClass.MEASUREMENT,
116  para_name="TL",
117  ),
119  key="temperature_average",
120  name="Temperature Average",
121  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
122  device_class=SensorDeviceClass.TEMPERATURE,
123  state_class=SensorStateClass.MEASUREMENT,
124  para_name="TLAM",
125  ),
127  key="precipitation",
128  name="Precipitation",
129  native_unit_of_measurement=UnitOfPrecipitationDepth.MILLIMETERS,
130  device_class=SensorDeviceClass.PRECIPITATION,
131  state_class=SensorStateClass.MEASUREMENT,
132  para_name="RR",
133  ),
135  key="snow",
136  name="Snow",
137  native_unit_of_measurement=UnitOfPrecipitationDepth.CENTIMETERS,
138  device_class=SensorDeviceClass.PRECIPITATION,
139  state_class=SensorStateClass.MEASUREMENT,
140  para_name="SCHNEE",
141  ),
143  key="dewpoint",
144  name="Dew Point",
145  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
146  device_class=SensorDeviceClass.TEMPERATURE,
147  state_class=SensorStateClass.MEASUREMENT,
148  para_name="TP",
149  ),
151  key="dewpoint_average",
152  name="Dew Point Average",
153  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
154  device_class=SensorDeviceClass.TEMPERATURE,
155  state_class=SensorStateClass.MEASUREMENT,
156  para_name="TPAM",
157  ),
158 )
159 
160 SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
161 
162 API_FIELDS: list[str] = [desc.para_name for desc in SENSOR_TYPES]
163 
164 
166  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
167 ) -> None:
168  """Set up the ZAMG sensor platform."""
169  coordinator = hass.data[DOMAIN][entry.entry_id]
170 
172  ZamgSensor(coordinator, entry.title, entry.data[CONF_STATION_ID], description)
173  for description in SENSOR_TYPES
174  )
175 
176 
178  """Implementation of a ZAMG sensor."""
179 
180  _attr_attribution = ATTRIBUTION
181  entity_description: ZamgSensorEntityDescription
182 
183  def __init__(
184  self,
185  coordinator: ZamgDataUpdateCoordinator,
186  name: str,
187  station_id: str,
188  description: ZamgSensorEntityDescription,
189  ) -> None:
190  """Initialize the sensor."""
191  super().__init__(coordinator)
192  self.entity_descriptionentity_description = description
193  self._attr_name_attr_name = f"{name} {description.name}"
194  self._attr_unique_id_attr_unique_id = f"{station_id}_{description.key}"
195  self.station_idstation_id = f"{station_id}"
196  self._attr_device_info_attr_device_info = DeviceInfo(
197  entry_type=DeviceEntryType.SERVICE,
198  identifiers={(DOMAIN, station_id)},
199  manufacturer=ATTRIBUTION,
200  configuration_url=MANUFACTURER_URL,
201  name=name,
202  )
203  coordinator.api_fields = API_FIELDS
204 
205  @property
206  def native_value(self) -> StateType:
207  """Return the state of the sensor."""
208  try:
209  return self.coordinator.data[self.station_idstation_id][
210  self.entity_descriptionentity_description.para_name
211  ]["data"]
212  except KeyError:
213  return None
214 
215  @property
216  def extra_state_attributes(self) -> Mapping[str, str]:
217  """Return the state attributes."""
218  if (update_time := self.coordinator.data["last_update"]) is not None:
219  update_time = update_time.isoformat()
220  return {
221  ATTR_STATION: self.coordinator.data["Name"],
222  ATTR_UPDATED: update_time,
223  }
None __init__(self, ZamgDataUpdateCoordinator coordinator, str name, str station_id, ZamgSensorEntityDescription description)
Definition: sensor.py:189
Mapping[str, str] extra_state_attributes(self)
Definition: sensor.py:216
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:167