99from dataclasses import dataclass
1010from typing import Dict , Set
1111
12- from frequenz .channels import Broadcast , Receiver
12+ from frequenz .channels import Broadcast , Receiver , Sender
1313from frequenz .channels .util import MergeNamed
1414
1515from ..._internal .asyncio import cancel_and_await
@@ -72,13 +72,16 @@ class BatteryPoolStatus:
7272 def __init__ (
7373 self ,
7474 battery_ids : Set [int ],
75+ battery_status_sender : Sender [BatteryStatus ],
7576 max_data_age_sec : float ,
7677 max_blocking_duration_sec : float ,
7778 ) -> None :
7879 """Create BatteryPoolStatus instance.
7980
8081 Args:
8182 battery_ids: set of batteries ids that should be stored in pool.
83+ battery_status_sender: The sender used for sending the status of the
84+ batteries in the pool.
8285 max_data_age_sec: If component stopped sending data, then
8386 this is the maximum time when its last message should be considered as
8487 valid. After that time, component won't be used until it starts sending
@@ -121,7 +124,7 @@ def __init__(
121124 ** receivers ,
122125 )
123126
124- self ._task = asyncio .create_task (self ._run ())
127+ self ._task = asyncio .create_task (self ._run (battery_status_sender ))
125128
126129 async def join (self ) -> None :
127130 """Await for the battery pool, and return when the task completes.
@@ -146,23 +149,30 @@ async def stop(self) -> None:
146149 )
147150 await self ._battery_status_channel .stop ()
148151
149- async def _run (self ) -> None :
150- """Start tracking batteries status."""
152+ async def _run (self , battery_status_sender : Sender [BatteryStatus ]) -> None :
153+ """Start tracking batteries status.
154+
155+ Args:
156+ battery_status_sender: The sender used for sending the status of the
157+ batteries in the pool.
158+ """
151159 while True :
152160 try :
153- await self ._update_status (self . _battery_status_channel )
161+ await self ._update_status (battery_status_sender )
154162 except Exception as err : # pylint: disable=broad-except
155163 _logger .error (
156164 "BatteryPoolStatus failed with error: %s. Restarting." , err
157165 )
158166
159- async def _update_status (self , status_channel : MergeNamed [Status ]) -> None :
167+ async def _update_status (
168+ self , battery_status_sender : Sender [BatteryStatus ]
169+ ) -> None :
160170 """Wait for any battery to change status and update status.
161171
162172 Args:
163- status_channel: Receivers packed in Select object .
173+ battery_status_sender: Sender to send the current status of the batteries .
164174 """
165- async for channel_name , status in status_channel :
175+ async for channel_name , status in self . _battery_status_channel :
166176 battery_id = self ._batteries [channel_name ].battery_id
167177 if status == Status .WORKING :
168178 self ._current_status .working .add (battery_id )
@@ -174,7 +184,7 @@ async def _update_status(self, status_channel: MergeNamed[Status]) -> None:
174184 self ._current_status .working .discard (battery_id )
175185 self ._current_status .uncertain .discard (battery_id )
176186
177- # In the future here we should send status to the subscribed actors
187+ await battery_status_sender . send ( self . _current_status )
178188
179189 async def update_status (
180190 self , succeed_batteries : Set [int ], failed_batteries : Set [int ]
0 commit comments