|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2023 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Fetches the Grid Frequency.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import logging |
| 10 | +from typing import TYPE_CHECKING |
| 11 | + |
| 12 | +from frequenz.channels import Receiver, Sender |
| 13 | + |
| 14 | +from ..actor import ChannelRegistry |
| 15 | +from ..microgrid import connection_manager |
| 16 | +from ..microgrid.component import Component, ComponentCategory, ComponentMetricId |
| 17 | +from ..timeseries._base_types import Sample |
| 18 | +from ..timeseries._quantities import Frequency |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + # Imported here to avoid a circular import. |
| 22 | + from ..actor import ComponentMetricRequest |
| 23 | + |
| 24 | + |
| 25 | +def create_request(component_id: int) -> ComponentMetricRequest: |
| 26 | + """Create a request for grid frequency. |
| 27 | +
|
| 28 | + Args: |
| 29 | + component_id: The component id to use for the request. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + A component metric request for grid frequency. |
| 33 | + """ |
| 34 | + # Imported here to avoid a circular import. |
| 35 | + # pylint: disable=import-outside-toplevel |
| 36 | + from ..actor import ComponentMetricRequest |
| 37 | + |
| 38 | + return ComponentMetricRequest( |
| 39 | + "grid-frequency", component_id, ComponentMetricId.FREQUENCY, None |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +class GridFrequency: |
| 44 | + """Grid Frequency.""" |
| 45 | + |
| 46 | + def __init__( |
| 47 | + self, |
| 48 | + data_sourcing_request_sender: Sender[ComponentMetricRequest], |
| 49 | + channel_registry: ChannelRegistry, |
| 50 | + component: Component, |
| 51 | + ): |
| 52 | + """Initialize the grid frequency formula generator. |
| 53 | +
|
| 54 | + Args: |
| 55 | + data_sourcing_request_sender: The sender to use for requests. |
| 56 | + channel_registry: The channel registry to use for the grid frequency. |
| 57 | + component: The component to use for the grid frequency receiver. If not |
| 58 | + provided, the first component that is either a meter, inverter or EV |
| 59 | + charger will be used. |
| 60 | + """ |
| 61 | + self._request_sender = data_sourcing_request_sender |
| 62 | + self._channel_registry = channel_registry |
| 63 | + self._component = component |
| 64 | + self._component_metric_request = create_request(component.component_id) |
| 65 | + |
| 66 | + self._task: None | asyncio.Task[None] = None |
| 67 | + |
| 68 | + @property |
| 69 | + def component(self) -> Component: |
| 70 | + """The component that is used for grid frequency. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + The component that is used for grid frequency. |
| 74 | + """ |
| 75 | + return self._component |
| 76 | + |
| 77 | + def new_receiver(self) -> Receiver[Sample[Frequency]]: |
| 78 | + """Create a receiver for grid frequency. |
| 79 | +
|
| 80 | + Returns: |
| 81 | + A receiver that will receive grid frequency samples. |
| 82 | + """ |
| 83 | + receiver = self._channel_registry.new_receiver( |
| 84 | + self._component_metric_request.get_channel_name() |
| 85 | + ) |
| 86 | + |
| 87 | + if not self._task: |
| 88 | + self._task = asyncio.create_task(self._send_request()) |
| 89 | + else: |
| 90 | + logging.info("Grid frequency request already sent: %s", self._component) |
| 91 | + |
| 92 | + return receiver |
| 93 | + |
| 94 | + async def _send_request(self) -> None: |
| 95 | + """Send the request for grid frequency.""" |
| 96 | + logging.info("Sending request for grid frequency: %s", self._component) |
| 97 | + await self._request_sender.send(self._component_metric_request) |
| 98 | + logging.info("Sent request for grid frequency: %s", self._component) |
| 99 | + |
| 100 | + @staticmethod |
| 101 | + def find_frequency_component() -> Component: |
| 102 | + """Find the component that will be used for grid frequency. |
| 103 | +
|
| 104 | + Uses the first meter it can find to gather the frequency. If no meter is |
| 105 | + available, it will use the first inverter, then EV charger. |
| 106 | +
|
| 107 | + Returns: |
| 108 | + The component that will be used for grid frequency. |
| 109 | +
|
| 110 | + Raises: |
| 111 | + ValueError: when the component graph doesn't have a `GRID` component. |
| 112 | + """ |
| 113 | + component_graph = connection_manager.get().component_graph |
| 114 | + grid_component = next( |
| 115 | + ( |
| 116 | + comp |
| 117 | + for comp in component_graph.components() |
| 118 | + if comp.category == ComponentCategory.GRID |
| 119 | + ), |
| 120 | + None, |
| 121 | + ) |
| 122 | + |
| 123 | + if grid_component is None: |
| 124 | + raise ValueError( |
| 125 | + "Unable to find a GRID component from the component graph." |
| 126 | + ) |
| 127 | + |
| 128 | + # Sort by component id to ensure consistent results |
| 129 | + grid_successors = sorted( |
| 130 | + component_graph.successors(grid_component.component_id), |
| 131 | + key=lambda comp: comp.component_id, |
| 132 | + ) |
| 133 | + |
| 134 | + def find_component(component_category: ComponentCategory) -> Component | None: |
| 135 | + return next( |
| 136 | + ( |
| 137 | + comp |
| 138 | + for comp in grid_successors |
| 139 | + if comp.category == component_category |
| 140 | + ), |
| 141 | + None, |
| 142 | + ) |
| 143 | + |
| 144 | + # Find the first component that is either a meter, inverter or EV charger |
| 145 | + # with category priority in that order. |
| 146 | + component = next( |
| 147 | + filter( |
| 148 | + None, |
| 149 | + map( |
| 150 | + find_component, |
| 151 | + [ |
| 152 | + ComponentCategory.METER, |
| 153 | + ComponentCategory.INVERTER, |
| 154 | + ComponentCategory.EV_CHARGER, |
| 155 | + ], |
| 156 | + ), |
| 157 | + ), |
| 158 | + None, |
| 159 | + ) |
| 160 | + |
| 161 | + if component is None: |
| 162 | + raise ValueError( |
| 163 | + "Unable to find a METER, INVERTER or EV_CHARGER component from the component graph." |
| 164 | + ) |
| 165 | + |
| 166 | + return component |
0 commit comments