1 """Support for monitoring a Sense energy sensor."""
3 from datetime
import datetime
5 from sense_energy
import ASyncSenseable, Scale
6 from sense_energy.sense_api
import SenseDevice
15 UnitOfElectricPotential,
22 from .
import SenseConfigEntry
40 from .coordinator
import SenseRealtimeCoordinator, SenseTrendCoordinator
41 from .entity
import SenseDeviceEntity, SenseEntity
44 TRENDS_SENSOR_TYPES = {
47 Scale.MONTH:
"Monthly",
53 SENSOR_VARIANTS = [(PRODUCTION_ID, PRODUCTION_NAME), (CONSUMPTION_ID, CONSUMPTION_NAME)]
56 TREND_SENSOR_VARIANTS = [
58 (PRODUCTION_PCT_ID, PRODUCTION_PCT_NAME),
59 (NET_PRODUCTION_ID, NET_PRODUCTION_NAME),
60 (FROM_GRID_ID, FROM_GRID_NAME),
61 (TO_GRID_ID, TO_GRID_NAME),
62 (SOLAR_POWERED_ID, SOLAR_POWERED_NAME),
68 config_entry: SenseConfigEntry,
69 async_add_entities: AddEntitiesCallback,
71 """Set up the Sense sensor."""
72 data = config_entry.runtime_data.data
73 trends_coordinator = config_entry.runtime_data.trends
74 realtime_coordinator = config_entry.runtime_data.rt
78 await trends_coordinator.async_request_refresh()
80 sense_monitor_id = data.sense_monitor_id
82 entities: list[SensorEntity] = []
84 for device
in config_entry.runtime_data.data.devices:
93 for variant_id, variant_name
in SENSOR_VARIANTS:
96 data, sense_monitor_id, variant_id, variant_name, realtime_coordinator
102 for i
in range(len(data.active_voltage))
106 for variant_id, variant_name
in TREND_SENSOR_VARIANTS:
122 """Implementation of a Sense energy sensor."""
124 _attr_device_class = SensorDeviceClass.POWER
125 _attr_native_unit_of_measurement = UnitOfPower.WATT
126 _attr_state_class = SensorStateClass.MEASUREMENT
130 gateway: ASyncSenseable,
131 sense_monitor_id: str,
134 realtime_coordinator: SenseRealtimeCoordinator,
136 """Initialize the Sense sensor."""
139 realtime_coordinator,
141 f
"{ACTIVE_TYPE}-{variant_id}",
148 """Return the state of the sensor."""
157 """Implementation of a Sense energy voltage sensor."""
159 _attr_device_class = SensorDeviceClass.VOLTAGE
160 _attr_state_class = SensorStateClass.MEASUREMENT
161 _attr_native_unit_of_measurement = UnitOfElectricPotential.VOLT
165 gateway: ASyncSenseable,
167 sense_monitor_id: str,
168 realtime_coordinator: SenseRealtimeCoordinator,
170 """Initialize the Sense sensor."""
172 gateway, realtime_coordinator, sense_monitor_id, f
"L{index + 1}"
179 """Return the state of the sensor."""
184 """Implementation of a Sense energy sensor."""
188 gateway: ASyncSenseable,
192 trends_coordinator: SenseTrendCoordinator,
193 sense_monitor_id: str,
195 """Initialize the Sense sensor."""
200 f
"{TRENDS_SENSOR_TYPES[scale].lower()}-{variant_id}",
202 self.
_attr_name_attr_name = f
"{TRENDS_SENSOR_TYPES[scale]} {variant_name}"
206 if variant_id
in [PRODUCTION_PCT_ID, SOLAR_POWERED_ID]:
218 """Return the state of the sensor."""
223 """Return the time when the sensor was last reset, if any."""
230 """Implementation of a Sense energy device."""
232 _attr_state_class = SensorStateClass.MEASUREMENT
233 _attr_native_unit_of_measurement = UnitOfPower.WATT
234 _attr_device_class = SensorDeviceClass.POWER
239 sense_monitor_id: str,
240 coordinator: SenseRealtimeCoordinator,
242 """Initialize the Sense device sensor."""
244 device, coordinator, sense_monitor_id, f
"{device.id}-{CONSUMPTION_ID}"
249 """Return the state of the sensor."""
250 return self.
_device_device.power_w
254 """Implementation of a Sense device energy sensor."""
256 _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
257 _attr_state_class = SensorStateClass.TOTAL_INCREASING
258 _attr_device_class = SensorDeviceClass.ENERGY
264 coordinator: SenseTrendCoordinator,
265 sense_monitor_id: str,
267 """Initialize the Sense device sensor."""
272 f
"{device.id}-{TRENDS_SENSOR_TYPES[scale].lower()}-energy",
281 """Return the state of the sensor."""
None __init__(self, SenseDevice device, Scale scale, SenseTrendCoordinator coordinator, str sense_monitor_id)
_attr_suggested_display_precision
None __init__(self, SenseDevice device, str sense_monitor_id, SenseRealtimeCoordinator coordinator)
None __init__(self, ASyncSenseable gateway, str sense_monitor_id, str variant_id, str variant_name, SenseRealtimeCoordinator realtime_coordinator)
_attr_entity_registry_enabled_default
datetime|None last_reset(self)
None __init__(self, ASyncSenseable gateway, Scale scale, str variant_id, str variant_name, SenseTrendCoordinator trends_coordinator, str sense_monitor_id)
_attr_native_unit_of_measurement
None __init__(self, ASyncSenseable gateway, int index, str sense_monitor_id, SenseRealtimeCoordinator realtime_coordinator)
None async_setup_entry(HomeAssistant hass, SenseConfigEntry config_entry, AddEntitiesCallback async_add_entities)