Skip to content

Commit 64e570d

Browse files
authored
Fix #31 energy_current_hour will show wrong value with paid subscription (#37)
* #31 Correction of energy_current_hour evaluation * Cleanup and typos corrections * Fix black error
1 parent a1ef549 commit 64e570d

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

forecast_solar/models.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ def _timed_value(at: datetime, data: dict[datetime, int]) -> int | None:
2626
return None
2727

2828

29+
def _interval_value_sum(
30+
interval_begin: datetime, interval_end: datetime, data: dict[datetime, int]
31+
) -> int:
32+
"""Return the sum of values in interval."""
33+
34+
total = 0
35+
36+
for timestamp, wh in data.items():
37+
# Skip all until this hour
38+
if timestamp < interval_begin:
39+
continue
40+
41+
if timestamp >= interval_end:
42+
break
43+
44+
total += wh
45+
46+
return total
47+
48+
2949
class AccountType(str, Enum):
3050
"""Enumeration representing the Forecast.Solar account type."""
3151

@@ -77,9 +97,11 @@ def energy_production_tomorrow(self) -> int:
7797
@property
7898
def energy_production_today_remaining(self) -> int:
7999
"""Return estimated energy produced in rest of today."""
80-
return self.sum_energy_production_in_interval(
100+
return _interval_value_sum(
81101
self.now(),
82-
self.now().replace(hour=0, minute=0, second=0) + timedelta(days=1),
102+
self.now().replace(hour=0, minute=0, second=0, microsecond=0)
103+
+ timedelta(days=1),
104+
self.wh_period,
83105
)
84106

85107
@property
@@ -100,7 +122,11 @@ def power_highest_peak_time_tomorrow(self) -> datetime:
100122
@property
101123
def energy_current_hour(self) -> int:
102124
"""Return the estimated energy production for the current hour."""
103-
return _timed_value(self.now(), self.wh_period) or 0
125+
return _interval_value_sum(
126+
self.now().replace(minute=0, second=0, microsecond=0),
127+
self.now().replace(minute=0, second=0, microsecond=0) + timedelta(hours=1),
128+
self.wh_period,
129+
)
104130

105131
def day_production(self, specific_date: date) -> int:
106132
"""Return the day production."""
@@ -133,29 +159,10 @@ def power_production_at_time(self, time: datetime) -> int:
133159

134160
def sum_energy_production(self, period_hours: int) -> int:
135161
"""Return the sum of the energy production."""
136-
now = self.now().replace(minute=59, second=59)
162+
now = self.now().replace(minute=59, second=59, microsecond=999)
137163
until = now + timedelta(hours=period_hours)
138164

139-
return self.sum_energy_production_in_interval(now, until)
140-
141-
def sum_energy_production_in_interval(
142-
self, interval_begin: datetime, interval_end: datetime
143-
) -> int:
144-
"""Return the sum of the energy production in interval."""
145-
146-
total = 0
147-
148-
for timestamp, wh in self.wh_period.items():
149-
# Skip all dates until this hour
150-
if timestamp < interval_begin:
151-
continue
152-
153-
if timestamp > interval_end:
154-
break
155-
156-
total += wh
157-
158-
return total
165+
return _interval_value_sum(now, until, self.wh_period)
159166

160167
@classmethod
161168
def from_dict(cls, data: dict[str, Any]) -> Estimate:

tests/test_models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def test_estimate_previous_day(patch_previous_day):
1616
assert estimate.energy_production_tomorrow == 5435
1717

1818
assert estimate.power_production_now == 0
19-
assert estimate.energy_current_hour == 140
19+
# production this hour at 23:48 is zero
20+
assert estimate.energy_current_hour == 0
2021

2122
assert estimate.power_highest_peak_time_today == datetime.fromisoformat(
2223
"2022-10-15T15:00:00+02:00"
@@ -70,7 +71,8 @@ def test_estimate_near_end(patch_near_end_today):
7071
assert estimate.energy_production_tomorrow == 5435
7172

7273
assert estimate.power_production_now == 337
73-
assert estimate.energy_current_hour == 502
74+
# production this hour at 16:48 is sum of values between 16:00 and 16:59:59.999
75+
assert estimate.energy_current_hour == 642
7476

7577
assert estimate.power_highest_peak_time_today == datetime.fromisoformat(
7678
"2022-10-15T15:00:00+02:00"

0 commit comments

Comments
 (0)