@@ -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+
2949class 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 :
0 commit comments