Skip to content

Commit cc5e5ba

Browse files
authored
Add microgrid-related ID types (#77)
2 parents 7802605 + 514aa3f commit cc5e5ba

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

RELEASE_NOTES.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010

1111
## New Features
1212

13-
<!-- Here goes the main new features and examples or instructions on how to use them -->
13+
- New `BaseId` class to create unique IDs for entities in the system.
14+
- New ID classes for microgrid-related entities:
15+
16+
* `EnterpriseId`
17+
* `MicrogridId`
18+
* `ComponentId`
19+
* `SensorId`
1420

1521
## Bug Fixes
1622

pyproject.toml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ requires-python = ">= 3.11, < 4"
2828
dependencies = [
2929
"typing-extensions >= 4.6.0, < 5",
3030
"frequenz-api-common >= 0.6.0, < 7",
31+
"frequenz-core >= 1.0.0, < 2",
3132
]
3233
dynamic = ["version"]
3334

@@ -144,7 +145,18 @@ disable = [
144145
]
145146

146147
[tool.pytest.ini_options]
147-
addopts = "-W=all -Werror -Wdefault::DeprecationWarning -Wdefault::PendingDeprecationWarning '-Wdefault:Protobuf gencode version 5.27.2 is exactly one major version older than the runtime version:UserWarning' -vv"
148+
addopts = "-vv"
149+
filterwarnings = [
150+
"error",
151+
"once::DeprecationWarning",
152+
"once::PendingDeprecationWarning",
153+
# We ignore warnings about protobuf gencode version being one version older
154+
# than the current version, as this is supported by protobuf, and we expect to
155+
# have such cases. If we go too far, we will get a proper error anyways.
156+
# We use a raw string (single quotes) to avoid the need to escape special
157+
# characters as this is a regex.
158+
'ignore:Protobuf gencode version .*exactly one major version older.*:UserWarning',
159+
]
148160
testpaths = ["tests", "src"]
149161
asyncio_mode = "auto"
150162
asyncio_default_fixture_loop_scope = "function"

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,17 @@
22
# Copyright © 2023 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/microgrid/components/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
33

44
"""Defines the components that can be used in a microgrid."""
5+
56
from __future__ import annotations
67

78
from enum import Enum
9+
from typing import final
810

911
# pylint: disable=no-name-in-module
1012
from frequenz.api.common.v1.microgrid.components.components_pb2 import (
@@ -16,10 +18,16 @@
1618
from frequenz.api.common.v1.microgrid.components.components_pb2 import (
1719
ComponentStateCode as PBComponentStateCode,
1820
)
21+
from frequenz.core.id import BaseId
1922

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

2225

26+
@final
27+
class ComponentId(BaseId, str_prefix="CID"):
28+
"""A unique identifier for a microgrid component."""
29+
30+
2331
class ComponentCategory(Enum):
2432
"""Possible types of microgrid component."""
2533

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.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.microgrid import EnterpriseId, MicrogridId
10+
from frequenz.client.common.microgrid.components import ComponentId
11+
from frequenz.client.common.microgrid.sensors import SensorId
12+
13+
14+
@pytest.mark.parametrize(
15+
"id_class, prefix",
16+
[
17+
(EnterpriseId, "EID"),
18+
(MicrogridId, "MID"),
19+
(ComponentId, "CID"),
20+
(SensorId, "SID"),
21+
],
22+
)
23+
def test_string_representation(id_class: type[BaseId], prefix: str) -> None:
24+
"""Test string representation of IDs."""
25+
_id = id_class(123)
26+
27+
assert str(_id) == f"{prefix}123"
28+
assert repr(_id) == f"{id_class.__name__}(123)"

0 commit comments

Comments
 (0)