From 66efbd5228734ffff80467d886d08412a3cc2428 Mon Sep 17 00:00:00 2001 From: Piotr Machowski Date: Fri, 5 May 2023 02:31:10 +0200 Subject: [PATCH 1/3] Improve handling missing statistics --- .../tauron_amiplus/statistics.py | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/custom_components/tauron_amiplus/statistics.py b/custom_components/tauron_amiplus/statistics.py index 74a1c67..9af1a3f 100644 --- a/custom_components/tauron_amiplus/statistics.py +++ b/custom_components/tauron_amiplus/statistics.py @@ -7,7 +7,7 @@ statistics_during_period) from homeassistant.const import ENERGY_KILO_WATT_HOUR from homeassistant.core import HomeAssistant -from homeassistant.util.dt import get_time_zone, utc_from_timestamp +from homeassistant.util.dt import get_time_zone, utc_from_timestamp, as_utc from .connector import TauronAmiplusConnector, TauronAmiplusRawData from .const import CONST_BALANCED, CONST_CONSUMPTION, CONST_GENERATION, DEFAULT_NAME, STATISTICS_DOMAIN @@ -37,7 +37,7 @@ async def update_all(self, last_data: TauronAmiplusRawData) -> None: all_stat_ids = await self.prepare_stats_ids(zones) - if not all([v["has_stats"] for v in all_stat_ids.values()]): + if not all([self.are_stats_up_to_date(v["last_stats_end"]) for v in all_stat_ids.values()]): now = datetime.datetime.now() start_range = (now - datetime.timedelta(365)).replace(day=1) data_consumption = await self.hass.async_add_executor_job(self.connector.get_raw_values_daily_for_range, @@ -56,8 +56,8 @@ async def update_all(self, last_data: TauronAmiplusRawData) -> None: raw_data[f"{CONST_BALANCED}_{CONST_GENERATION}"] = balanced_generation for s, v in all_stat_ids.items(): - if v["has_stats"]: - stat = await self.get_stats(raw_data[v["data_source"]], s) + if v["last_stats_end"] is not None: + stat = await self.get_stats(raw_data[v["data_source"]], s) # here v["sum"] = stat[s][0]["sum"] start = stat[s][0]["start"] if isinstance(start, float): @@ -113,12 +113,12 @@ async def prepare_stats_ids(self, zones): "data_source": s["data"], "sum": 0, "last_stats_time": None, - "has_stats": False + "last_stats_end": None } for s in suffixes } for k, v in all_stat_ids.items(): - v["has_stats"] = await self.has_stats(k) + v["last_stats_end"] = await self.get_last_stats_date(k) return all_stat_ids def get_stats_id(self, suffix): @@ -127,6 +127,13 @@ def get_stats_id(self, suffix): def get_stats_name(self, suffix): return f"{DEFAULT_NAME} {self.meter_id} {suffix}" + @staticmethod + def are_stats_up_to_date(last_stats_end): + if last_stats_end is None: + return False + now = datetime.datetime.now() + return (as_utc(now) - last_stats_end).days < 30 + @staticmethod def prepare_balanced_raw_data(raw_data) -> (dict, dict): consumption_data = raw_data[CONST_CONSUMPTION] @@ -198,8 +205,14 @@ async def update_stats(self, statistic_id, statistic_name, initial_sum, last_sta statistic_data.append(stats) async_add_external_statistics(self.hass, metadata, statistic_data) - async def has_stats(self, statistic_id): - return len(await self.get_last_stats(statistic_id)) > 0 + async def get_last_stats_date(self, statistic_id): + last_stats = await self.get_last_stats(statistic_id) + if statistic_id in last_stats and len(last_stats[statistic_id]) > 0: + end = last_stats[statistic_id][0]["end"] + if isinstance(end, float): + end = utc_from_timestamp(end) + return end + return None async def get_last_stats(self, statistic_id): return await get_instance(self.hass).async_add_executor_job( @@ -209,7 +222,7 @@ async def get_last_stats(self, statistic_id): async def get_stats(self, raw_data, statistic_id): return await get_instance(self.hass).async_add_executor_job( statistics_during_period, - self.hass, self.get_time(raw_data[0]), None, [statistic_id], "hour", None, {"state", "sum"}) + self.hass, self.get_time(raw_data[0]), None, [statistic_id], "hour", None, {"state", "sum"}) # here @staticmethod def get_time(raw_hour): From 8ddfde21c9a185e31089e56df90c19f68a58750b Mon Sep 17 00:00:00 2001 From: Piotr Machowski Date: Fri, 5 May 2023 02:32:03 +0200 Subject: [PATCH 2/3] Bump version to v2.4.11 --- custom_components/tauron_amiplus/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/tauron_amiplus/manifest.json b/custom_components/tauron_amiplus/manifest.json index 827c3ea..20adc0a 100644 --- a/custom_components/tauron_amiplus/manifest.json +++ b/custom_components/tauron_amiplus/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "issue_tracker": "https://github.com/PiotrMachowski/Home-Assistant-custom-components-Tauron-AMIplus/issues", "requirements": ["requests"], - "version": "v2.4.10" + "version": "v2.4.11" } From f73b869390736770f4264b8e4ae3877206e10469 Mon Sep 17 00:00:00 2001 From: Piotr Machowski Date: Fri, 5 May 2023 02:32:54 +0200 Subject: [PATCH 3/3] Cleanup --- custom_components/tauron_amiplus/statistics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/tauron_amiplus/statistics.py b/custom_components/tauron_amiplus/statistics.py index 9af1a3f..c15afbf 100644 --- a/custom_components/tauron_amiplus/statistics.py +++ b/custom_components/tauron_amiplus/statistics.py @@ -57,7 +57,7 @@ async def update_all(self, last_data: TauronAmiplusRawData) -> None: for s, v in all_stat_ids.items(): if v["last_stats_end"] is not None: - stat = await self.get_stats(raw_data[v["data_source"]], s) # here + stat = await self.get_stats(raw_data[v["data_source"]], s) v["sum"] = stat[s][0]["sum"] start = stat[s][0]["start"] if isinstance(start, float): @@ -222,7 +222,7 @@ async def get_last_stats(self, statistic_id): async def get_stats(self, raw_data, statistic_id): return await get_instance(self.hass).async_add_executor_job( statistics_during_period, - self.hass, self.get_time(raw_data[0]), None, [statistic_id], "hour", None, {"state", "sum"}) # here + self.hass, self.get_time(raw_data[0]), None, [statistic_id], "hour", None, {"state", "sum"}) @staticmethod def get_time(raw_hour):