Skip to content

Commit

Permalink
Don't check new periods on every home assistant restart
Browse files Browse the repository at this point in the history
  • Loading branch information
yinyang17 committed Mar 17, 2024
1 parent a24070b commit 1c1e1fc
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions custom_components/pvpc_energy/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ async def import_energy_data(hass, force_update=False):
last_consumption_date = datetime.datetime.fromtimestamp(max(list(consumptions.keys()))).date()
start_billing_date = min(start_date, first_consumption_date)

billing_periods = await PvpcCoordinator.get_billing_periods(start_billing_date)
get_new_periods = last_consumption_date is None or end_date > last_consumption_date
billing_periods = await PvpcCoordinator.get_billing_periods(start_billing_date, get_new_periods)

if force_update or first_consumption_date is None or first_consumption_date > start_date:
await PvpcCoordinator.get_data(UFD.consumptions, start_date, end_date, consumptions, 14, force_update)
Expand Down Expand Up @@ -169,14 +170,14 @@ async def get_data(getter, start_date, end_date, data, days, force_update=False)

_LOGGER.debug(f"END - get_data: new_data=={len(data)-data_len}")

async def get_billing_periods(first_consumption_date):
_LOGGER.debug(f"START - get_billing_periods(first_consumption_date={first_consumption_date})")
async def get_billing_periods(start_billing_date, get_new_periods):
_LOGGER.debug(f"START - get_billing_periods(start_billing_date={start_billing_date}, get_new_periods={get_new_periods})")
billing_periods = PvpcCoordinator.load_billing_periods(BILLING_PERIODS_FILE)

if first_consumption_date:
if start_billing_date:
remove_periods = []
for billing_period in billing_periods:
if billing_period['start_date'] < first_consumption_date:
if billing_period['start_date'] < start_billing_date:
remove_periods.append(billing_period)
if len(remove_periods) > 0:
for billing_period in remove_periods:
Expand All @@ -197,7 +198,7 @@ async def get_billing_periods(first_consumption_date):

end_date = datetime.date.today()
_LOGGER.debug(f"start_date={start_date.isoformat()}, end_date={end_date.isoformat()}, (end_date - start_date).days={(end_date - start_date).days}")
if (end_date - start_date).days > 25:
if get_new_periods and (end_date - start_date).days > 25:
new_billing_periods = await UFD.billingPeriods(start_date, end_date)
if len(new_billing_periods) > 0:
billing_periods += new_billing_periods
Expand Down

0 comments on commit 1c1e1fc

Please sign in to comment.