|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2025 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Client for requests to the PlatformAssets API.""" |
| 5 | + |
| 6 | +from typing import Iterable, Optional |
| 7 | + |
| 8 | +# pylint: disable=no-name-in-module |
| 9 | +from frequenz.api.assets.v1.assets_pb2 import ( |
| 10 | + GetMicrogridRequest as PBGetMicrogridRequest, |
| 11 | +) |
| 12 | +from frequenz.api.assets.v1.assets_pb2 import ( |
| 13 | + GetMicrogridResponse as PBGetMicrogridResponse, |
| 14 | +) |
| 15 | +from frequenz.api.assets.v1.assets_pb2 import ( |
| 16 | + ListMicrogridComponentConnectionsRequest as PBListMicrogridComponentConnectionsRequest, |
| 17 | +) |
| 18 | +from frequenz.api.assets.v1.assets_pb2 import ( |
| 19 | + ListMicrogridComponentConnectionsResponse as PBListMicrogridComponentConnectionsResponse, |
| 20 | +) |
| 21 | +from frequenz.api.assets.v1.assets_pb2 import ( |
| 22 | + ListMicrogridComponentsRequest as PBListMicrogridComponentsRequest, |
| 23 | +) |
| 24 | +from frequenz.api.assets.v1.assets_pb2 import ( |
| 25 | + ListMicrogridComponentsResponse as PBListMicrogridComponentsResponse, |
| 26 | +) |
| 27 | +from frequenz.api.assets.v1.assets_pb2_grpc import PlatformAssetsStub |
| 28 | +from frequenz.api.common.v1.microgrid.components.components_pb2 import ( |
| 29 | + ComponentCategory as PBComponentCategory, |
| 30 | +) |
| 31 | +from frequenz.client.base.client import BaseApiClient, call_stub_method |
| 32 | +from frequenz.client.base.exception import ClientNotConnected |
| 33 | + |
| 34 | +# pylint: enable=no-name-in-module |
| 35 | + |
| 36 | + |
| 37 | +class AssetsApiClient(BaseApiClient[PlatformAssetsStub]): |
| 38 | + """A client for the Frequenz PlatformAssets service.""" |
| 39 | + |
| 40 | + def __init__(self, server_url: str, *, connect: bool = True) -> None: |
| 41 | + """Initialize the client. |
| 42 | +
|
| 43 | + Args: |
| 44 | + server_url: The URL of the PlatformAssets service. |
| 45 | + connect: Whether to establish a connection immediately. |
| 46 | + """ |
| 47 | + super().__init__(server_url, PlatformAssetsStub, connect=connect) |
| 48 | + |
| 49 | + @property |
| 50 | + def stub(self) -> PlatformAssetsStub: |
| 51 | + """Return the gRPC stub for the PlatformAssets service.""" |
| 52 | + if self.channel is None or self._stub is None: |
| 53 | + raise ClientNotConnected(server_url=self.server_url, operation="stub") |
| 54 | + return self._stub |
| 55 | + |
| 56 | + async def get_microgrid(self, microgrid_id: int) -> PBGetMicrogridResponse: |
| 57 | + """Fetch details of a specific microgrid. |
| 58 | +
|
| 59 | + Args: |
| 60 | + microgrid_id: The ID of the microgrid to fetch. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + The response containing the microgrid details. |
| 64 | + """ |
| 65 | + request = PBGetMicrogridRequest(microgrid_id=microgrid_id) |
| 66 | + return await call_stub_method(self, lambda: self.stub.GetMicrogrid(request)) |
| 67 | + |
| 68 | + async def list_microgrid_components( |
| 69 | + self, |
| 70 | + microgrid_id: int, |
| 71 | + component_ids: Optional[list[int]] | None, |
| 72 | + categories: Optional[Iterable[PBComponentCategory]] | None, |
| 73 | + ) -> PBListMicrogridComponentsResponse: |
| 74 | + """List electrical components of a microgrid. |
| 75 | +
|
| 76 | + Args: |
| 77 | + microgrid_id: The ID of the microgrid to fetch components for. |
| 78 | + component_ids: The IDs of the components to fetch. |
| 79 | + categories: The categories of the components to fetch. |
| 80 | +
|
| 81 | + Returns: |
| 82 | + The response containing the microgrid components. |
| 83 | + """ |
| 84 | + request = PBListMicrogridComponentsRequest( |
| 85 | + microgrid_id=microgrid_id, |
| 86 | + component_ids=component_ids, |
| 87 | + categories=categories, |
| 88 | + ) |
| 89 | + return await call_stub_method( |
| 90 | + self, lambda: self.stub.ListMicrogridComponents(request) |
| 91 | + ) |
| 92 | + |
| 93 | + async def list_microgrid_component_connections( |
| 94 | + self, |
| 95 | + microgrid_id: int, |
| 96 | + source_component_ids: Optional[list[int]] | None, |
| 97 | + destination_component_ids: Optional[list[int]] | None, |
| 98 | + ) -> PBListMicrogridComponentConnectionsResponse: |
| 99 | + """List electrical connections between components in a microgrid. |
| 100 | +
|
| 101 | + Args: |
| 102 | + microgrid_id: The ID of the microgrid to fetch component connections for. |
| 103 | + source_component_ids: The IDs of the source components to fetch connections for. |
| 104 | + destination_component_ids: The IDs of the destination components to fetch |
| 105 | + connections for. |
| 106 | +
|
| 107 | + Returns: |
| 108 | + The response containing the component connections. |
| 109 | + """ |
| 110 | + request = PBListMicrogridComponentConnectionsRequest( |
| 111 | + microgrid_id=microgrid_id, |
| 112 | + source_component_ids=source_component_ids, |
| 113 | + destination_component_ids=destination_component_ids, |
| 114 | + ) |
| 115 | + return await call_stub_method( |
| 116 | + self, lambda: self.stub.ListMicrogridComponentConnections(request) |
| 117 | + ) |
0 commit comments