Skip to content

Commit 5693da1

Browse files
Fix method to compute lower bound in power distributing
Do small refactoring of power distributing module. Signed-off-by: ela-kotulska-frequenz <[email protected]>
1 parent 5a850f5 commit 5693da1

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from dataclasses import dataclass
2020
from datetime import datetime, timezone
2121
from enum import Enum
22+
from math import ceil, floor
2223
from typing import ( # pylint: disable=unused-import
2324
Any,
2425
Dict,
@@ -300,7 +301,7 @@ def _create_users_tasks(self) -> None:
300301
for user, handler in self._users_channels.items():
301302
asyncio.create_task(self._wait_for_request(_User(user, handler)))
302303

303-
def get_upper_bound(self, batteries: Set[int]) -> float:
304+
def _get_upper_bound(self, batteries: Set[int]) -> int:
304305
"""Get total upper bound of power to be set for given batteries.
305306
306307
Note, output of that function doesn't guarantee that this bound will be
@@ -313,12 +314,13 @@ def get_upper_bound(self, batteries: Set[int]) -> float:
313314
Upper bound for `set_power` operation.
314315
"""
315316
pairs_data: List[InvBatPair] = self._get_components_data(batteries)
316-
return sum(
317+
bound = sum(
317318
min(battery.power_upper_bound, inverter.active_power_upper_bound)
318319
for battery, inverter in pairs_data
319320
)
321+
return floor(bound)
320322

321-
def get_lower_bound(self, batteries: Set[int]) -> float:
323+
def _get_lower_bound(self, batteries: Set[int]) -> int:
322324
"""Get total lower bound of power to be set for given batteries.
323325
324326
Note, output of that function doesn't guarantee that this bound will be
@@ -331,10 +333,11 @@ def get_lower_bound(self, batteries: Set[int]) -> float:
331333
Lower bound for `set_power` operation.
332334
"""
333335
pairs_data: List[InvBatPair] = self._get_components_data(batteries)
334-
return sum(
335-
min(battery.power_lower_bound, inverter.active_power_lower_bound)
336+
bound = sum(
337+
max(battery.power_lower_bound, inverter.active_power_lower_bound)
336338
for battery, inverter in pairs_data
337339
)
340+
return ceil(bound)
338341

339342
def _within_bounds(self, request: Request) -> bool:
340343
"""Check whether the requested power is withing the bounds.
@@ -346,8 +349,8 @@ def _within_bounds(self, request: Request) -> bool:
346349
True if power is between the bounds, False otherwise.
347350
"""
348351
power = request.power
349-
lower_bound = self.get_lower_bound(request.batteries)
350-
return lower_bound <= power <= self.get_upper_bound(request.batteries)
352+
lower_bound = self._get_lower_bound(request.batteries)
353+
return lower_bound <= power <= self._get_upper_bound(request.batteries)
351354

352355
async def run(self) -> None:
353356
"""Run actor main function.
@@ -360,7 +363,7 @@ async def run(self) -> None:
360363
"""
361364
await self._create_channels()
362365

363-
# Wait 2 seconds to get data from the channels created above.
366+
# Wait few seconds to get data from the channels created above.
364367
await asyncio.sleep(self._wait_for_data_sec)
365368
self._started.set()
366369
while True:

0 commit comments

Comments
 (0)