|
15 | 15 | from frequenz.api.microgrid import microgrid_pb2 as microgrid_pb |
16 | 16 | from frequenz.api.microgrid.microgrid_pb2_grpc import MicrogridStub |
17 | 17 | from frequenz.channels import Broadcast, Receiver, Sender |
| 18 | +from google.protobuf.empty_pb2 import Empty # pylint: disable=no-name-in-module |
18 | 19 |
|
19 | 20 | from ..._internal._constants import RECEIVER_MAX_SIZE |
20 | 21 | from ..component import ( |
|
30 | 31 | _component_metadata_from_protobuf, |
31 | 32 | _component_type_from_protobuf, |
32 | 33 | ) |
| 34 | +from ..metadata import Location, Metadata |
33 | 35 | from ._connection import Connection |
34 | 36 | from ._retry import LinearBackoff, RetryStrategy |
35 | 37 |
|
@@ -62,6 +64,14 @@ async def components(self) -> Iterable[Component]: |
62 | 64 | Iterator whose elements are all the components in the microgrid. |
63 | 65 | """ |
64 | 66 |
|
| 67 | + @abstractmethod |
| 68 | + async def metadata(self) -> Metadata: |
| 69 | + """Fetch the microgrid metadata. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + the microgrid metadata. |
| 73 | + """ |
| 74 | + |
65 | 75 | @abstractmethod |
66 | 76 | async def connections( |
67 | 77 | self, |
@@ -259,6 +269,36 @@ async def components(self) -> Iterable[Component]: |
259 | 269 |
|
260 | 270 | return result |
261 | 271 |
|
| 272 | + async def metadata(self) -> Metadata: |
| 273 | + """Fetch the microgrid metadata. |
| 274 | +
|
| 275 | + If there is an error fetching the metadata, the microgrid ID and |
| 276 | + location will be set to None. |
| 277 | +
|
| 278 | + Returns: |
| 279 | + the microgrid metadata. |
| 280 | + """ |
| 281 | + microgrid_metadata: microgrid_pb.MicrogridMetadata | None = None |
| 282 | + try: |
| 283 | + microgrid_metadata = await self.api.GetMicrogridMetadata( |
| 284 | + Empty(), |
| 285 | + timeout=int(DEFAULT_GRPC_CALL_TIMEOUT), |
| 286 | + ) # type: ignore[misc] |
| 287 | + except grpc.aio.AioRpcError: |
| 288 | + _logger.exception("The microgrid metadata is not available.") |
| 289 | + |
| 290 | + if not microgrid_metadata: |
| 291 | + return Metadata() |
| 292 | + |
| 293 | + location: Location | None = None |
| 294 | + if microgrid_metadata.location: |
| 295 | + location = Location( |
| 296 | + latitude=microgrid_metadata.location.latitude, |
| 297 | + longitude=microgrid_metadata.location.longitude, |
| 298 | + ) |
| 299 | + |
| 300 | + return Metadata(microgrid_id=microgrid_metadata.microgrid_id, location=location) |
| 301 | + |
262 | 302 | async def connections( |
263 | 303 | self, |
264 | 304 | starts: set[int] | None = None, |
|
0 commit comments