|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2026 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Abstract base class for component pools.""" |
| 5 | + |
| 6 | +import asyncio |
| 7 | +import uuid |
| 8 | +from abc import ABC, abstractmethod |
| 9 | +from collections import abc |
| 10 | + |
| 11 | +from frequenz.client.common.microgrid.components import ComponentId |
| 12 | +from frequenz.quantities import Power |
| 13 | + |
| 14 | +from frequenz.sdk._internal._channels import MappingReceiverFetcher, ReceiverFetcher |
| 15 | +from frequenz.sdk.microgrid import _power_distributing, _power_managing |
| 16 | +from frequenz.sdk.timeseries import Bounds |
| 17 | +from frequenz.sdk.timeseries._base_types import SystemBounds |
| 18 | +from frequenz.sdk.timeseries.abstract_pool._abstract_pool_reference_store import ( |
| 19 | + AbstractPoolReferenceStore, |
| 20 | +) |
| 21 | +from frequenz.sdk.timeseries.formulas import Formula |
| 22 | + |
| 23 | + |
| 24 | +class AbstractPool(ABC): |
| 25 | + """Abstract base class for component pools.""" |
| 26 | + |
| 27 | + def __init__( # pylint: disable=too-many-arguments |
| 28 | + self, |
| 29 | + *, |
| 30 | + pool_ref_store: AbstractPoolReferenceStore, |
| 31 | + name: str | None, |
| 32 | + priority: int, |
| 33 | + ) -> None: |
| 34 | + """Create an `AbstractPool` instance. |
| 35 | +
|
| 36 | + Args: |
| 37 | + pool_ref_store: The pool reference store instance. |
| 38 | + name: An optional name used to identify this instance of the pool or a |
| 39 | + corresponding actor in the logs. |
| 40 | + priority: The priority of the actor using this wrapper. |
| 41 | + """ |
| 42 | + self._pool_ref_store = pool_ref_store |
| 43 | + unique_id = str(uuid.uuid4()) |
| 44 | + self._source_id = unique_id if name is None else f"{name}-{unique_id}" |
| 45 | + self._priority = priority |
| 46 | + |
| 47 | + @property |
| 48 | + def component_ids(self) -> abc.Set[ComponentId]: |
| 49 | + """Return component IDs of all component IDs managed by this pool. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + Set of managed component IDs. |
| 53 | + """ |
| 54 | + return self._pool_ref_store.component_ids |
| 55 | + |
| 56 | + async def propose_power( |
| 57 | + self, |
| 58 | + power: Power | None, |
| 59 | + bounds: Bounds[Power | None] = Bounds(None, None), |
| 60 | + ) -> None: |
| 61 | + """Send a proposal to the power manager for the pool's underlying components. |
| 62 | +
|
| 63 | + This proposal is for the maximum power that can be set for the components in |
| 64 | + the pool. The actual production or consumption might be lower. |
| 65 | +
|
| 66 | + Details on how the power manager handles proposals can be found in the |
| 67 | + [Microgrid][frequenz.sdk.microgrid--setting-power] documentation. |
| 68 | +
|
| 69 | + Args: |
| 70 | + power: The power to propose. If `None`, |
| 71 | + this proposal will not have any effect on the target power, unless |
| 72 | + bounds are specified. When specified without bounds, bounds for lower |
| 73 | + priority actors will be shifted by this power. If both are `None`, it |
| 74 | + is equivalent to not having a proposal or withdrawing a previous one. |
| 75 | + bounds: The power bounds for the proposal. When specified, these bounds will |
| 76 | + limit the bounds for lower priority actors. |
| 77 | + """ |
| 78 | + await self._pool_ref_store.power_manager_requests_sender.send( |
| 79 | + _power_managing.Proposal( |
| 80 | + source_id=self._source_id, |
| 81 | + preferred_power=power, |
| 82 | + bounds=bounds, |
| 83 | + component_ids=self._pool_ref_store.component_ids, |
| 84 | + priority=self._priority, |
| 85 | + creation_time=asyncio.get_running_loop().time(), |
| 86 | + ) |
| 87 | + ) |
| 88 | + |
| 89 | + @property |
| 90 | + @abstractmethod |
| 91 | + def power(self) -> Formula[Power]: |
| 92 | + """Fetch the total power for the components in the pool. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + A Formula that will calculate and stream the total power of all |
| 96 | + components in the pool. |
| 97 | + """ |
| 98 | + |
| 99 | + @property |
| 100 | + def power_status(self) -> ReceiverFetcher[_power_managing._Report]: |
| 101 | + """Get a receiver to receive new power status reports when they change. |
| 102 | +
|
| 103 | + These include |
| 104 | + - the current inclusion/exclusion bounds available for the pool's priority, |
| 105 | + - the current target power for the pool's set of components, |
| 106 | + - the result of the last distribution request for the pool's set of components,. |
| 107 | +
|
| 108 | + Returns: |
| 109 | + A receiver that will stream power status reports for the pool's priority. |
| 110 | + """ |
| 111 | + sub = _power_managing.ReportRequest( |
| 112 | + source_id=self._source_id, |
| 113 | + priority=self._priority, |
| 114 | + component_ids=self._pool_ref_store.component_ids, |
| 115 | + ) |
| 116 | + self._pool_ref_store.power_bounds_subs[sub.get_channel_name()] = ( |
| 117 | + asyncio.create_task( |
| 118 | + self._pool_ref_store.power_manager_bounds_subs_sender.send(sub) |
| 119 | + ) |
| 120 | + ) |
| 121 | + channel = self._pool_ref_store.channel_registry.get_or_create( |
| 122 | + _power_managing._Report, # pylint: disable=protected-access |
| 123 | + sub.get_channel_name(), |
| 124 | + ) |
| 125 | + channel.resend_latest = True |
| 126 | + |
| 127 | + return channel |
| 128 | + |
| 129 | + @property |
| 130 | + def power_distribution_results(self) -> ReceiverFetcher[_power_distributing.Result]: |
| 131 | + """Get a receiver to receive power distribution results. |
| 132 | +
|
| 133 | + Returns: |
| 134 | + A receiver that will stream power distribution results for the pool's set of |
| 135 | + components. |
| 136 | + """ |
| 137 | + return MappingReceiverFetcher( |
| 138 | + self._pool_ref_store.power_distribution_results_fetcher, |
| 139 | + lambda recv: recv.filter( |
| 140 | + lambda x: x.request.component_ids == self._pool_ref_store.component_ids |
| 141 | + ), |
| 142 | + ) |
| 143 | + |
| 144 | + @property |
| 145 | + def _system_power_bounds(self) -> ReceiverFetcher[SystemBounds]: |
| 146 | + """Return a receiver fetcher for the system power bounds.""" |
| 147 | + return self._pool_ref_store.bounds_channel |
| 148 | + |
| 149 | + async def stop(self) -> None: |
| 150 | + """Stop all tasks and channels owned by the pool.""" |
| 151 | + # This was closing the pool_ref_store, which is not correct, because those are |
| 152 | + # shared. |
| 153 | + # |
| 154 | + # This method will do until we have a mechanism to track the resources created |
| 155 | + # through it. It can also eventually cleanup the pool_ref_store, when it is |
| 156 | + # holding the last reference to it. |
0 commit comments