Skip to content

Commit 646ee22

Browse files
authored
Apply changes from v0.3 to v1alpha8 (#98)
This applies the v0.3 changes done to the frequenz.client.common.* dir to frequenz.client.common.v1alpha8 dir * replacing and deprecating from_proto * using enum.Enum in favor of Enum * using enum.unique * addition of base id types
2 parents 6a66511 + c502499 commit 646ee22

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed

src/frequenz/client/common/v1alpha8/metric/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
"""Module to define the metrics used with the common client."""
55

6-
from enum import Enum
6+
import enum
77
from typing import Self
88

99
from frequenz.api.common.v1alpha8.metrics.metrics_pb2 import Metric as PBMetric
10+
from typing_extensions import deprecated
1011

1112

12-
class Metric(Enum):
13+
@enum.unique
14+
class Metric(enum.Enum):
1315
"""List of supported metrics.
1416
1517
AC energy metrics information:
@@ -137,6 +139,7 @@ class Metric(Enum):
137139
SENSOR_IRRADIANCE = PBMetric.METRIC_SENSOR_IRRADIANCE
138140

139141
@classmethod
142+
@deprecated("Use `frequenz.client.common.enum_proto.enum_from_proto` instead.")
140143
def from_proto(cls, metric: PBMetric.ValueType) -> Self:
141144
"""Convert a protobuf Metric value to Metric enum.
142145

src/frequenz/client/common/v1alpha8/microgrid/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,17 @@
22
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH
33

44
"""Frequenz microgrid definition."""
5+
6+
from typing import final
7+
8+
from frequenz.core.id import BaseId
9+
10+
11+
@final
12+
class EnterpriseId(BaseId, str_prefix="EID"):
13+
"""A unique identifier for an enterprise account."""
14+
15+
16+
@final
17+
class MicrogridId(BaseId, str_prefix="MID"):
18+
"""A unique identifier for a microgrid."""

src/frequenz/client/common/v1alpha8/microgrid/electrical_components/__init__.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"""Defines the electrical components that can be used in a microgrid."""
55
from __future__ import annotations
66

7-
from enum import Enum
7+
import enum
8+
from typing import final
89

910
# pylint: disable=no-name-in-module
1011
from frequenz.api.common.v1alpha8.microgrid.electrical_components.electrical_components_pb2 import (
@@ -16,11 +17,19 @@
1617
from frequenz.api.common.v1alpha8.microgrid.electrical_components.electrical_components_pb2 import (
1718
ElectricalComponentStateCode as PBElectricalComponentStateCode,
1819
)
20+
from frequenz.core.id import BaseId
21+
from typing_extensions import deprecated
1922

2023
# pylint: enable=no-name-in-module
2124

2225

23-
class ElectricalComponentCategory(Enum):
26+
@final
27+
class ElectricalComponentId(BaseId, str_prefix="CID"):
28+
"""A unique identifier for a microgrid electrical component."""
29+
30+
31+
@enum.unique
32+
class ElectricalComponentCategory(enum.Enum):
2433
"""Possible types of microgrid electrical component."""
2534

2635
UNSPECIFIED = (
@@ -80,6 +89,7 @@ class ElectricalComponentCategory(Enum):
8089
"""A heating, ventilation, and air conditioning (HVAC) system."""
8190

8291
@classmethod
92+
@deprecated("Use `frequenz.client.common.enum_proto.enum_from_proto` instead.")
8393
def from_proto(
8494
cls, component_category: PBElectricalComponentCategory.ValueType
8595
) -> ElectricalComponentCategory:
@@ -104,7 +114,8 @@ def to_proto(self) -> PBElectricalComponentCategory.ValueType:
104114
return self.value
105115

106116

107-
class ElectricalComponentStateCode(Enum):
117+
@enum.unique
118+
class ElectricalComponentStateCode(enum.Enum):
108119
"""All possible states of a microgrid electrical component."""
109120

110121
UNSPECIFIED = (
@@ -206,6 +217,7 @@ class ElectricalComponentStateCode(Enum):
206217
"""The precharger circuit is closed, allowing full current to flow to the main circuit."""
207218

208219
@classmethod
220+
@deprecated("Use `frequenz.client.common.enum_proto.enum_from_proto` instead.")
209221
def from_proto(
210222
cls, component_state: PBElectricalComponentStateCode.ValueType
211223
) -> ElectricalComponentStateCode:
@@ -230,7 +242,8 @@ def to_proto(self) -> PBElectricalComponentStateCode.ValueType:
230242
return self.value
231243

232244

233-
class ElectricalComponentDiagnosticCode(Enum):
245+
@enum.unique
246+
class ElectricalComponentDiagnosticCode(enum.Enum):
234247
"""All diagnostics that can occur across electrical component categories."""
235248

236249
UNSPECIFIED = (
@@ -419,6 +432,7 @@ class ElectricalComponentDiagnosticCode(Enum):
419432
times."""
420433

421434
@classmethod
435+
@deprecated("Use `frequenz.client.common.enum_proto.enum_from_proto` instead.")
422436
def from_proto(
423437
cls, component_error_code: PBElectricalComponentDiagnosticCode.ValueType
424438
) -> ElectricalComponentDiagnosticCode:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# License: MIT
2+
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Microgrid sensors."""
5+
6+
from typing import final
7+
8+
from frequenz.core.id import BaseId
9+
10+
11+
@final
12+
class SensorId(BaseId, str_prefix="SID"):
13+
"""A unique identifier for a microgrid sensor."""

tests/microgrid/test_ids_alpha8.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# License: MIT
2+
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Tests for microgrid-related IDs."""
5+
6+
import pytest
7+
from frequenz.core.id import BaseId
8+
9+
from frequenz.client.common.v1alpha8.microgrid import EnterpriseId, MicrogridId
10+
from frequenz.client.common.v1alpha8.microgrid.electrical_components import (
11+
ElectricalComponentId,
12+
)
13+
from frequenz.client.common.v1alpha8.microgrid.sensors import SensorId
14+
15+
16+
@pytest.mark.parametrize(
17+
"id_class, prefix",
18+
[
19+
(EnterpriseId, "EID"),
20+
(MicrogridId, "MID"),
21+
(ElectricalComponentId, "CID"),
22+
(SensorId, "SID"),
23+
],
24+
)
25+
def test_string_representation(id_class: type[BaseId], prefix: str) -> None:
26+
"""Test string representation of IDs."""
27+
_id = id_class(123)
28+
29+
assert str(_id) == f"{prefix}123"
30+
assert repr(_id) == f"{id_class.__name__}(123)"

0 commit comments

Comments
 (0)