1 """Platform for sensor integration."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
9 from energyflip.const
import (
10 SOURCE_TYPE_ELECTRICITY,
11 SOURCE_TYPE_ELECTRICITY_IN,
12 SOURCE_TYPE_ELECTRICITY_IN_LOW,
13 SOURCE_TYPE_ELECTRICITY_OUT,
14 SOURCE_TYPE_ELECTRICITY_OUT_LOW,
21 SensorEntityDescription,
36 DataUpdateCoordinator,
44 SENSOR_TYPE_THIS_MONTH,
45 SENSOR_TYPE_THIS_WEEK,
46 SENSOR_TYPE_THIS_YEAR,
49 _LOGGER = logging.getLogger(__name__)
52 @dataclass(frozen=True)
54 """Class describing Airly sensor entities."""
56 sensor_type: str = SENSOR_TYPE_RATE
61 translation_key=
"current_power",
62 sensor_type=SENSOR_TYPE_RATE,
63 device_class=SensorDeviceClass.POWER,
64 native_unit_of_measurement=UnitOfPower.WATT,
65 key=SOURCE_TYPE_ELECTRICITY,
66 state_class=SensorStateClass.MEASUREMENT,
69 translation_key=
"current_power_peak",
70 sensor_type=SENSOR_TYPE_RATE,
71 device_class=SensorDeviceClass.POWER,
72 native_unit_of_measurement=UnitOfPower.WATT,
73 key=SOURCE_TYPE_ELECTRICITY_IN,
74 state_class=SensorStateClass.MEASUREMENT,
77 translation_key=
"current_power_off_peak",
78 sensor_type=SENSOR_TYPE_RATE,
79 device_class=SensorDeviceClass.POWER,
80 native_unit_of_measurement=UnitOfPower.WATT,
81 key=SOURCE_TYPE_ELECTRICITY_IN_LOW,
82 state_class=SensorStateClass.MEASUREMENT,
85 translation_key=
"current_power_out_peak",
86 sensor_type=SENSOR_TYPE_RATE,
87 device_class=SensorDeviceClass.POWER,
88 native_unit_of_measurement=UnitOfPower.WATT,
89 key=SOURCE_TYPE_ELECTRICITY_OUT,
90 state_class=SensorStateClass.MEASUREMENT,
93 translation_key=
"current_power_out_off_peak",
94 sensor_type=SENSOR_TYPE_RATE,
95 device_class=SensorDeviceClass.POWER,
96 native_unit_of_measurement=UnitOfPower.WATT,
97 key=SOURCE_TYPE_ELECTRICITY_OUT_LOW,
98 state_class=SensorStateClass.MEASUREMENT,
101 translation_key=
"energy_consumption_peak_today",
102 device_class=SensorDeviceClass.ENERGY,
103 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
104 key=SOURCE_TYPE_ELECTRICITY_IN,
105 sensor_type=SENSOR_TYPE_THIS_DAY,
106 state_class=SensorStateClass.TOTAL_INCREASING,
107 suggested_display_precision=3,
110 translation_key=
"energy_consumption_off_peak_today",
111 device_class=SensorDeviceClass.ENERGY,
112 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
113 key=SOURCE_TYPE_ELECTRICITY_IN_LOW,
114 sensor_type=SENSOR_TYPE_THIS_DAY,
115 state_class=SensorStateClass.TOTAL_INCREASING,
116 suggested_display_precision=3,
119 translation_key=
"energy_production_peak_today",
120 device_class=SensorDeviceClass.ENERGY,
121 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
122 key=SOURCE_TYPE_ELECTRICITY_OUT,
123 sensor_type=SENSOR_TYPE_THIS_DAY,
124 state_class=SensorStateClass.TOTAL_INCREASING,
125 suggested_display_precision=3,
128 translation_key=
"energy_production_off_peak_today",
129 device_class=SensorDeviceClass.ENERGY,
130 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
131 key=SOURCE_TYPE_ELECTRICITY_OUT_LOW,
132 sensor_type=SENSOR_TYPE_THIS_DAY,
133 state_class=SensorStateClass.TOTAL_INCREASING,
134 suggested_display_precision=3,
137 translation_key=
"energy_today",
138 device_class=SensorDeviceClass.ENERGY,
139 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
140 state_class=SensorStateClass.TOTAL,
141 key=SOURCE_TYPE_ELECTRICITY,
142 sensor_type=SENSOR_TYPE_THIS_DAY,
143 suggested_display_precision=1,
146 translation_key=
"energy_week",
147 device_class=SensorDeviceClass.ENERGY,
148 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
149 state_class=SensorStateClass.TOTAL,
150 key=SOURCE_TYPE_ELECTRICITY,
151 sensor_type=SENSOR_TYPE_THIS_WEEK,
152 suggested_display_precision=1,
155 translation_key=
"energy_month",
156 device_class=SensorDeviceClass.ENERGY,
157 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
158 state_class=SensorStateClass.TOTAL,
159 key=SOURCE_TYPE_ELECTRICITY,
160 sensor_type=SENSOR_TYPE_THIS_MONTH,
161 suggested_display_precision=1,
164 translation_key=
"energy_year",
165 device_class=SensorDeviceClass.ENERGY,
166 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
167 state_class=SensorStateClass.TOTAL,
168 key=SOURCE_TYPE_ELECTRICITY,
169 sensor_type=SENSOR_TYPE_THIS_YEAR,
170 suggested_display_precision=1,
173 translation_key=
"current_gas",
174 native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR,
175 sensor_type=SENSOR_TYPE_RATE,
176 state_class=SensorStateClass.MEASUREMENT,
178 suggested_display_precision=2,
181 translation_key=
"gas_today",
182 device_class=SensorDeviceClass.GAS,
183 native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
185 sensor_type=SENSOR_TYPE_THIS_DAY,
186 state_class=SensorStateClass.TOTAL_INCREASING,
187 suggested_display_precision=2,
190 translation_key=
"gas_week",
191 device_class=SensorDeviceClass.GAS,
192 native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
194 sensor_type=SENSOR_TYPE_THIS_WEEK,
195 state_class=SensorStateClass.TOTAL_INCREASING,
196 suggested_display_precision=2,
199 translation_key=
"gas_month",
200 device_class=SensorDeviceClass.GAS,
201 native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
203 sensor_type=SENSOR_TYPE_THIS_MONTH,
204 state_class=SensorStateClass.TOTAL_INCREASING,
205 suggested_display_precision=2,
208 translation_key=
"gas_year",
209 device_class=SensorDeviceClass.GAS,
210 native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
212 sensor_type=SENSOR_TYPE_THIS_YEAR,
213 state_class=SensorStateClass.TOTAL_INCREASING,
214 suggested_display_precision=2,
221 config_entry: ConfigEntry,
222 async_add_entities: AddEntitiesCallback,
224 """Set up the sensor platform."""
225 coordinator: DataUpdateCoordinator[dict[str, dict[str, Any]]] = hass.data[DOMAIN][
226 config_entry.entry_id
228 user_id = config_entry.data[CONF_ID]
232 for description
in SENSORS_INFO
237 CoordinatorEntity[DataUpdateCoordinator[dict[str, dict[str, Any]]]], SensorEntity
239 """Defines a EnergyFlip sensor."""
241 entity_description: EnergyFlipSensorEntityDescription
242 _attr_has_entity_name =
True
246 coordinator: DataUpdateCoordinator[dict[str, dict[str, Any]]],
248 description: EnergyFlipSensorEntityDescription,
250 """Initialize the sensor."""
256 f
"{DOMAIN}_{user_id}_{description.key}_{description.sensor_type}"
261 """Return the state of the sensor."""
272 """Return if entity is available."""
275 and self.coordinator.data
276 and self.
_source_type_source_type
in self.coordinator.data
277 and self.coordinator.data[self.
_source_type_source_type]
int|float|None native_value(self)
None __init__(self, DataUpdateCoordinator[dict[str, dict[str, Any]]] coordinator, str user_id, EnergyFlipSensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)