Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Aurora ABB PowerOne Solar Photovoltaic (PV) inverter."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 from aurorapy.mapping import Mapping as AuroraMapping
10 
12  SensorDeviceClass,
13  SensorEntity,
14  SensorEntityDescription,
15  SensorStateClass,
16 )
17 from homeassistant.const import (
18  ATTR_SERIAL_NUMBER,
19  EntityCategory,
20  UnitOfElectricCurrent,
21  UnitOfElectricPotential,
22  UnitOfEnergy,
23  UnitOfFrequency,
24  UnitOfPower,
25  UnitOfTemperature,
26 )
27 from homeassistant.core import HomeAssistant
28 from homeassistant.helpers.device_registry import DeviceInfo
29 from homeassistant.helpers.entity_platform import AddEntitiesCallback
30 from homeassistant.helpers.typing import StateType
31 from homeassistant.helpers.update_coordinator import CoordinatorEntity
32 
33 from .const import (
34  ATTR_DEVICE_NAME,
35  ATTR_FIRMWARE,
36  ATTR_MODEL,
37  DEFAULT_DEVICE_NAME,
38  DOMAIN,
39  MANUFACTURER,
40 )
41 from .coordinator import AuroraAbbConfigEntry, AuroraAbbDataUpdateCoordinator
42 
43 _LOGGER = logging.getLogger(__name__)
44 ALARM_STATES = list(AuroraMapping.ALARM_STATES.values())
45 
46 SENSOR_TYPES = [
48  key="grid_voltage",
49  device_class=SensorDeviceClass.VOLTAGE,
50  entity_category=EntityCategory.DIAGNOSTIC,
51  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
52  state_class=SensorStateClass.MEASUREMENT,
53  translation_key="grid_voltage",
54  entity_registry_enabled_default=False,
55  ),
57  key="grid_current",
58  device_class=SensorDeviceClass.CURRENT,
59  entity_category=EntityCategory.DIAGNOSTIC,
60  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
61  state_class=SensorStateClass.MEASUREMENT,
62  translation_key="grid_current",
63  entity_registry_enabled_default=False,
64  ),
66  key="grid_frequency",
67  device_class=SensorDeviceClass.FREQUENCY,
68  entity_category=EntityCategory.DIAGNOSTIC,
69  native_unit_of_measurement=UnitOfFrequency.HERTZ,
70  state_class=SensorStateClass.MEASUREMENT,
71  entity_registry_enabled_default=False,
72  ),
74  key="i_leak_dcdc",
75  device_class=SensorDeviceClass.CURRENT,
76  entity_category=EntityCategory.DIAGNOSTIC,
77  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
78  state_class=SensorStateClass.MEASUREMENT,
79  translation_key="i_leak_dcdc",
80  entity_registry_enabled_default=False,
81  ),
83  key="i_leak_inverter",
84  device_class=SensorDeviceClass.CURRENT,
85  entity_category=EntityCategory.DIAGNOSTIC,
86  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
87  state_class=SensorStateClass.MEASUREMENT,
88  translation_key="i_leak_inverter",
89  entity_registry_enabled_default=False,
90  ),
92  key="alarm",
93  device_class=SensorDeviceClass.ENUM,
94  options=ALARM_STATES,
95  entity_category=EntityCategory.DIAGNOSTIC,
96  translation_key="alarm",
97  ),
99  key="instantaneouspower",
100  device_class=SensorDeviceClass.POWER,
101  native_unit_of_measurement=UnitOfPower.WATT,
102  state_class=SensorStateClass.MEASUREMENT,
103  translation_key="power_output",
104  ),
106  key="temp",
107  device_class=SensorDeviceClass.TEMPERATURE,
108  entity_category=EntityCategory.DIAGNOSTIC,
109  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
110  state_class=SensorStateClass.MEASUREMENT,
111  ),
113  key="r_iso",
114  entity_category=EntityCategory.DIAGNOSTIC,
115  native_unit_of_measurement="MOhms",
116  state_class=SensorStateClass.MEASUREMENT,
117  translation_key="r_iso",
118  entity_registry_enabled_default=False,
119  ),
121  key="totalenergy",
122  device_class=SensorDeviceClass.ENERGY,
123  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
124  state_class=SensorStateClass.TOTAL_INCREASING,
125  translation_key="total_energy",
126  ),
127 ]
128 
129 
131  hass: HomeAssistant,
132  config_entry: AuroraAbbConfigEntry,
133  async_add_entities: AddEntitiesCallback,
134 ) -> None:
135  """Set up aurora_abb_powerone sensor based on a config entry."""
136 
137  coordinator = config_entry.runtime_data
138  data = config_entry.data
139 
140  entities = [AuroraSensor(coordinator, data, sens) for sens in SENSOR_TYPES]
141 
142  _LOGGER.debug("async_setup_entry adding %d entities", len(entities))
143  async_add_entities(entities, True)
144 
145 
146 class AuroraSensor(CoordinatorEntity[AuroraAbbDataUpdateCoordinator], SensorEntity):
147  """Representation of a Sensor on an Aurora ABB PowerOne Solar inverter."""
148 
149  _attr_has_entity_name = True
150 
151  def __init__(
152  self,
153  coordinator: AuroraAbbDataUpdateCoordinator,
154  data: Mapping[str, Any],
155  entity_description: SensorEntityDescription,
156  ) -> None:
157  """Initialize the sensor."""
158  super().__init__(coordinator)
159  self.entity_descriptionentity_description = entity_description
160  self._attr_unique_id_attr_unique_id = f"{data[ATTR_SERIAL_NUMBER]}_{entity_description.key}"
161  self._attr_device_info_attr_device_info = DeviceInfo(
162  identifiers={(DOMAIN, data[ATTR_SERIAL_NUMBER])},
163  manufacturer=MANUFACTURER,
164  model=data[ATTR_MODEL],
165  name=data.get(ATTR_DEVICE_NAME, DEFAULT_DEVICE_NAME),
166  sw_version=data[ATTR_FIRMWARE],
167  )
168 
169  @property
170  def native_value(self) -> StateType:
171  """Get the value of the sensor from previously collected data."""
172  return self.coordinator.data.get(self.entity_descriptionentity_description.key)
None __init__(self, AuroraAbbDataUpdateCoordinator coordinator, Mapping[str, Any] data, SensorEntityDescription entity_description)
Definition: sensor.py:156
None async_setup_entry(HomeAssistant hass, AuroraAbbConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:134