Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for WattTime sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any, cast
7 
9  SensorEntity,
10  SensorEntityDescription,
11  SensorStateClass,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import (
15  ATTR_LATITUDE,
16  ATTR_LONGITUDE,
17  CONF_SHOW_ON_MAP,
18  PERCENTAGE,
19  UnitOfMass,
20 )
21 from homeassistant.core import HomeAssistant
22 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 from homeassistant.helpers.typing import StateType
26  CoordinatorEntity,
27  DataUpdateCoordinator,
28 )
29 
30 from .const import CONF_BALANCING_AUTHORITY, CONF_BALANCING_AUTHORITY_ABBREV, DOMAIN
31 
32 ATTR_BALANCING_AUTHORITY = "balancing_authority"
33 
34 SENSOR_TYPE_REALTIME_EMISSIONS_MOER = "moer"
35 SENSOR_TYPE_REALTIME_EMISSIONS_PERCENT = "percent"
36 
37 
38 REALTIME_EMISSIONS_SENSOR_DESCRIPTIONS = (
40  key=SENSOR_TYPE_REALTIME_EMISSIONS_MOER,
41  translation_key="marginal_operating_emissions_rate",
42  native_unit_of_measurement=f"{UnitOfMass.POUNDS} CO2/MWh",
43  state_class=SensorStateClass.MEASUREMENT,
44  ),
46  key=SENSOR_TYPE_REALTIME_EMISSIONS_PERCENT,
47  translation_key="relative_marginal_emissions_intensity",
48  native_unit_of_measurement=PERCENTAGE,
49  state_class=SensorStateClass.MEASUREMENT,
50  ),
51 )
52 
53 
55  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
56 ) -> None:
57  """Set up WattTime sensors based on a config entry."""
58  coordinator = hass.data[DOMAIN][entry.entry_id]
60  [
61  RealtimeEmissionsSensor(coordinator, entry, description)
62  for description in REALTIME_EMISSIONS_SENSOR_DESCRIPTIONS
63  if description.key in coordinator.data
64  ]
65  )
66 
67 
69  """Define a realtime emissions sensor."""
70 
71  _attr_has_entity_name = True
72 
73  def __init__(
74  self,
75  coordinator: DataUpdateCoordinator,
76  entry: ConfigEntry,
77  description: SensorEntityDescription,
78  ) -> None:
79  """Initialize the sensor."""
80  super().__init__(coordinator)
81  self._attr_unique_id_attr_unique_id = f"{entry.entry_id}_{description.key}"
82  self._entry_entry = entry
83  self.entity_descriptionentity_description = description
84  self._attr_device_info_attr_device_info = DeviceInfo(
85  identifiers={(DOMAIN, entry.entry_id)},
86  name=entry.data[CONF_BALANCING_AUTHORITY_ABBREV],
87  entry_type=DeviceEntryType.SERVICE,
88  )
89 
90  @property
91  def extra_state_attributes(self) -> Mapping[str, Any] | None:
92  """Return entity specific state attributes."""
93  attrs = {
94  ATTR_BALANCING_AUTHORITY: self._entry_entry.data[CONF_BALANCING_AUTHORITY],
95  }
96 
97  # Displaying the geography on the map relies upon putting the latitude/longitude
98  # in the entity attributes with "latitude" and "longitude" as the keys.
99  # Conversely, we can hide the location on the map by using other keys, like
100  # "lati" and "long".
101  if self._entry_entry.options.get(CONF_SHOW_ON_MAP) is not False:
102  attrs[ATTR_LATITUDE] = self._entry_entry.data[ATTR_LATITUDE]
103  attrs[ATTR_LONGITUDE] = self._entry_entry.data[ATTR_LONGITUDE]
104  else:
105  attrs["lati"] = self._entry_entry.data[ATTR_LATITUDE]
106  attrs["long"] = self._entry_entry.data[ATTR_LONGITUDE]
107 
108  return attrs
109 
110  @property
111  def native_value(self) -> StateType:
112  """Return the value reported by the sensor."""
113  return cast(StateType, self.coordinator.data[self.entity_descriptionentity_description.key])
None __init__(self, DataUpdateCoordinator coordinator, ConfigEntry entry, SensorEntityDescription description)
Definition: sensor.py:78
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:56