Skip to content

Commit aa44b9e

Browse files
committed
fixup! Implement ListComponents
Add active_at().
1 parent 2f5c185 commit aa44b9e

File tree

1 file changed

+14
-5
lines changed
  • src/frequenz/client/microgrid/component

1 file changed

+14
-5
lines changed

src/frequenz/client/microgrid/component/_base.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import dataclasses
77
import logging
88
from collections.abc import Mapping
9+
from datetime import datetime, timezone
910
from functools import cached_property
1011
from typing import Any, Self
1112

@@ -97,16 +98,24 @@ def __new__(cls, *args: Any, **kwargs: Any) -> Self:
9798
raise TypeError(f"Cannot instantiate {cls.__name__} directly")
9899
return super().__new__(cls)
99100

100-
@cached_property
101-
def active(self) -> bool:
102-
"""Whether this component is currently active."""
101+
def active_at(self, timestamp: datetime) -> bool:
102+
"""Check whether this component is active at a specific timestamp."""
103103
if self.status is ComponentStatus.UNSPECIFIED:
104-
# Because this is a cached property, the warning will only be logged once.
105104
_logger.warning(
106105
"Component %s has an unspecified status. Assuming it is active.",
107106
self,
108107
)
109-
return self.status in (ComponentStatus.ACTIVE, ComponentStatus.UNSPECIFIED)
108+
return self.operational_lifetime.active_at(timestamp)
109+
110+
return (
111+
self.status is ComponentStatus.ACTIVE
112+
and self.operational_lifetime.active_at(timestamp)
113+
)
114+
115+
@cached_property
116+
def active(self) -> bool:
117+
"""Whether this component is currently active."""
118+
return self.active_at(datetime.now(timezone.utc))
110119

111120
def __str__(self) -> str:
112121
"""Return a human-readable string representation of this instance."""

0 commit comments

Comments
 (0)