Skip to content

Commit f81859a

Browse files
Make battery_pool variable local in PowerDistributingActor
Move parse_result method to set_distributed_power to decrease nubmer of local variables. Signed-off-by: ela-kotulska-frequenz <[email protected]>
1 parent 34fb5a5 commit f81859a

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/frequenz/sdk/actor/power_distributing/power_distributing.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ def __init__(
161161
self.power_distributor_exponent
162162
)
163163

164-
self._battery_pool: BatteryPoolStatus
165-
166164
self._bat_inv_map, self._inv_bat_map = self._get_components_pairs(
167165
microgrid.get().component_graph
168166
)
@@ -248,7 +246,7 @@ async def run(self) -> None:
248246
await self._create_channels()
249247

250248
api = microgrid.get().api_client
251-
self._battery_pool = await BatteryPoolStatus.async_new(
249+
battery_pool = await BatteryPoolStatus.async_new(
252250
battery_ids=set(self._bat_inv_map.keys()),
253251
max_blocking_duration_sec=30.0,
254252
max_data_age_sec=10.0,
@@ -263,7 +261,7 @@ async def run(self) -> None:
263261

264262
try:
265263
pairs_data: List[InvBatPair] = self._get_components_data(
266-
request.batteries
264+
battery_pool.get_working_batteries(request.batteries)
267265
)
268266
except KeyError as err:
269267
await user.channel.send(Error(request, str(err)))
@@ -295,14 +293,10 @@ async def run(self) -> None:
295293
str(battery_distribution),
296294
)
297295

298-
tasks = await self._set_distributed_power(
296+
failed_power, failed_batteries = await self._set_distributed_power(
299297
api, distribution, request.request_timeout_sec
300298
)
301299

302-
failed_power, failed_batteries = self._parse_result(
303-
tasks, distribution.distribution, request.request_timeout_sec
304-
)
305-
306300
if len(failed_batteries) > 0:
307301
succeed_batteries = set(battery_distribution.keys()) - failed_batteries
308302
response = PartialFailure(
@@ -321,15 +315,15 @@ async def run(self) -> None:
321315
excess_power=distribution.remaining_power,
322316
)
323317

324-
self._battery_pool.update_last_request_status(response)
318+
battery_pool.update_last_request_status(response)
325319
await user.channel.send(response)
326320

327321
async def _set_distributed_power(
328322
self,
329323
api: MicrogridApiClient,
330324
distribution: DistributionResult,
331325
timeout_sec: float,
332-
) -> Dict[int, asyncio.Task[Empty]]:
326+
) -> Tuple[int, Set[int]]:
333327
"""Send distributed power to the inverters.
334328
335329
Args:
@@ -338,7 +332,8 @@ async def _set_distributed_power(
338332
timeout_sec: How long wait for the response
339333
340334
Returns:
341-
Dict with finished or cancelled task for each inverter.
335+
Tuple where first element is total failed power, and the second element
336+
set of batteries that failed.
342337
"""
343338
tasks = {
344339
inverter_id: asyncio.create_task(api.set_power(inverter_id, power))
@@ -352,7 +347,8 @@ async def _set_distributed_power(
352347
)
353348

354349
await self._cancel_tasks(pending)
355-
return tasks
350+
351+
return self._parse_result(tasks, distribution.distribution, timeout_sec)
356352

357353
def _check_request(self, request: Request) -> Optional[Result]:
358354
"""Check whether the given request if correct.
@@ -540,7 +536,7 @@ def _get_components_data(self, batteries: Set[int]) -> List[InvBatPair]:
540536
"""
541537
pairs_data: List[InvBatPair] = []
542538

543-
for battery_id in self._battery_pool.get_working_batteries(batteries):
539+
for battery_id in batteries:
544540
if battery_id not in self._battery_receivers:
545541
raise KeyError(
546542
f"No battery {battery_id}, "

0 commit comments

Comments
 (0)