-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path_lifetime.py
More file actions
52 lines (39 loc) · 1.85 KB
/
_lifetime.py
File metadata and controls
52 lines (39 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# License: MIT
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH
"""Lifetime of a microgrid asset."""
from dataclasses import dataclass
from datetime import datetime, timezone
@dataclass(frozen=True, kw_only=True)
class Lifetime:
"""An active operational period of a microgrid asset.
Warning:
The [`end`][frequenz.client.microgrid.Lifetime.end] timestamp indicates that the
asset has been permanently removed from the system.
"""
start: datetime | None = None
"""The moment when the asset became operationally active.
If `None`, the asset is considered to be active in any past moment previous to the
[`end`][frequenz.client.microgrid.Lifetime.end].
"""
end: datetime | None = None
"""The moment when the asset's operational activity ceased.
If `None`, the asset is considered to be active with no plans to be deactivated.
"""
def __post_init__(self) -> None:
"""Validate this lifetime."""
if self.start is not None and self.end is not None and self.start > self.end:
raise ValueError("Start must be before or equal to end.")
def is_operational_at(self, timestamp: datetime) -> bool:
"""Check whether this lifetime is active at a specific timestamp."""
# Handle start time - it's not active if start is in the future
if self.start is not None and self.start > timestamp:
return False
# Handle end time - active up to and including end time
if self.end is not None:
return self.end >= timestamp
# self.end is None, and either self.start is None or self.start <= timestamp,
# so it is active at this timestamp
return True
def is_operational_now(self) -> bool:
"""Whether this lifetime is currently active."""
return self.is_operational_at(datetime.now(timezone.utc))