1 """Support for a Emonitor channel sensor."""
3 from __future__
import annotations
5 from aioemonitor.monitor
import EmonitorChannel, EmonitorStatus
10 SensorEntityDescription,
20 DataUpdateCoordinator,
23 from .
import EmonitorConfigEntry, name_short_mac
29 translation_key=
"average",
30 entity_registry_enabled_default=
False,
33 key=
"max_power", translation_key=
"max", entity_registry_enabled_default=
False
40 config_entry: EmonitorConfigEntry,
41 async_add_entities: AddEntitiesCallback,
44 coordinator = config_entry.runtime_data
45 channels = coordinator.data.channels
46 entities: list[EmonitorPowerSensor] = []
48 for channel_number, channel
in channels.items():
49 seen_channels.add(channel_number)
50 if not channel.active:
52 if channel.paired_with_channel
in seen_channels:
57 for description
in SENSORS
64 """Representation of an Emonitor power sensor entity."""
66 _attr_device_class = SensorDeviceClass.POWER
67 _attr_native_unit_of_measurement = UnitOfPower.WATT
68 _attr_state_class = SensorStateClass.MEASUREMENT
69 _attr_has_entity_name =
True
73 coordinator: DataUpdateCoordinator,
74 description: SensorEntityDescription,
77 """Initialize the channel sensor."""
81 emonitor_status = self.coordinator.data
82 mac_address = emonitor_status.network.mac_address
85 if description.translation_key
is not None:
87 self.
_attr_unique_id_attr_unique_id = f
"{mac_address}_{channel_number}_{description.key}"
92 connections={(dr.CONNECTION_NETWORK_MAC, mac_address)},
93 manufacturer=
"Powerhouse Dynamics, Inc.",
95 sw_version=emonitor_status.hardware.firmware_version,
102 """Return the channels dict."""
103 channels: dict[int, EmonitorChannel] = self.coordinator.data.channels
108 """Return the channel data."""
112 """Cumulative attributes for channel and paired channel."""
114 attr_val = getattr(channel_data, attr_name)
115 if paired_channel := channel_data.paired_with_channel:
116 attr_val += getattr(self.
channelschannels[paired_channel], attr_name)
121 """Handle updated data from the coordinator."""
EmonitorChannel channel_data(self)
dict[int, EmonitorChannel] channels(self)
None _handle_coordinator_update(self)
None __init__(self, DataUpdateCoordinator coordinator, SensorEntityDescription description, int channel_number)
float _paired_attr(self, str attr_name)
_attr_extra_state_attributes
_attr_translation_placeholders
None async_setup_entry(HomeAssistant hass, EmonitorConfigEntry config_entry, AddEntitiesCallback async_add_entities)
def name_short_mac(short_mac)