Use shorthand attributes in Smart meter texas (#99838)
Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
parent
5ba573a1b4
commit
27c430bbac
1 changed files with 9 additions and 29 deletions
|
@ -47,50 +47,30 @@ class SmartMeterTexasSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
|
|||
_attr_device_class = SensorDeviceClass.ENERGY
|
||||
_attr_state_class = SensorStateClass.TOTAL_INCREASING
|
||||
_attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
|
||||
_attr_available = False
|
||||
|
||||
def __init__(self, meter: Meter, coordinator: DataUpdateCoordinator) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator)
|
||||
self.meter = meter
|
||||
self._state = None
|
||||
self._available = False
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Device Name."""
|
||||
return f"{ELECTRIC_METER} {self.meter.meter}"
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Device Uniqueid."""
|
||||
return f"{self.meter.esiid}_{self.meter.meter}"
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
return self._available
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Get the latest reading."""
|
||||
return self._state
|
||||
self._attr_name = f"{ELECTRIC_METER} {meter.meter}"
|
||||
self._attr_unique_id = f"{meter.esiid}_{meter.meter}"
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return the device specific state attributes."""
|
||||
attributes = {
|
||||
return {
|
||||
METER_NUMBER: self.meter.meter,
|
||||
ESIID: self.meter.esiid,
|
||||
CONF_ADDRESS: self.meter.address,
|
||||
}
|
||||
return attributes
|
||||
|
||||
@callback
|
||||
def _state_update(self):
|
||||
"""Call when the coordinator has an update."""
|
||||
self._available = self.coordinator.last_update_success
|
||||
if self._available:
|
||||
self._state = self.meter.reading
|
||||
self._attr_available = self.coordinator.last_update_success
|
||||
if self._attr_available:
|
||||
self._attr_native_value = self.meter.reading
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
|
@ -104,5 +84,5 @@ class SmartMeterTexasSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
|
|||
return
|
||||
|
||||
if last_state := await self.async_get_last_state():
|
||||
self._state = last_state.state
|
||||
self._available = True
|
||||
self._attr_native_value = last_state.state
|
||||
self._attr_available = True
|
||||
|
|
Loading…
Add table
Reference in a new issue