Fix atom integration for long term statistics (#54285)
* Fix atom integration for long term statistics * Remove commented code * Fix last_reset syntax * last_reset not an extra attribute * last_reset as utc * black formatting * isort fixpull/54323/head
parent
a1abd4f0d6
commit
d97f93933f
|
@ -14,12 +14,13 @@ from homeassistant.const import (
|
|||
CONF_NAME,
|
||||
CONF_PASSWORD,
|
||||
CONF_USERNAME,
|
||||
DEVICE_CLASS_ENERGY,
|
||||
DEVICE_CLASS_POWER,
|
||||
ENERGY_KILO_WATT_HOUR,
|
||||
POWER_WATT,
|
||||
)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.util import Throttle, dt as dt_util
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -87,12 +88,16 @@ class AtomeData:
|
|||
self._is_connected = None
|
||||
self._day_usage = None
|
||||
self._day_price = None
|
||||
self._day_last_reset = None
|
||||
self._week_usage = None
|
||||
self._week_price = None
|
||||
self._week_last_reset = None
|
||||
self._month_usage = None
|
||||
self._month_price = None
|
||||
self._month_last_reset = None
|
||||
self._year_usage = None
|
||||
self._year_price = None
|
||||
self._year_last_reset = None
|
||||
|
||||
@property
|
||||
def live_power(self):
|
||||
|
@ -137,6 +142,11 @@ class AtomeData:
|
|||
"""Return latest daily usage value."""
|
||||
return self._day_price
|
||||
|
||||
@property
|
||||
def day_last_reset(self):
|
||||
"""Return latest daily last reset."""
|
||||
return self._day_last_reset
|
||||
|
||||
@Throttle(DAILY_SCAN_INTERVAL)
|
||||
def update_day_usage(self):
|
||||
"""Return current daily power usage."""
|
||||
|
@ -144,6 +154,7 @@ class AtomeData:
|
|||
values = self.atome_client.get_consumption(DAILY_TYPE)
|
||||
self._day_usage = values["total"] / 1000
|
||||
self._day_price = values["price"]
|
||||
self._day_last_reset = dt_util.parse_datetime(values["startPeriod"])
|
||||
_LOGGER.debug("Updating Atome daily data. Got: %d", self._day_usage)
|
||||
|
||||
except KeyError as error:
|
||||
|
@ -159,6 +170,11 @@ class AtomeData:
|
|||
"""Return latest weekly usage value."""
|
||||
return self._week_price
|
||||
|
||||
@property
|
||||
def week_last_reset(self):
|
||||
"""Return latest weekly last reset value."""
|
||||
return self._week_last_reset
|
||||
|
||||
@Throttle(WEEKLY_SCAN_INTERVAL)
|
||||
def update_week_usage(self):
|
||||
"""Return current weekly power usage."""
|
||||
|
@ -166,6 +182,7 @@ class AtomeData:
|
|||
values = self.atome_client.get_consumption(WEEKLY_TYPE)
|
||||
self._week_usage = values["total"] / 1000
|
||||
self._week_price = values["price"]
|
||||
self._week_last_reset = dt_util.parse_datetime(values["startPeriod"])
|
||||
_LOGGER.debug("Updating Atome weekly data. Got: %d", self._week_usage)
|
||||
|
||||
except KeyError as error:
|
||||
|
@ -181,6 +198,11 @@ class AtomeData:
|
|||
"""Return latest monthly usage value."""
|
||||
return self._month_price
|
||||
|
||||
@property
|
||||
def month_last_reset(self):
|
||||
"""Return latest monthly last reset value."""
|
||||
return self._month_last_reset
|
||||
|
||||
@Throttle(MONTHLY_SCAN_INTERVAL)
|
||||
def update_month_usage(self):
|
||||
"""Return current monthly power usage."""
|
||||
|
@ -188,6 +210,7 @@ class AtomeData:
|
|||
values = self.atome_client.get_consumption(MONTHLY_TYPE)
|
||||
self._month_usage = values["total"] / 1000
|
||||
self._month_price = values["price"]
|
||||
self._month_last_reset = dt_util.parse_datetime(values["startPeriod"])
|
||||
_LOGGER.debug("Updating Atome monthly data. Got: %d", self._month_usage)
|
||||
|
||||
except KeyError as error:
|
||||
|
@ -203,6 +226,11 @@ class AtomeData:
|
|||
"""Return latest yearly usage value."""
|
||||
return self._year_price
|
||||
|
||||
@property
|
||||
def year_last_reset(self):
|
||||
"""Return latest yearly last reset value."""
|
||||
return self._year_last_reset
|
||||
|
||||
@Throttle(YEARLY_SCAN_INTERVAL)
|
||||
def update_year_usage(self):
|
||||
"""Return current yearly power usage."""
|
||||
|
@ -210,6 +238,7 @@ class AtomeData:
|
|||
values = self.atome_client.get_consumption(YEARLY_TYPE)
|
||||
self._year_usage = values["total"] / 1000
|
||||
self._year_price = values["price"]
|
||||
self._year_last_reset = dt_util.parse_datetime(values["startPeriod"])
|
||||
_LOGGER.debug("Updating Atome yearly data. Got: %d", self._year_usage)
|
||||
|
||||
except KeyError as error:
|
||||
|
@ -219,19 +248,19 @@ class AtomeData:
|
|||
class AtomeSensor(SensorEntity):
|
||||
"""Representation of a sensor entity for Atome."""
|
||||
|
||||
_attr_device_class = DEVICE_CLASS_POWER
|
||||
|
||||
def __init__(self, data, name, sensor_type):
|
||||
"""Initialize the sensor."""
|
||||
self._attr_name = name
|
||||
self._data = data
|
||||
|
||||
self._sensor_type = sensor_type
|
||||
self._attr_state_class = STATE_CLASS_MEASUREMENT
|
||||
|
||||
if sensor_type == LIVE_TYPE:
|
||||
self._attr_device_class = DEVICE_CLASS_POWER
|
||||
self._attr_unit_of_measurement = POWER_WATT
|
||||
self._attr_state_class = STATE_CLASS_MEASUREMENT
|
||||
else:
|
||||
self._attr_device_class = DEVICE_CLASS_ENERGY
|
||||
self._attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR
|
||||
|
||||
def update(self):
|
||||
|
@ -247,6 +276,9 @@ class AtomeSensor(SensorEntity):
|
|||
}
|
||||
else:
|
||||
self._attr_state = getattr(self._data, f"{self._sensor_type}_usage")
|
||||
self._attr_last_reset = dt_util.as_utc(
|
||||
getattr(self._data, f"{self._sensor_type}_last_reset")
|
||||
)
|
||||
self._attr_extra_state_attributes = {
|
||||
"price": getattr(self._data, f"{self._sensor_type}_price")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue