Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ async def _send_updated_target_power(
await self._power_distributing_requests_sender.send(
power_distributing.Request(
power=target_power,
batteries=component_ids,
component_ids=component_ids,
request_timeout=request_timeout,
adjust_power=True,
)
Expand Down Expand Up @@ -229,10 +229,12 @@ async def _run(self) -> None:
)

result = selected.value
self._distribution_results[frozenset(result.request.batteries)] = result
self._distribution_results[
frozenset(result.request.component_ids)
] = result
match result:
case power_distributing.PartialFailure(request):
await self._send_updated_target_power(
frozenset(request.batteries), None, must_send=True
frozenset(request.component_ids), None, must_send=True
)
await self._send_reports(frozenset(result.request.batteries))
await self._send_reports(frozenset(result.request.component_ids))
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ async def _run(self) -> None: # pylint: disable=too-many-locals
async for request in self._requests_receiver:
try:
pairs_data: list[InvBatPair] = self._get_components_data(
request.batteries
request.component_ids
)
except KeyError as err:
await self._result_sender.send(Error(request=request, msg=str(err)))
Expand All @@ -306,7 +306,7 @@ async def _run(self) -> None: # pylint: disable=too-many-locals
if not pairs_data:
error_msg = (
"No data for at least one of the given "
f"batteries {str(request.batteries)}"
f"batteries {str(request.component_ids)}"
)
await self._result_sender.send(
Error(request=request, msg=str(error_msg))
Expand Down Expand Up @@ -427,7 +427,7 @@ def _get_power_distribution(
self._bat_bats_map, {pair.battery.component_id for pair in inv_bat_pairs}
)

unavailable_bat_ids = request.batteries - available_bat_ids
unavailable_bat_ids = request.component_ids - available_bat_ids
unavailable_inv_ids: set[int] = set()

for inverter_ids in [
Expand Down Expand Up @@ -455,10 +455,10 @@ def _check_request(
Returns:
Result for the user if the request is wrong, None otherwise.
"""
if not request.batteries:
if not request.component_ids:
return Error(request=request, msg="Empty battery IDs in the request")

for battery in request.batteries:
for battery in request.component_ids:
_logger.debug("Checking battery %d", battery)
if battery not in self._battery_receivers:
msg = (
Expand Down
6 changes: 3 additions & 3 deletions src/frequenz/sdk/actor/power_distributing/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Request:
power: Power
"""The requested power."""

batteries: abc.Set[int]
"""The component ids of the batteries to be used for this request."""
component_ids: abc.Set[int]
"""The component ids of the components to be used for this request."""

request_timeout: timedelta = timedelta(seconds=5.0)
"""The maximum amount of time to wait for the request to be fulfilled."""
Expand All @@ -29,6 +29,6 @@ class Request:
If `True`, the power will be adjusted (lowered) to match the bounds, so
only the reduced power will be set.

If `False` and the power is outside the batteries' bounds, the request will
If `False` and the power is outside the available bounds, the request will
fail and be replied to with an `OutOfBound` result.
"""
70 changes: 35 additions & 35 deletions tests/actor/power_distributing/test_power_distributing.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ async def test_power_distributor_one_user(self, mocker: MockerFixture) -> None:

request = Request(
power=Power.from_kilowatts(1.2),
batteries={9, 19},
component_ids={9, 19},
request_timeout=SAFETY_TIMEOUT,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}
mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
return_value=MagicMock(spec=ComponentPoolStatusTracker, **attrs),
Expand Down Expand Up @@ -202,7 +202,7 @@ async def test_power_distributor_exclusion_bounds(
# zero power requests should pass through despite the exclusion bounds.
request = Request(
power=Power.zero(),
batteries={9, 19},
component_ids={9, 19},
request_timeout=SAFETY_TIMEOUT,
)

Expand All @@ -227,7 +227,7 @@ async def test_power_distributor_exclusion_bounds(
# rejected.
request = Request(
power=Power.from_watts(300.0),
batteries={9, 19},
component_ids={9, 19},
request_timeout=SAFETY_TIMEOUT,
)

Expand Down Expand Up @@ -282,11 +282,11 @@ async def test_two_batteries_one_inverters(self, mocker: MockerFixture) -> None:
results_channel = Broadcast[Result]("power_distributor results")
request = Request(
power=Power.from_watts(1200.0),
batteries={bat_component1.component_id, bat_component2.component_id},
component_ids={bat_component1.component_id, bat_component2.component_id},
request_timeout=SAFETY_TIMEOUT,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}

mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
Expand Down Expand Up @@ -364,11 +364,11 @@ async def test_two_batteries_one_broken_one_inverters(

request = Request(
power=Power.from_watts(1200.0),
batteries=set(battery.component_id for battery in bat_components),
component_ids=set(battery.component_id for battery in bat_components),
request_timeout=SAFETY_TIMEOUT,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}

mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
Expand Down Expand Up @@ -438,11 +438,11 @@ async def test_battery_two_inverters(self, mocker: MockerFixture) -> None:

request = Request(
power=Power.from_watts(1200.0),
batteries={bat_component.component_id},
component_ids={bat_component.component_id},
request_timeout=SAFETY_TIMEOUT,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}

mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
Expand Down Expand Up @@ -516,11 +516,11 @@ async def test_two_batteries_three_inverters(self, mocker: MockerFixture) -> Non

request = Request(
power=Power.from_watts(1700.0),
batteries={batteries[0].component_id, batteries[1].component_id},
component_ids={batteries[0].component_id, batteries[1].component_id},
request_timeout=SAFETY_TIMEOUT,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}

mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
Expand Down Expand Up @@ -605,11 +605,11 @@ async def test_two_batteries_one_inverter_different_exclusion_bounds_2(

request = Request(
power=Power.from_watts(300.0),
batteries={batteries[0].component_id, batteries[1].component_id},
component_ids={batteries[0].component_id, batteries[1].component_id},
request_timeout=SAFETY_TIMEOUT,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}

mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
Expand Down Expand Up @@ -693,11 +693,11 @@ async def test_two_batteries_one_inverter_different_exclusion_bounds(

request = Request(
power=Power.from_watts(300.0),
batteries={batteries[0].component_id, batteries[1].component_id},
component_ids={batteries[0].component_id, batteries[1].component_id},
request_timeout=SAFETY_TIMEOUT,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}

mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
Expand Down Expand Up @@ -764,11 +764,11 @@ async def test_connected_but_not_requested_batteries(

request = Request(
power=Power.from_watts(600.0),
batteries={batteries[0].component_id},
component_ids={batteries[0].component_id},
request_timeout=SAFETY_TIMEOUT,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}

mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
Expand Down Expand Up @@ -827,11 +827,11 @@ async def test_battery_soc_nan(self, mocker: MockerFixture) -> None:

request = Request(
power=Power.from_kilowatts(1.2),
batteries={9, 19},
component_ids={9, 19},
request_timeout=SAFETY_TIMEOUT,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}
mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
return_value=MagicMock(spec=ComponentPoolStatusTracker, **attrs),
Expand All @@ -844,7 +844,7 @@ async def test_battery_soc_nan(self, mocker: MockerFixture) -> None:
results_sender=results_channel.new_sender(),
battery_status_sender=battery_status_channel.new_sender(),
):
attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}
mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing"
".ComponentPoolStatusTracker",
Expand Down Expand Up @@ -892,10 +892,10 @@ async def test_battery_capacity_nan(self, mocker: MockerFixture) -> None:

request = Request(
power=Power.from_kilowatts(1.2),
batteries={9, 19},
component_ids={9, 19},
request_timeout=SAFETY_TIMEOUT,
)
attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}
mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
return_value=MagicMock(spec=ComponentPoolStatusTracker, **attrs),
Expand Down Expand Up @@ -964,10 +964,10 @@ async def test_battery_power_bounds_nan(self, mocker: MockerFixture) -> None:

request = Request(
power=Power.from_kilowatts(1.2),
batteries={9, 19},
component_ids={9, 19},
request_timeout=SAFETY_TIMEOUT,
)
attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}
mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
return_value=MagicMock(spec=ComponentPoolStatusTracker, **attrs),
Expand Down Expand Up @@ -1013,11 +1013,11 @@ async def test_power_distributor_invalid_battery_id(
results_channel = Broadcast[Result]("power_distributor results")
request = Request(
power=Power.from_kilowatts(1.2),
batteries={9, 100},
component_ids={9, 100},
request_timeout=SAFETY_TIMEOUT,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}
mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
return_value=MagicMock(spec=ComponentPoolStatusTracker, **attrs),
Expand Down Expand Up @@ -1061,12 +1061,12 @@ async def test_power_distributor_one_user_adjust_power_consume(

request = Request(
power=Power.from_kilowatts(1.2),
batteries={9, 19},
component_ids={9, 19},
request_timeout=SAFETY_TIMEOUT,
adjust_power=False,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}
mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
return_value=MagicMock(spec=ComponentPoolStatusTracker, **attrs),
Expand Down Expand Up @@ -1113,12 +1113,12 @@ async def test_power_distributor_one_user_adjust_power_supply(

request = Request(
power=-Power.from_kilowatts(1.2),
batteries={9, 19},
component_ids={9, 19},
request_timeout=SAFETY_TIMEOUT,
adjust_power=False,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}
mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
return_value=MagicMock(spec=ComponentPoolStatusTracker, **attrs),
Expand Down Expand Up @@ -1165,12 +1165,12 @@ async def test_power_distributor_one_user_adjust_power_success(

request = Request(
power=Power.from_kilowatts(1.0),
batteries={9, 19},
component_ids={9, 19},
request_timeout=SAFETY_TIMEOUT,
adjust_power=False,
)

attrs = {"get_working_components.return_value": request.batteries}
attrs = {"get_working_components.return_value": request.component_ids}
mocker.patch(
"frequenz.sdk.actor.power_distributing.power_distributing.ComponentPoolStatusTracker",
return_value=MagicMock(spec=ComponentPoolStatusTracker, **attrs),
Expand Down Expand Up @@ -1231,7 +1231,7 @@ async def test_not_all_batteries_are_working(self, mocker: MockerFixture) -> Non
):
request = Request(
power=Power.from_kilowatts(1.2),
batteries=batteries,
component_ids=batteries,
request_timeout=SAFETY_TIMEOUT,
)

Expand Down Expand Up @@ -1292,7 +1292,7 @@ async def test_partial_failure_result(self, mocker: MockerFixture) -> None:
):
request = Request(
power=Power.from_kilowatts(1.70),
batteries=batteries,
component_ids=batteries,
request_timeout=SAFETY_TIMEOUT,
)

Expand Down