|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2024 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Battery component.""" |
| 5 | + |
| 6 | +import dataclasses |
| 7 | +import enum |
| 8 | +from typing import Any, Literal, Self, TypeAlias |
| 9 | + |
| 10 | +from frequenz.api.common.v1.microgrid.components import battery_pb2 |
| 11 | + |
| 12 | +from ._category import ComponentCategory |
| 13 | +from ._component import Component |
| 14 | + |
| 15 | + |
| 16 | +@enum.unique |
| 17 | +class BatteryType(enum.Enum): |
| 18 | + """The known types of batteries.""" |
| 19 | + |
| 20 | + UNSPECIFIED = battery_pb2.BATTERY_TYPE_UNSPECIFIED |
| 21 | + """The battery type is unspecified.""" |
| 22 | + |
| 23 | + LI_ION = battery_pb2.BATTERY_TYPE_LI_ION |
| 24 | + """Lithium-ion (Li-ion) battery.""" |
| 25 | + |
| 26 | + NA_ION = battery_pb2.BATTERY_TYPE_NA_ION |
| 27 | + """Sodium-ion (Na-ion) battery.""" |
| 28 | + |
| 29 | + |
| 30 | +@dataclasses.dataclass(frozen=True, kw_only=True) |
| 31 | +class Battery(Component): |
| 32 | + """An abstract battery component.""" |
| 33 | + |
| 34 | + category: Literal[ComponentCategory.BATTERY] = ComponentCategory.BATTERY |
| 35 | + """The category of this component. |
| 36 | +
|
| 37 | + Note: |
| 38 | + This should not be used normally, you should test if a component |
| 39 | + [`isinstance`][] of a concrete component class instead. |
| 40 | +
|
| 41 | + It is only provided for using with a newer version of the API where the client |
| 42 | + doesn't know about a new category yet (i.e. for use with |
| 43 | + [`UnrecognizedComponent`][frequenz.client.microgrid.component.UnrecognizedComponent]) |
| 44 | + and in case some low level code needs to know the category of a component. |
| 45 | + """ |
| 46 | + |
| 47 | + type: BatteryType | int |
| 48 | + """The type of this battery. |
| 49 | +
|
| 50 | + Note: |
| 51 | + This should not be used normally, you should test if a battery |
| 52 | + [`isinstance`][] of a concrete battery class instead. |
| 53 | +
|
| 54 | + It is only provided for using with a newer version of the API where the client |
| 55 | + doesn't know about the new battery type yet (i.e. for use with |
| 56 | + [`UnrecognizedBattery`][frequenz.client.microgrid.component.UnrecognizedBattery]). |
| 57 | + """ |
| 58 | + |
| 59 | + # pylint: disable-next=unused-argument |
| 60 | + def __new__(cls, *args: Any, **kwargs: Any) -> Self: |
| 61 | + """Prevent instantiation of this class.""" |
| 62 | + if cls is Battery: |
| 63 | + raise TypeError(f"Cannot instantiate {cls.__name__} directly") |
| 64 | + return super().__new__(cls) |
| 65 | + |
| 66 | + |
| 67 | +@dataclasses.dataclass(frozen=True, kw_only=True) |
| 68 | +class UnspecifiedBattery(Battery): |
| 69 | + """A battery of a unspecified type.""" |
| 70 | + |
| 71 | + type: Literal[BatteryType.UNSPECIFIED] = BatteryType.UNSPECIFIED |
| 72 | + """The type of this battery. |
| 73 | +
|
| 74 | + Note: |
| 75 | + This should not be used normally, you should test if a battery |
| 76 | + [`isinstance`][] of a concrete battery class instead. |
| 77 | +
|
| 78 | + It is only provided for using with a newer version of the API where the client |
| 79 | + doesn't know about the new battery type yet (i.e. for use with |
| 80 | + [`UnrecognizedBattery`][frequenz.client.microgrid.component.UnrecognizedBattery]). |
| 81 | + """ |
| 82 | + |
| 83 | + |
| 84 | +@dataclasses.dataclass(frozen=True, kw_only=True) |
| 85 | +class LiIonBattery(Battery): |
| 86 | + """A Li-ion battery.""" |
| 87 | + |
| 88 | + type: Literal[BatteryType.LI_ION] = BatteryType.LI_ION |
| 89 | + """The type of this battery. |
| 90 | +
|
| 91 | + Note: |
| 92 | + This should not be used normally, you should test if a battery |
| 93 | + [`isinstance`][] of a concrete battery class instead. |
| 94 | +
|
| 95 | + It is only provided for using with a newer version of the API where the client |
| 96 | + doesn't know about the new battery type yet (i.e. for use with |
| 97 | + [`UnrecognizedBattery`][frequenz.client.microgrid.component.UnrecognizedBattery]). |
| 98 | + """ |
| 99 | + |
| 100 | + |
| 101 | +@dataclasses.dataclass(frozen=True, kw_only=True) |
| 102 | +class NaIonBattery(Battery): |
| 103 | + """A Na-ion battery.""" |
| 104 | + |
| 105 | + type: Literal[BatteryType.NA_ION] = BatteryType.NA_ION |
| 106 | + """The type of this battery. |
| 107 | +
|
| 108 | + Note: |
| 109 | + This should not be used normally, you should test if a battery |
| 110 | + [`isinstance`][] of a concrete battery class instead. |
| 111 | +
|
| 112 | + It is only provided for using with a newer version of the API where the client |
| 113 | + doesn't know about the new battery type yet (i.e. for use with |
| 114 | + [`UnrecognizedBattery`][frequenz.client.microgrid.component.UnrecognizedBattery]). |
| 115 | + """ |
| 116 | + |
| 117 | + |
| 118 | +@dataclasses.dataclass(frozen=True, kw_only=True) |
| 119 | +class UnrecognizedBattery(Battery): |
| 120 | + """A battery of an unrecognized type.""" |
| 121 | + |
| 122 | + type: int |
| 123 | + """The unrecognized type of this battery.""" |
| 124 | + |
| 125 | + |
| 126 | +BatteryTypes: TypeAlias = ( |
| 127 | + LiIonBattery | NaIonBattery | UnrecognizedBattery | UnspecifiedBattery |
| 128 | +) |
| 129 | +"""All possible battery types.""" |
0 commit comments