@@ -55,55 +55,53 @@ class _ComponentStreamStatus:
5555
5656@dataclass
5757class _BlockingStatus :
58- min_duration_sec : float
59- """The minimum blocking duration (in seconds) ."""
58+ min_duration : timedelta
59+ """The minimum blocking duration."""
6060
61- max_duration_sec : float
62- """The maximum blocking duration (in seconds) ."""
61+ max_duration : timedelta
62+ """The maximum blocking duration."""
6363
64- last_blocking_duration_sec : float = 0.0
65- """Last blocking duration (in seconds) ."""
64+ last_blocking_duration : timedelta = timedelta ( seconds = 0.0 )
65+ """Last blocking duration."""
6666
6767 blocked_until : datetime | None = None
6868 """Until when the battery is blocked."""
6969
7070 def __post_init__ (self ) -> None :
71- assert self .min_duration_sec <= self .max_duration_sec , (
72- f"Minimum blocking duration ({ self .min_duration_sec } ) cannot be greater "
73- f"than maximum blocking duration ({ self .max_duration_sec } )"
71+ assert self .min_duration <= self .max_duration , (
72+ f"Minimum blocking duration ({ self .min_duration } ) cannot be greater "
73+ f"than maximum blocking duration ({ self .max_duration } )"
7474 )
75- self .last_blocking_duration_sec = self .min_duration_sec
75+ self .last_blocking_duration = self .min_duration
7676
77- def block (self ) -> float :
77+ def block (self ) -> timedelta :
7878 """Block battery.
7979
8080 Battery can be unblocked using `self.unblock()` method.
8181
8282 Returns:
83- For how long (in seconds) the battery is blocked.
83+ The duration for which the battery is blocked.
8484 """
8585 now = datetime .now (tz = timezone .utc )
8686
8787 # If is not blocked
8888 if self .blocked_until is None :
89- self .last_blocking_duration_sec = self .min_duration_sec
90- self .blocked_until = now + timedelta (
91- seconds = self .last_blocking_duration_sec
92- )
93- return self .last_blocking_duration_sec
89+ self .last_blocking_duration = self .min_duration
90+ self .blocked_until = now + self .last_blocking_duration
91+ return self .last_blocking_duration
9492
9593 # If still blocked, then do nothing
9694 if self .blocked_until > now :
97- return 0.0
95+ return timedelta ( seconds = 0.0 )
9896
9997 # If previous blocking time expired, then blocked it once again.
10098 # Increase last blocking time, unless it reach the maximum.
101- self .last_blocking_duration_sec = min (
102- 2 * self .last_blocking_duration_sec , self .max_duration_sec
99+ self .last_blocking_duration = min (
100+ 2 * self .last_blocking_duration , self .max_duration
103101 )
104- self .blocked_until = now + timedelta ( seconds = self .last_blocking_duration_sec )
102+ self .blocked_until = now + self .last_blocking_duration
105103
106- return self .last_blocking_duration_sec
104+ return self .last_blocking_duration
107105
108106 def unblock (self ) -> None :
109107 """Unblock battery.
@@ -166,20 +164,19 @@ class BatteryStatusTracker(ComponentStatusTracker):
166164 def __init__ ( # pylint: disable=too-many-arguments
167165 self ,
168166 component_id : int ,
169- max_data_age_sec : float ,
170- max_blocking_duration_sec : float ,
167+ max_data_age : timedelta ,
168+ max_blocking_duration : timedelta ,
171169 status_sender : Sender [ComponentStatus ],
172170 set_power_result_receiver : Receiver [SetPowerResult ],
173171 ) -> None :
174172 """Create class instance.
175173
176174 Args:
177175 component_id: Id of this battery
178- max_data_age_sec: If component stopped sending data, then
179- this is the maximum time when its last message should be considered as
180- valid. After that time, component won't be used until it starts sending
181- data.
182- max_blocking_duration_sec: This value tell what should be the maximum
176+ max_data_age: If component stopped sending data, then this is the maximum
177+ time when its last message should be considered as valid. After that
178+ time, component won't be used until it starts sending data.
179+ max_blocking_duration: This value tell what should be the maximum
183180 timeout used for blocking failing component.
184181 status_sender: Channel to send status updates.
185182 set_power_result_receiver: Channel to receive results of the requests to the
@@ -188,12 +185,12 @@ def __init__( # pylint: disable=too-many-arguments
188185 Raises:
189186 RuntimeError: If battery has no adjacent inverter.
190187 """
191- self ._max_data_age = max_data_age_sec
188+ self ._max_data_age = max_data_age
192189 # First battery is considered as not working.
193190 # Change status after first messages are received.
194191 self ._last_status : ComponentStatusEnum = ComponentStatusEnum .NOT_WORKING
195192 self ._blocking_status : _BlockingStatus = _BlockingStatus (
196- 1.0 , max_blocking_duration_sec
193+ timedelta ( seconds = 1.0 ), max_blocking_duration
197194 )
198195
199196 inverter_id = self ._find_adjacent_inverter_id (component_id )
@@ -204,11 +201,11 @@ def __init__( # pylint: disable=too-many-arguments
204201
205202 self ._battery : _ComponentStreamStatus = _ComponentStreamStatus (
206203 component_id ,
207- data_recv_timer = Timer .timeout (timedelta ( seconds = max_data_age_sec ) ),
204+ data_recv_timer = Timer .timeout (max_data_age ),
208205 )
209206 self ._inverter : _ComponentStreamStatus = _ComponentStreamStatus (
210207 inverter_id ,
211- data_recv_timer = Timer .timeout (timedelta ( seconds = max_data_age_sec ) ),
208+ data_recv_timer = Timer .timeout (max_data_age ),
212209 )
213210
214211 # Select needs receivers that can be get in async way only.
@@ -259,9 +256,9 @@ def _handle_status_set_power_result(self, result: SetPowerResult) -> None:
259256 ):
260257 duration = self ._blocking_status .block ()
261258
262- if duration > 0 :
259+ if duration > timedelta ( seconds = 0.0 ) :
263260 _logger .warning (
264- "battery %d failed last response. block it for %f sec " ,
261+ "battery %d failed last response. block it for %s " ,
265262 self .battery_id ,
266263 duration ,
267264 )
@@ -345,7 +342,7 @@ async def _run(
345342 if (
346343 datetime .now (tz = timezone .utc )
347344 - self ._battery .last_msg_timestamp
348- ) < timedelta ( seconds = self ._max_data_age ) :
345+ ) < self ._max_data_age :
349346 # This means that we have received data from the battery
350347 # since the timer triggered, but the timer event arrived
351348 # late, so we can ignore it.
@@ -356,7 +353,7 @@ async def _run(
356353 if (
357354 datetime .now (tz = timezone .utc )
358355 - self ._inverter .last_msg_timestamp
359- ) < timedelta ( seconds = self ._max_data_age ) :
356+ ) < self ._max_data_age :
360357 # This means that we have received data from the inverter
361358 # since the timer triggered, but the timer event arrived
362359 # late, so we can ignore it.
@@ -505,7 +502,7 @@ def _is_timestamp_outdated(self, timestamp: datetime) -> bool:
505502 _True if timestamp is to old, False otherwise
506503 """
507504 now = datetime .now (tz = timezone .utc )
508- diff = ( now - timestamp ). total_seconds ()
505+ diff = now - timestamp
509506 return diff > self ._max_data_age
510507
511508 def _is_message_reliable (self , message : ComponentData ) -> bool :
0 commit comments