Skip to content

Commit fb3156a

Browse files
Merge pull request #134 from PiotrMachowski/dev
v2.4.11
2 parents fe505cb + f73b869 commit fb3156a

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

custom_components/tauron_amiplus/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"iot_class": "cloud_polling",
99
"issue_tracker": "https://github.com/PiotrMachowski/Home-Assistant-custom-components-Tauron-AMIplus/issues",
1010
"requirements": ["requests"],
11-
"version": "v2.4.10"
11+
"version": "v2.4.11"
1212
}

custom_components/tauron_amiplus/statistics.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
statistics_during_period)
88
from homeassistant.const import ENERGY_KILO_WATT_HOUR
99
from homeassistant.core import HomeAssistant
10-
from homeassistant.util.dt import get_time_zone, utc_from_timestamp
10+
from homeassistant.util.dt import get_time_zone, utc_from_timestamp, as_utc
1111

1212
from .connector import TauronAmiplusConnector, TauronAmiplusRawData
1313
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:
3737

3838
all_stat_ids = await self.prepare_stats_ids(zones)
3939

40-
if not all([v["has_stats"] for v in all_stat_ids.values()]):
40+
if not all([self.are_stats_up_to_date(v["last_stats_end"]) for v in all_stat_ids.values()]):
4141
now = datetime.datetime.now()
4242
start_range = (now - datetime.timedelta(365)).replace(day=1)
4343
data_consumption = await self.hass.async_add_executor_job(self.connector.get_raw_values_daily_for_range,
@@ -56,7 +56,7 @@ async def update_all(self, last_data: TauronAmiplusRawData) -> None:
5656
raw_data[f"{CONST_BALANCED}_{CONST_GENERATION}"] = balanced_generation
5757

5858
for s, v in all_stat_ids.items():
59-
if v["has_stats"]:
59+
if v["last_stats_end"] is not None:
6060
stat = await self.get_stats(raw_data[v["data_source"]], s)
6161
v["sum"] = stat[s][0]["sum"]
6262
start = stat[s][0]["start"]
@@ -113,12 +113,12 @@ async def prepare_stats_ids(self, zones):
113113
"data_source": s["data"],
114114
"sum": 0,
115115
"last_stats_time": None,
116-
"has_stats": False
116+
"last_stats_end": None
117117
}
118118
for s in suffixes
119119
}
120120
for k, v in all_stat_ids.items():
121-
v["has_stats"] = await self.has_stats(k)
121+
v["last_stats_end"] = await self.get_last_stats_date(k)
122122
return all_stat_ids
123123

124124
def get_stats_id(self, suffix):
@@ -127,6 +127,13 @@ def get_stats_id(self, suffix):
127127
def get_stats_name(self, suffix):
128128
return f"{DEFAULT_NAME} {self.meter_id} {suffix}"
129129

130+
@staticmethod
131+
def are_stats_up_to_date(last_stats_end):
132+
if last_stats_end is None:
133+
return False
134+
now = datetime.datetime.now()
135+
return (as_utc(now) - last_stats_end).days < 30
136+
130137
@staticmethod
131138
def prepare_balanced_raw_data(raw_data) -> (dict, dict):
132139
consumption_data = raw_data[CONST_CONSUMPTION]
@@ -198,8 +205,14 @@ async def update_stats(self, statistic_id, statistic_name, initial_sum, last_sta
198205
statistic_data.append(stats)
199206
async_add_external_statistics(self.hass, metadata, statistic_data)
200207

201-
async def has_stats(self, statistic_id):
202-
return len(await self.get_last_stats(statistic_id)) > 0
208+
async def get_last_stats_date(self, statistic_id):
209+
last_stats = await self.get_last_stats(statistic_id)
210+
if statistic_id in last_stats and len(last_stats[statistic_id]) > 0:
211+
end = last_stats[statistic_id][0]["end"]
212+
if isinstance(end, float):
213+
end = utc_from_timestamp(end)
214+
return end
215+
return None
203216

204217
async def get_last_stats(self, statistic_id):
205218
return await get_instance(self.hass).async_add_executor_job(

0 commit comments

Comments
 (0)