Skip to content

Commit e416543

Browse files
Add microgrid metadata types
The microgrid metadata is needed to retrieve the ID and location of the microgrid. Signed-off-by: Daniel Zullo <[email protected]>
1 parent 05adc38 commit e416543

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ dependencies = [
3838
"numpy >= 1.24.2, < 2",
3939
"protobuf >= 4.21.6, < 5",
4040
"pydantic >= 2.3, < 3",
41+
"timezonefinder >= 6.2.0, < 7",
4142
"tqdm >= 4.38.0, < 5",
4243
"typing_extensions >= 4.6.1, < 5",
4344
"watchfiles >= 0.15.0",

src/frequenz/sdk/microgrid/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124

125125
from ..actor import ResamplerConfig
126126
from ..timeseries.grid import initialize as initialize_grid
127-
from . import _data_pipeline, client, component, connection_manager, fuse
127+
from . import _data_pipeline, client, component, connection_manager, fuse, metadata
128128
from ._data_pipeline import (
129129
battery_pool,
130130
ev_charger_pool,
@@ -161,4 +161,5 @@ async def initialize(host: str, port: int, resampler_config: ResamplerConfig) ->
161161
"grid",
162162
"frequency",
163163
"logical_meter",
164+
"metadata",
164165
]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# License: MIT
2+
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Metadata that describes a microgrid."""
5+
6+
from dataclasses import dataclass
7+
from zoneinfo import ZoneInfo
8+
9+
from timezonefinder import TimezoneFinder
10+
11+
_timezone_finder = TimezoneFinder()
12+
13+
14+
@dataclass(frozen=True, kw_only=True)
15+
class Location:
16+
"""Metadata for the location of microgrid."""
17+
18+
latitude: float | None = None
19+
"""The latitude of the microgrid in degree."""
20+
21+
longitude: float | None = None
22+
"""The longitude of the microgrid in degree."""
23+
24+
timezone: ZoneInfo | None = None
25+
"""The timezone of the microgrid.
26+
27+
The timezone will be set to None if the latitude or longitude points
28+
are not set or the timezone cannot be found given the location points.
29+
"""
30+
31+
def __post_init__(self) -> None:
32+
"""Initialize the timezone of the microgrid."""
33+
if self.latitude is None or self.longitude is None or self.timezone is not None:
34+
return
35+
36+
timezone = _timezone_finder.timezone_at(lat=self.latitude, lng=self.longitude)
37+
if timezone:
38+
# The dataclass is frozen, so it needs to use __setattr__ to set the timezone.
39+
object.__setattr__(self, "timezone", ZoneInfo(key=timezone))
40+
41+
42+
@dataclass(frozen=True, kw_only=True)
43+
class Metadata:
44+
"""Metadata for the microgrid."""
45+
46+
microgrid_id: int | None = None
47+
"""The ID of the microgrid."""
48+
49+
location: Location | None = None
50+
"""The location of the microgrid."""

0 commit comments

Comments
 (0)