|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2025 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Definition of a microgrid.""" |
| 5 | + |
| 6 | +import datetime |
| 7 | +import enum |
| 8 | +import logging |
| 9 | +from dataclasses import dataclass |
| 10 | +from functools import cached_property |
| 11 | + |
| 12 | +from frequenz.api.common.v1.microgrid import microgrid_pb2 |
| 13 | +from frequenz.client.common.microgrid import EnterpriseId, MicrogridId |
| 14 | + |
| 15 | +from ._delivery_area import DeliveryArea |
| 16 | +from ._location import Location |
| 17 | + |
| 18 | +_logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +@enum.unique |
| 22 | +class MicrogridStatus(enum.Enum): |
| 23 | + """The possible statuses for a microgrid.""" |
| 24 | + |
| 25 | + UNSPECIFIED = microgrid_pb2.MICROGRID_STATUS_UNSPECIFIED |
| 26 | + """The status is unspecified. This should not be used.""" |
| 27 | + |
| 28 | + ACTIVE = microgrid_pb2.MICROGRID_STATUS_ACTIVE |
| 29 | + """The microgrid is active.""" |
| 30 | + |
| 31 | + INACTIVE = microgrid_pb2.MICROGRID_STATUS_INACTIVE |
| 32 | + """The microgrid is inactive.""" |
| 33 | + |
| 34 | + |
| 35 | +@dataclass(frozen=True, kw_only=True) |
| 36 | +class MicrogridInfo: |
| 37 | + """A localized grouping of electricity generation, energy storage, and loads. |
| 38 | +
|
| 39 | + A microgrid is a localized grouping of electricity generation, energy storage, and |
| 40 | + loads that normally operates connected to a traditional centralized grid. |
| 41 | +
|
| 42 | + Each microgrid has a unique identifier and is associated with an enterprise account. |
| 43 | +
|
| 44 | + A key feature is that it has a physical location and is situated in a delivery area. |
| 45 | +
|
| 46 | + Note: Key Concepts |
| 47 | + - Physical Location: Geographical coordinates specify the exact physical |
| 48 | + location of the microgrid. |
| 49 | + - Delivery Area: Each microgrid is part of a broader delivery area, which is |
| 50 | + crucial for energy trading and compliance. |
| 51 | + """ |
| 52 | + |
| 53 | + id: MicrogridId |
| 54 | + """The unique identifier of the microgrid.""" |
| 55 | + |
| 56 | + enterprise_id: EnterpriseId |
| 57 | + """The unique identifier linking this microgrid to its parent enterprise account.""" |
| 58 | + |
| 59 | + name: str | None |
| 60 | + """Name of the microgrid.""" |
| 61 | + |
| 62 | + delivery_area: DeliveryArea | None |
| 63 | + """The delivery area where the microgrid is located, as identified by a specific code.""" |
| 64 | + |
| 65 | + location: Location | None |
| 66 | + """Physical location of the microgrid, in geographical co-ordinates.""" |
| 67 | + |
| 68 | + status: MicrogridStatus | int |
| 69 | + """The current status of the microgrid.""" |
| 70 | + |
| 71 | + create_timestamp: datetime.datetime |
| 72 | + """The UTC timestamp indicating when the microgrid was initially created.""" |
| 73 | + |
| 74 | + @cached_property |
| 75 | + def is_active(self) -> bool: |
| 76 | + """Whether the microgrid is active.""" |
| 77 | + if self.status is MicrogridStatus.UNSPECIFIED: |
| 78 | + # Because this is a cached property, the warning will only be logged once. |
| 79 | + _logger.warning( |
| 80 | + "Microgrid %s has an unspecified status. Assuming it is active.", self |
| 81 | + ) |
| 82 | + return self.status in (MicrogridStatus.ACTIVE, MicrogridStatus.UNSPECIFIED) |
| 83 | + |
| 84 | + def __str__(self) -> str: |
| 85 | + """Return the ID of this microgrid as a string.""" |
| 86 | + name = f":{self.name}" if self.name else "" |
| 87 | + return f"{self.id}{name}" |
0 commit comments