mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
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
@ -47,50 +47,30 @@ class SmartMeterTexasSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
|
|||||||
_attr_device_class = SensorDeviceClass.ENERGY
|
_attr_device_class = SensorDeviceClass.ENERGY
|
||||||
_attr_state_class = SensorStateClass.TOTAL_INCREASING
|
_attr_state_class = SensorStateClass.TOTAL_INCREASING
|
||||||
_attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
|
_attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
|
||||||
|
_attr_available = False
|
||||||
|
|
||||||
def __init__(self, meter: Meter, coordinator: DataUpdateCoordinator) -> None:
|
def __init__(self, meter: Meter, coordinator: DataUpdateCoordinator) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self.meter = meter
|
self.meter = meter
|
||||||
self._state = None
|
self._attr_name = f"{ELECTRIC_METER} {meter.meter}"
|
||||||
self._available = False
|
self._attr_unique_id = f"{meter.esiid}_{meter.meter}"
|
||||||
|
|
||||||
@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
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the device specific state attributes."""
|
"""Return the device specific state attributes."""
|
||||||
attributes = {
|
return {
|
||||||
METER_NUMBER: self.meter.meter,
|
METER_NUMBER: self.meter.meter,
|
||||||
ESIID: self.meter.esiid,
|
ESIID: self.meter.esiid,
|
||||||
CONF_ADDRESS: self.meter.address,
|
CONF_ADDRESS: self.meter.address,
|
||||||
}
|
}
|
||||||
return attributes
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _state_update(self):
|
def _state_update(self):
|
||||||
"""Call when the coordinator has an update."""
|
"""Call when the coordinator has an update."""
|
||||||
self._available = self.coordinator.last_update_success
|
self._attr_available = self.coordinator.last_update_success
|
||||||
if self._available:
|
if self._attr_available:
|
||||||
self._state = self.meter.reading
|
self._attr_native_value = self.meter.reading
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
@ -104,5 +84,5 @@ class SmartMeterTexasSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if last_state := await self.async_get_last_state():
|
if last_state := await self.async_get_last_state():
|
||||||
self._state = last_state.state
|
self._attr_native_value = last_state.state
|
||||||
self._available = True
|
self._attr_available = True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user