Migrate Emonitor to has entity name (#107153)

This commit is contained in:
Joost Lekkerkerker 2024-01-04 21:17:23 +01:00 committed by GitHub
parent eb320b69bc
commit bf229be7bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -27,10 +27,12 @@ from .const import DOMAIN
SENSORS = ( SENSORS = (
SensorEntityDescription(key="inst_power"), SensorEntityDescription(key="inst_power"),
SensorEntityDescription( SensorEntityDescription(
key="avg_power", name="Average", entity_registry_enabled_default=False key="avg_power",
translation_key="average",
entity_registry_enabled_default=False,
), ),
SensorEntityDescription( SensorEntityDescription(
key="max_power", name="Max", entity_registry_enabled_default=False key="max_power", translation_key="max", entity_registry_enabled_default=False
), ),
) )
@ -66,6 +68,7 @@ class EmonitorPowerSensor(CoordinatorEntity, SensorEntity):
_attr_device_class = SensorDeviceClass.POWER _attr_device_class = SensorDeviceClass.POWER
_attr_native_unit_of_measurement = UnitOfPower.WATT _attr_native_unit_of_measurement = UnitOfPower.WATT
_attr_state_class = SensorStateClass.MEASUREMENT _attr_state_class = SensorStateClass.MEASUREMENT
_attr_has_entity_name = True
def __init__( def __init__(
self, self,
@ -79,9 +82,9 @@ class EmonitorPowerSensor(CoordinatorEntity, SensorEntity):
super().__init__(coordinator) super().__init__(coordinator)
mac_address = self.emonitor_status.network.mac_address mac_address = self.emonitor_status.network.mac_address
device_name = name_short_mac(mac_address[-6:]) device_name = name_short_mac(mac_address[-6:])
label = self.channel_data.label or f"{device_name} {channel_number}" label = self.channel_data.label or str(channel_number)
if description.name is not UNDEFINED: if description.name is not UNDEFINED:
self._attr_name = f"{label} {description.name}" self._attr_translation_placeholders = {"label": label}
self._attr_unique_id = f"{mac_address}_{channel_number}_{description.key}" self._attr_unique_id = f"{mac_address}_{channel_number}_{description.key}"
else: else:
self._attr_name = label self._attr_name = label

View File

@ -22,5 +22,15 @@
"abort": { "abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]" "already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
} }
},
"entity": {
"sensor": {
"average": {
"name": "{label} average"
},
"max": {
"name": "{label} max"
}
}
} }
} }