Skip to content

Commit bd02b9d

Browse files
Add function to retrieve the microgrid metadata
So far the microgrid metadata is needed to get the ID and the location of the microgrid. The metadata is retrieved from the microgrid API through the gRPC call GetMicrogridMetadata. Signed-off-by: Daniel Zullo <[email protected]>
1 parent e416543 commit bd02b9d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/frequenz/sdk/microgrid/client/_client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from frequenz.api.microgrid import microgrid_pb2 as microgrid_pb
1616
from frequenz.api.microgrid.microgrid_pb2_grpc import MicrogridStub
1717
from frequenz.channels import Broadcast, Receiver, Sender
18+
from google.protobuf.empty_pb2 import Empty # pylint: disable=no-name-in-module
1819

1920
from ..._internal._constants import RECEIVER_MAX_SIZE
2021
from ..component import (
@@ -30,6 +31,7 @@
3031
_component_metadata_from_protobuf,
3132
_component_type_from_protobuf,
3233
)
34+
from ..metadata import Location, Metadata
3335
from ._connection import Connection
3436
from ._retry import LinearBackoff, RetryStrategy
3537

@@ -62,6 +64,14 @@ async def components(self) -> Iterable[Component]:
6264
Iterator whose elements are all the components in the microgrid.
6365
"""
6466

67+
@abstractmethod
68+
async def metadata(self) -> Metadata:
69+
"""Fetch the microgrid metadata.
70+
71+
Returns:
72+
the microgrid metadata.
73+
"""
74+
6575
@abstractmethod
6676
async def connections(
6777
self,
@@ -259,6 +269,36 @@ async def components(self) -> Iterable[Component]:
259269

260270
return result
261271

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+
262302
async def connections(
263303
self,
264304
starts: set[int] | None = None,

0 commit comments

Comments
 (0)