mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
mill stats
Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
This commit is contained in:
parent
524e468afa
commit
d4173793c4
@ -40,13 +40,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
update_interval = timedelta(seconds=30)
|
update_interval = timedelta(seconds=30)
|
||||||
key = entry.data[CONF_USERNAME]
|
key = entry.data[CONF_USERNAME]
|
||||||
conn_type = CLOUD
|
conn_type = CLOUD
|
||||||
|
|
||||||
historic_data_coordinator = MillHistoricDataUpdateCoordinator(
|
historic_data_coordinator = MillHistoricDataUpdateCoordinator(
|
||||||
hass,
|
hass,
|
||||||
mill_data_connection=mill_data_connection,
|
mill_data_connection=mill_data_connection,
|
||||||
update_interval=update_interval,
|
|
||||||
)
|
)
|
||||||
|
historic_data_coordinator.async_add_listener(lambda: None)
|
||||||
await historic_data_coordinator.async_config_entry_first_refresh()
|
await historic_data_coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not await mill_data_connection.connect():
|
if not await mill_data_connection.connect():
|
||||||
raise ConfigEntryNotReady
|
raise ConfigEntryNotReady
|
||||||
|
@ -25,7 +25,7 @@ from .const import DOMAIN
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
ONE_YEAR = 1 * 365 * 24
|
TWO_YEARS = 2 * 365 * 24
|
||||||
|
|
||||||
|
|
||||||
class MillDataUpdateCoordinator(DataUpdateCoordinator):
|
class MillDataUpdateCoordinator(DataUpdateCoordinator):
|
||||||
@ -56,26 +56,25 @@ class MillHistoricDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
update_interval: timedelta | None = None,
|
|
||||||
*,
|
*,
|
||||||
mill_data_connection: Mill,
|
mill_data_connection: Mill,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize global Mill data updater."""
|
"""Initialize global Mill data updater."""
|
||||||
self.mill_data_connection = mill_data_connection
|
self.mill_data_connection = mill_data_connection
|
||||||
self._last_stats_time = dt_util.utcnow() - timedelta(days=1)
|
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
name=DOMAIN,
|
name="MillHistoricDataUpdateCoordinator",
|
||||||
update_interval=update_interval,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _async_update_data(self):
|
async def _async_update_data(self):
|
||||||
"""Update data via API."""
|
"""Update historic data via API."""
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
if self._last_stats_time > now - timedelta(hours=1):
|
self.update_interval = (
|
||||||
return
|
timedelta(hours=1) + now.replace(minute=0, second=0) - now
|
||||||
|
)
|
||||||
|
|
||||||
for dev_id, heater in self.mill_data_connection.devices.items():
|
for dev_id, heater in self.mill_data_connection.devices.items():
|
||||||
if not isinstance(heater, Heater):
|
if not isinstance(heater, Heater):
|
||||||
continue
|
continue
|
||||||
@ -88,7 +87,7 @@ class MillHistoricDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
if not last_stats or not last_stats.get(statistic_id):
|
if not last_stats or not last_stats.get(statistic_id):
|
||||||
hourly_data = (
|
hourly_data = (
|
||||||
await self.mill_data_connection.fetch_historic_energy_usage(
|
await self.mill_data_connection.fetch_historic_energy_usage(
|
||||||
dev_id, n_days=ONE_YEAR
|
dev_id, n_days=TWO_YEARS
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
hourly_data = dict(sorted(hourly_data.items(), key=lambda x: x[0]))
|
hourly_data = dict(sorted(hourly_data.items(), key=lambda x: x[0]))
|
||||||
@ -108,7 +107,7 @@ class MillHistoricDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
if not hourly_data:
|
if not hourly_data:
|
||||||
return
|
continue
|
||||||
hourly_data = dict(sorted(hourly_data.items(), key=lambda x: x[0]))
|
hourly_data = dict(sorted(hourly_data.items(), key=lambda x: x[0]))
|
||||||
start_time = next(iter(hourly_data))
|
start_time = next(iter(hourly_data))
|
||||||
|
|
||||||
@ -151,5 +150,3 @@ class MillHistoricDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
||||||
)
|
)
|
||||||
async_add_external_statistics(self.hass, metadata, statistics)
|
async_add_external_statistics(self.hass, metadata, statistics)
|
||||||
self._last_stats_time = now.replace(minute=0, second=0)
|
|
||||||
return
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user