Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor component for PECO outage counter."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Final
8 
10  SensorEntity,
11  SensorEntityDescription,
12  SensorStateClass,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import PERCENTAGE
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.device_registry import DeviceInfo
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20  CoordinatorEntity,
21  DataUpdateCoordinator,
22 )
23 
24 from . import PECOCoordinatorData
25 from .const import ATTR_CONTENT, CONF_COUNTY, DOMAIN
26 
27 
28 @dataclass(frozen=True, kw_only=True)
30  """Description for PECO sensor."""
31 
32  value_fn: Callable[[PECOCoordinatorData], int | str]
33  attribute_fn: Callable[[PECOCoordinatorData], dict[str, str]]
34 
35 
36 PARALLEL_UPDATES: Final = 0
37 SENSOR_LIST: tuple[PECOSensorEntityDescription, ...] = (
39  key="customers_out",
40  translation_key="customers_out",
41  value_fn=lambda data: int(data.outages.customers_out),
42  attribute_fn=lambda data: {},
43  state_class=SensorStateClass.MEASUREMENT,
44  ),
46  key="percent_customers_out",
47  translation_key="percent_customers_out",
48  native_unit_of_measurement=PERCENTAGE,
49  value_fn=lambda data: int(data.outages.percent_customers_out),
50  attribute_fn=lambda data: {},
51  state_class=SensorStateClass.MEASUREMENT,
52  ),
54  key="outage_count",
55  translation_key="outage_count",
56  value_fn=lambda data: int(data.outages.outage_count),
57  attribute_fn=lambda data: {},
58  state_class=SensorStateClass.MEASUREMENT,
59  ),
61  key="customers_served",
62  translation_key="customers_served",
63  value_fn=lambda data: int(data.outages.customers_served),
64  attribute_fn=lambda data: {},
65  state_class=SensorStateClass.MEASUREMENT,
66  ),
68  key="map_alert",
69  translation_key="map_alert",
70  value_fn=lambda data: str(data.alerts.alert_title),
71  attribute_fn=lambda data: {ATTR_CONTENT: data.alerts.alert_content},
72  ),
73 )
74 
75 
77  hass: HomeAssistant,
78  config_entry: ConfigEntry,
79  async_add_entities: AddEntitiesCallback,
80 ) -> None:
81  """Set up the sensor platform."""
82  county: str = config_entry.data[CONF_COUNTY]
83  coordinator = hass.data[DOMAIN][config_entry.entry_id]["outage_count"]
84 
86  PecoSensor(sensor, county, coordinator) for sensor in SENSOR_LIST
87  )
88 
89 
90 class PecoSensor(
91  CoordinatorEntity[DataUpdateCoordinator[PECOCoordinatorData]], SensorEntity
92 ):
93  """PECO outage counter sensor."""
94 
95  entity_description: PECOSensorEntityDescription
96 
97  _attr_has_entity_name = True
98 
99  def __init__(
100  self,
101  description: PECOSensorEntityDescription,
102  county: str,
103  coordinator: DataUpdateCoordinator[PECOCoordinatorData],
104  ) -> None:
105  """Initialize the sensor."""
106  super().__init__(coordinator)
107  self._attr_unique_id_attr_unique_id = f"{county}-{description.key}"
108  self._attr_device_info_attr_device_info = DeviceInfo(
109  identifiers={(DOMAIN, county)}, name=county.capitalize()
110  )
111  self.entity_descriptionentity_description = description
112 
113  @property
114  def native_value(self) -> int | str:
115  """Return the value of the sensor."""
116  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
117 
118  @property
119  def extra_state_attributes(self) -> dict[str, str]:
120  """Return state attributes for the sensor."""
121  return self.entity_descriptionentity_description.attribute_fn(self.coordinator.data)
None __init__(self, PECOSensorEntityDescription description, str county, DataUpdateCoordinator[PECOCoordinatorData] coordinator)
Definition: sensor.py:104
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:80