@@ -266,29 +266,13 @@ async def _distribute_power(
266266 Result from the microgrid API.
267267 """
268268 distributed_power_value = request .power - distribution .remaining_power
269- battery_distribution : dict [frozenset [ComponentId ], Power ] = {}
270269 battery_ids : set [ComponentId ] = set ()
271- for inverter_id , dist in distribution .distribution . items () :
270+ for inverter_id in distribution .distribution :
272271 for battery_id in self ._inv_bats_map [inverter_id ]:
273272 battery_ids .add (battery_id )
274- battery_distribution [self ._inv_bats_map [inverter_id ]] = dist
275- if _logger .isEnabledFor (logging .DEBUG ):
276- _logger .debug (
277- "Distributing power %s between the batteries: %s" ,
278- distributed_power_value ,
279- ", " .join (
280- (
281- str (next (iter (cids )))
282- if len (cids ) == 1
283- else f"({ ', ' .join (str (cid ) for cid in cids )} )"
284- )
285- + f": { power } "
286- for cids , power in battery_distribution .items ()
287- ),
288- )
289273
290274 failed_power , failed_batteries = await self ._set_distributed_power (
291- distribution , self ._api_power_request_timeout
275+ request , distribution , self ._api_power_request_timeout
292276 )
293277
294278 response : Success | PartialFailure
@@ -632,12 +616,14 @@ def _get_power_distribution(
632616
633617 async def _set_distributed_power (
634618 self ,
619+ request : Request ,
635620 distribution : DistributionResult ,
636621 timeout : timedelta ,
637622 ) -> tuple [Power , set [ComponentId ]]:
638623 """Send distributed power to the inverters.
639624
640625 Args:
626+ request: Request to set the power for.
641627 distribution: Distribution result
642628 timeout: How long wait for the response
643629
@@ -654,6 +640,25 @@ async def _set_distributed_power(
654640 for inverter_id , power in distribution .distribution .items ()
655641 }
656642
643+ if _logger .isEnabledFor (logging .DEBUG ):
644+ battery_distribution = {
645+ self ._inv_bats_map [inverter_id ]: distribution .distribution [inverter_id ]
646+ for inverter_id in tasks
647+ }
648+ _logger .debug (
649+ "Distributing power %s between the batteries: %s" ,
650+ request .power - distribution .remaining_power ,
651+ ", " .join (
652+ (
653+ str (next (iter (cids )))
654+ if len (cids ) == 1
655+ else f"({ ', ' .join (str (cid ) for cid in cids )} )"
656+ )
657+ + f": { power } "
658+ for cids , power in battery_distribution .items ()
659+ ),
660+ )
661+
657662 _ , pending = await asyncio .wait (
658663 tasks .values (),
659664 timeout = timeout .total_seconds (),
0 commit comments