Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """The PurpleAir integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from aiopurpleair.models.sensors import SensorModel
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, CONF_SHOW_ON_MAP
12 from homeassistant.helpers.device_registry import DeviceInfo
13 from homeassistant.helpers.update_coordinator import CoordinatorEntity
14 
15 from .const import DOMAIN
16 from .coordinator import PurpleAirDataUpdateCoordinator
17 
18 
19 class PurpleAirEntity(CoordinatorEntity[PurpleAirDataUpdateCoordinator]):
20  """Define a base PurpleAir entity."""
21 
22  _attr_has_entity_name = True
23 
24  def __init__(
25  self,
26  coordinator: PurpleAirDataUpdateCoordinator,
27  entry: ConfigEntry,
28  sensor_index: int,
29  ) -> None:
30  """Initialize."""
31  super().__init__(coordinator)
32 
33  self._sensor_index_sensor_index = sensor_index
34 
35  self._attr_device_info_attr_device_info = DeviceInfo(
36  configuration_url=self.coordinator.async_get_map_url(sensor_index),
37  hw_version=self.sensor_datasensor_data.hardware,
38  identifiers={(DOMAIN, str(sensor_index))},
39  manufacturer="PurpleAir, Inc.",
40  model=self.sensor_datasensor_data.model,
41  name=self.sensor_datasensor_data.name,
42  sw_version=self.sensor_datasensor_data.firmware_version,
43  )
44  self._entry_entry_entry = entry
45 
46  @property
47  def extra_state_attributes(self) -> Mapping[str, Any]:
48  """Return entity specific state attributes."""
49  attrs = {}
50 
51  # Displaying the geography on the map relies upon putting the latitude/longitude
52  # in the entity attributes with "latitude" and "longitude" as the keys.
53  # Conversely, we can hide the location on the map by using other keys, like
54  # "lati" and "long":
55  if self._entry_entry_entry.options.get(CONF_SHOW_ON_MAP):
56  attrs[ATTR_LATITUDE] = self.sensor_datasensor_data.latitude
57  attrs[ATTR_LONGITUDE] = self.sensor_datasensor_data.longitude
58  else:
59  attrs["lati"] = self.sensor_datasensor_data.latitude
60  attrs["long"] = self.sensor_datasensor_data.longitude
61  return attrs
62 
63  @property
64  def sensor_data(self) -> SensorModel:
65  """Define a property to get this entity's SensorModel object."""
66  return self.coordinator.data.data[self._sensor_index_sensor_index]
None __init__(self, PurpleAirDataUpdateCoordinator coordinator, ConfigEntry entry, int sensor_index)
Definition: entity.py:29