Skip to content

Commit 0045825

Browse files
committed
refactor(assets): Operational lifetime
missing operational lifetime, considering it always operational Signed-off-by: eduardiazf <[email protected]>
1 parent de523e9 commit 0045825

File tree

3 files changed

+14
-31
lines changed

3 files changed

+14
-31
lines changed

src/frequenz/client/assets/cli/__main__.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,8 @@ async def list_microgrid_electrical_component_connections(
274274
component_connections = (
275275
await client.list_microgrid_electrical_component_connections(
276276
microgrid_id,
277-
source_component_ids=(
278-
list(source_component_ids) if source_component_ids else None
279-
),
280-
destination_component_ids=(
281-
list(destination_component_ids)
282-
if destination_component_ids
283-
else None
284-
),
277+
source_component_ids=source_component_ids,
278+
destination_component_ids=destination_component_ids,
285279
)
286280
)
287281
print_component_connections(component_connections)

src/frequenz/client/assets/electrical_component/_connection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ComponentConnection:
5151
This is the component towards which the current flows.
5252
"""
5353

54-
operational_lifetime: Lifetime = dataclasses.field(default_factory=Lifetime)
54+
operational_lifetime: Lifetime | None = None
5555
"""The operational lifetime of the connection."""
5656

5757
def __post_init__(self) -> None:
@@ -61,6 +61,8 @@ def __post_init__(self) -> None:
6161

6262
def is_operational_at(self, timestamp: datetime) -> bool:
6363
"""Check whether this connection is operational at a specific timestamp."""
64+
if self.operational_lifetime is None:
65+
return True
6466
return self.operational_lifetime.is_operational_at(timestamp)
6567

6668
def is_operational_now(self) -> bool:

src/frequenz/client/assets/electrical_component/_connection_proto.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def component_connection_from_proto(
2525
minor_issues: list[str] = []
2626

2727
connection = component_connection_from_proto_with_issues(
28-
message, major_issues=major_issues, minor_issues=minor_issues
28+
message, major_issues=major_issues
2929
)
3030

3131
if major_issues:
@@ -48,7 +48,6 @@ def component_connection_from_proto_with_issues(
4848
message: electrical_components_pb2.ElectricalComponentConnection,
4949
*,
5050
major_issues: list[str],
51-
minor_issues: list[str],
5251
) -> ComponentConnection | None:
5352
"""Create a `ComponentConnection` from a protobuf message collecting issues.
5453
@@ -58,7 +57,6 @@ def component_connection_from_proto_with_issues(
5857
Args:
5958
message: The protobuf message to parse.
6059
major_issues: A list to collect major issues found during parsing.
61-
minor_issues: A list to collect minor issues found during parsing.
6260
6361
Returns:
6462
A `ComponentConnection` object created from the protobuf message, or
@@ -73,9 +71,11 @@ def component_connection_from_proto_with_issues(
7371
)
7472
return None
7573

76-
lifetime = _get_operational_lifetime_from_proto(
77-
message, major_issues=major_issues, minor_issues=minor_issues
78-
)
74+
try:
75+
lifetime = _get_operational_lifetime_from_proto(message)
76+
except ValueError as exc:
77+
major_issues.append(f"connection ignored: invalid operational lifetime ({exc})")
78+
return None
7979

8080
return ComponentConnection(
8181
source=source_component_id,
@@ -86,21 +86,8 @@ def component_connection_from_proto_with_issues(
8686

8787
def _get_operational_lifetime_from_proto(
8888
message: electrical_components_pb2.ElectricalComponentConnection,
89-
*,
90-
major_issues: list[str],
91-
minor_issues: list[str],
92-
) -> Lifetime:
89+
) -> Lifetime | None:
9390
"""Get the operational lifetime from a protobuf message."""
9491
if message.HasField("operational_lifetime"):
95-
try:
96-
return lifetime_from_proto(message.operational_lifetime)
97-
except ValueError as exc:
98-
major_issues.append(
99-
f"invalid operational lifetime ({exc}), considering it as missing "
100-
"(i.e. always operational)",
101-
)
102-
else:
103-
minor_issues.append(
104-
"missing operational lifetime, considering it always operational",
105-
)
106-
return Lifetime()
92+
return lifetime_from_proto(message.operational_lifetime)
93+
return None

0 commit comments

Comments
 (0)