Use more shorthand attrs in emonitor (#116307)
parent
83b5ecb36f
commit
cf682c0c44
|
@ -12,11 +12,10 @@ from homeassistant.components.sensor import (
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import UnitOfPower
|
from homeassistant.const import UnitOfPower
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import (
|
||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
|
@ -63,7 +62,7 @@ async def async_setup_entry(
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class EmonitorPowerSensor(CoordinatorEntity, SensorEntity):
|
class EmonitorPowerSensor(CoordinatorEntity[EmonitorStatus], SensorEntity):
|
||||||
"""Representation of an Emonitor power sensor entity."""
|
"""Representation of an Emonitor power sensor entity."""
|
||||||
|
|
||||||
_attr_device_class = SensorDeviceClass.POWER
|
_attr_device_class = SensorDeviceClass.POWER
|
||||||
|
@ -81,7 +80,8 @@ class EmonitorPowerSensor(CoordinatorEntity, SensorEntity):
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self.channel_number = channel_number
|
self.channel_number = channel_number
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
mac_address = self.emonitor_status.network.mac_address
|
emonitor_status = self.coordinator.data
|
||||||
|
mac_address = 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 str(channel_number)
|
label = self.channel_data.label or str(channel_number)
|
||||||
if description.translation_key is not None:
|
if description.translation_key is not None:
|
||||||
|
@ -94,13 +94,15 @@ class EmonitorPowerSensor(CoordinatorEntity, SensorEntity):
|
||||||
connections={(dr.CONNECTION_NETWORK_MAC, mac_address)},
|
connections={(dr.CONNECTION_NETWORK_MAC, mac_address)},
|
||||||
manufacturer="Powerhouse Dynamics, Inc.",
|
manufacturer="Powerhouse Dynamics, Inc.",
|
||||||
name=device_name,
|
name=device_name,
|
||||||
sw_version=self.emonitor_status.hardware.firmware_version,
|
sw_version=emonitor_status.hardware.firmware_version,
|
||||||
)
|
)
|
||||||
|
self._attr_extra_state_attributes = {"channel": channel_number}
|
||||||
|
self._attr_native_value = self._paired_attr(self.entity_description.key)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def channels(self) -> dict[int, EmonitorChannel]:
|
def channels(self) -> dict[int, EmonitorChannel]:
|
||||||
"""Return the channels dict."""
|
"""Return the channels dict."""
|
||||||
channels: dict[int, EmonitorChannel] = self.emonitor_status.channels
|
channels: dict[int, EmonitorChannel] = self.coordinator.data.channels
|
||||||
return channels
|
return channels
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -108,11 +110,6 @@ class EmonitorPowerSensor(CoordinatorEntity, SensorEntity):
|
||||||
"""Return the channel data."""
|
"""Return the channel data."""
|
||||||
return self.channels[self.channel_number]
|
return self.channels[self.channel_number]
|
||||||
|
|
||||||
@property
|
|
||||||
def emonitor_status(self) -> EmonitorStatus:
|
|
||||||
"""Return the EmonitorStatus."""
|
|
||||||
return self.coordinator.data
|
|
||||||
|
|
||||||
def _paired_attr(self, attr_name: str) -> float:
|
def _paired_attr(self, attr_name: str) -> float:
|
||||||
"""Cumulative attributes for channel and paired channel."""
|
"""Cumulative attributes for channel and paired channel."""
|
||||||
channel_data = self.channels[self.channel_number]
|
channel_data = self.channels[self.channel_number]
|
||||||
|
@ -121,12 +118,8 @@ class EmonitorPowerSensor(CoordinatorEntity, SensorEntity):
|
||||||
attr_val += getattr(self.channels[paired_channel], attr_name)
|
attr_val += getattr(self.channels[paired_channel], attr_name)
|
||||||
return attr_val
|
return attr_val
|
||||||
|
|
||||||
@property
|
@callback
|
||||||
def native_value(self) -> StateType:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""State of the sensor."""
|
"""Handle updated data from the coordinator."""
|
||||||
return self._paired_attr(self.entity_description.key)
|
self._attr_native_value = self._paired_attr(self.entity_description.key)
|
||||||
|
return super()._handle_coordinator_update()
|
||||||
@property
|
|
||||||
def extra_state_attributes(self) -> dict[str, int]:
|
|
||||||
"""Return the device specific state attributes."""
|
|
||||||
return {"channel": self.channel_number}
|
|
||||||
|
|
Loading…
Reference in New Issue