Skip to content

Commit

Permalink
Add debug log messages and implement the ability to update energy, vo…
Browse files Browse the repository at this point in the history
…lume and cost independently of each other
  • Loading branch information
Stéphane Senart committed Feb 5, 2025
1 parent c648170 commit d3e75c3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
11 changes: 8 additions & 3 deletions gazpar2haws/date_array.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import datetime as dt
from datetime import timedelta
from typing import Optional, overload

import numpy as np
Expand Down Expand Up @@ -70,8 +71,10 @@ def __getitem__(self, key):
start_date: dt.date = key.start # type: ignore
end_date: dt.date = key.stop # type: ignore
start_index: int = (start_date - self.start_date).days
end_index: int = (end_date - self.start_date).days + 1
return self.array[start_index:end_index]
end_index: int = (end_date - self.start_date).days
if start_index < 0 or end_index > len(self.array):
raise ValueError(f"Date slice [{start_date}:{end_date}] is out of range [{self.start_date}:{self.end_date}]")
return DateArray(start_date=start_date, end_date=end_date + timedelta(-1), array=self.array[start_index:end_index])
raise TypeError("Key must be a date or a slice of dates")

# ----------------------------------
Expand All @@ -95,7 +98,9 @@ def __setitem__(self, key, value: float):
start_date: dt.date = key.start # type: ignore
end_date: dt.date = key.stop # type: ignore
start_index: int = (start_date - self.start_date).days
end_index: int = (end_date - self.start_date).days + 1
end_index: int = (end_date - self.start_date).days
if start_index < 0 or end_index > len(self.array):
raise ValueError(f"Date slice [{start_date}:{end_date}] is out of range [{self.start_date}:{self.end_date}]")
self.array[start_index:end_index] = value
else:
raise TypeError("Key must be a date or a slice of dates")
Expand Down
36 changes: 25 additions & 11 deletions gazpar2haws/gazpar.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ async def publish(self): # pylint: disable=too-many-branches
min(v[0] for v in last_date_and_value_by_sensor.values()) + timedelta(days=1), self._as_of_date
)

# Get all start dates
energy_start_date = last_date_and_value_by_sensor[energy_sensor_name][0] + timedelta(days=1)
volume_start_date = last_date_and_value_by_sensor[volume_sensor_name][0] + timedelta(days=1)
cost_start_date = last_date_and_value_by_sensor[cost_sensor_name][0] + timedelta(days=1)

Logger.debug(f"Min start date for all sensors: {start_date}")
Logger.debug(f"Energy start date: {energy_start_date}")
Logger.debug(f"Volume start date: {volume_start_date}")
Logger.debug(f"Cost start date: {cost_start_date}")

# Fetch the data from GrDF and publish it to Home Assistant
daily_history = self.fetch_daily_gazpar_history(start_date, self._as_of_date)

Expand All @@ -113,11 +123,9 @@ async def publish(self): # pylint: disable=too-many-branches
else:
end_date = datetime.strptime(daily_history[-1][pygazpar.PropertyName.TIME_PERIOD.value], "%d/%m/%Y").date()

Logger.debug(f"Start date: {start_date}, end date: {end_date}")
Logger.debug(f"End date: {end_date}")

# Extract the volume from the daily history
volume_start_date = last_date_and_value_by_sensor[volume_sensor_name][0] + timedelta(days=1)
Logger.debug(f"Volume start date: {volume_start_date}")
volume_array = self.extract_property_from_daily_gazpar_history(
daily_history,
pygazpar.PropertyName.VOLUME.value,
Expand All @@ -126,12 +134,10 @@ async def publish(self): # pylint: disable=too-many-branches
)

# Extract the energy from the daily history
energy_start_date = last_date_and_value_by_sensor[energy_sensor_name][0] + timedelta(days=1)
Logger.debug(f"Energy start date: {energy_start_date}")
energy_array = self.extract_property_from_daily_gazpar_history(
daily_history,
pygazpar.PropertyName.ENERGY.value,
energy_start_date,
min(energy_start_date, cost_start_date),
end_date,
)

Expand All @@ -146,11 +152,11 @@ async def publish(self): # pylint: disable=too-many-branches
else:
Logger.info("No volume data to publish")

if energy_array is not None:
if energy_array is not None and energy_start_date <= end_date:
await self.publish_date_array(
energy_sensor_name,
"kWh",
energy_array,
energy_array[energy_start_date:end_date + timedelta(days=1)],
last_date_and_value_by_sensor[energy_sensor_name][1],
)
else:
Expand All @@ -165,11 +171,11 @@ async def publish(self): # pylint: disable=too-many-branches
pricer = Pricer(self._pricing_config)

quantities = ConsumptionQuantityArray(
start_date=energy_start_date,
start_date=cost_start_date,
end_date=end_date,
value_unit=QuantityUnit.KWH,
base_unit=TimeUnit.DAY,
value_array=energy_array,
value_array=energy_array[cost_start_date:end_date + timedelta(days=1)],
)

cost_array = pricer.compute(quantities, PriceUnit.EURO)
Expand Down Expand Up @@ -209,7 +215,15 @@ def fetch_daily_gazpar_history(self, start_date: date, end_date: date) -> MeterR
endDate=end_date,
frequencies=[pygazpar.Frequency.DAILY],
)
res = history[pygazpar.Frequency.DAILY.value]

# Filter the daily readings by keeping only dates between start_date and end_date
res = []
for reading in history[pygazpar.Frequency.DAILY.value]:
reading_date = datetime.strptime(reading[pygazpar.PropertyName.TIME_PERIOD.value], "%d/%m/%Y").date()
if start_date <= reading_date <= end_date:
res.append(reading)

Logger.debug(f"Fetched {len(res)} daily readings from start date {start_date} to end date {end_date}")
except Exception: # pylint: disable=broad-except
Logger.warning(f"Error while fetching data from GrDF: {traceback.format_exc()}")
res = MeterReadings()
Expand Down
13 changes: 13 additions & 0 deletions tests/test_date_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ def test_date_array():

for i in range(31):
assert date_array10[i] == 5


def test_slice():

date_array = DateArray(start_date=date(2021, 1, 1), end_date=date(2021, 1, 31), initial_value=1)

date_array_slice = date_array[date(2021, 1, 1):date(2021, 1, 11)]

assert len(date_array_slice) == 10

date_array_slice2 = date_array[date(2021, 1, 1):date(2021, 1, 2)]

assert len(date_array_slice2) == 1
2 changes: 1 addition & 1 deletion tests/test_haws.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ async def test_clear_statistics(self):

await self._haws.connect()

await self._haws.clear_statistics(["sensor.gazpar2haws_energy", "sensor.gazpar2haws_volume"])
await self._haws.clear_statistics(["sensor.gazpar2haws_energy", "sensor.gazpar2haws_volume", "sensor.gazpar2haws_cost"])

await self._haws.disconnect()

0 comments on commit d3e75c3

Please sign in to comment.