Skip to content

Commit 823c4a9

Browse files
committed
Add EnterpriseId
This ID will be used for getting the microgrid information. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 7eb6f28 commit 823c4a9

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/frequenz/client/microgrid/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
UnknownError,
3131
UnrecognizedGrpcStatus,
3232
)
33-
from ._id import ComponentId, MicrogridId, SensorId
33+
from ._id import ComponentId, EnterpriseId, MicrogridId, SensorId
3434
from ._lifetime import Lifetime
3535

3636
__all__ = [
3737
"ApiClientError",
3838
"ClientNotConnected",
3939
"ComponentId",
4040
"DataLoss",
41+
"EnterpriseId",
4142
"EntityAlreadyExists",
4243
"EntityNotFound",
4344
"GrpcError",

src/frequenz/client/microgrid/_id.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,62 @@
11
# License: MIT
22
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH
33

4-
"""Strongly typed IDs for microgrids, components and sensors."""
4+
"""Strongly typed IDs for enterprises, microgrids, components and sensors."""
55

66

77
from typing import final
88

99

10+
@final
11+
class EnterpriseId:
12+
"""A unique identifier for an enterprise account."""
13+
14+
def __init__(self, id_: int, /) -> None:
15+
"""Initialize this instance.
16+
17+
Args:
18+
id_: The numeric unique identifier of the enterprise account.
19+
20+
Raises:
21+
ValueError: If the ID is negative.
22+
"""
23+
if id_ < 0:
24+
raise ValueError("Enterprise ID can't be negative.")
25+
self._id = id_
26+
27+
def __int__(self) -> int:
28+
"""Return the numeric ID of this instance."""
29+
return self._id
30+
31+
def __eq__(self, other: object) -> bool:
32+
"""Check if this instance is equal to another object."""
33+
# This is not an unidiomatic typecheck, that's an odd name for the check.
34+
# isinstance() returns True for subclasses, which is not what we want here.
35+
# pylint: disable-next=unidiomatic-typecheck
36+
return type(other) is EnterpriseId and self._id == other._id
37+
38+
def __lt__(self, other: object) -> bool:
39+
"""Check if this instance is less than another object."""
40+
# pylint: disable-next=unidiomatic-typecheck
41+
if type(other) is EnterpriseId:
42+
return self._id < other._id
43+
return NotImplemented
44+
45+
def __hash__(self) -> int:
46+
"""Return the hash of this instance."""
47+
# We include the class because we explicitly want to avoid the same ID to give
48+
# the same hash for different classes of IDs
49+
return hash((EnterpriseId, self._id))
50+
51+
def __repr__(self) -> str:
52+
"""Return the string representation of this instance."""
53+
return f"{type(self).__name__}({self._id!r})"
54+
55+
def __str__(self) -> str:
56+
"""Return the short string representation of this instance."""
57+
return f"EID{self._id}"
58+
59+
1060
@final
1161
class MicrogridId:
1262
"""A unique identifier for a microgrid."""

tests/test_id.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# License: MIT
22
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH
33

4-
"""Tests for the microgrid and component IDs."""
4+
"""Tests for the enterprise, microgrid and component IDs."""
55

66
from dataclasses import dataclass
77

88
import pytest
99

10-
from frequenz.client.microgrid import ComponentId, MicrogridId, SensorId
10+
from frequenz.client.microgrid import ComponentId, EnterpriseId, MicrogridId, SensorId
1111

1212

1313
@dataclass(frozen=True)
@@ -21,6 +21,7 @@ class IdTypeInfo:
2121

2222
# Define all ID types to test here
2323
ID_TYPES: list[IdTypeInfo] = [
24+
IdTypeInfo(EnterpriseId, "EID", "Enterprise"),
2425
IdTypeInfo(MicrogridId, "MID", "Microgrid"),
2526
IdTypeInfo(ComponentId, "CID", "Component"),
2627
IdTypeInfo(SensorId, "SID", "Sensor"),

0 commit comments

Comments
 (0)