|
2 | 2 |
|
3 | 3 | import dataclasses |
4 | 4 | from dataclasses import dataclass |
| 5 | +from typing import Any, Self |
5 | 6 |
|
6 | 7 | from frequenz.client.common.microgrid import MicrogridId |
7 | 8 | from frequenz.client.common.microgrid.electrical_components import ElectricalComponentId |
@@ -42,6 +43,22 @@ class ElectricalComponent: |
42 | 43 | operational_lifetime: Lifetime = dataclasses.field(default_factory=Lifetime) |
43 | 44 | """The operational lifetime of the electrical component.""" |
44 | 45 |
|
| 46 | + def __new__(cls, *_: Any, **__: Any) -> Self: |
| 47 | + """Prevent instantiation of this class.""" |
| 48 | + if cls is ElectricalComponent: |
| 49 | + raise TypeError(f"Cannot instantiate {cls.__name__} directly") |
| 50 | + return super().__new__(cls) |
| 51 | + |
| 52 | + @property |
| 53 | + def identity(self) -> tuple[ElectricalComponentId, MicrogridId]: |
| 54 | + """The identity of this component. |
| 55 | +
|
| 56 | + This uses the electrical component ID and microgrid ID to identify a component |
| 57 | + without considering the other attributes, so even if a component state |
| 58 | + changed, the identity remains the same. |
| 59 | + """ |
| 60 | + return (self.id, self.microgrid_id) |
| 61 | + |
45 | 62 | def __str__(self) -> str: |
46 | 63 | """Return the ID of this electrical component as a string.""" |
47 | 64 | name = f":{self.name}" if self.name else "" |
|
0 commit comments