-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path_component.py
More file actions
212 lines (157 loc) · 6.45 KB
/
_component.py
File metadata and controls
212 lines (157 loc) · 6.45 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# License: MIT
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
"""Defines the components that can be used in a microgrid."""
from dataclasses import dataclass
from enum import Enum
from frequenz.api.common import components_pb2
from frequenz.api.microgrid import grid_pb2, inverter_pb2
from frequenz.client.common.microgrid.components import ComponentCategory, ComponentId
class ComponentType(Enum):
"""A base class from which individual component types are derived."""
class InverterType(ComponentType):
"""Enum representing inverter types."""
NONE = inverter_pb2.Type.TYPE_UNSPECIFIED
"""Unspecified inverter type."""
BATTERY = inverter_pb2.Type.TYPE_BATTERY
"""Battery inverter."""
SOLAR = inverter_pb2.Type.TYPE_SOLAR
"""Solar inverter."""
HYBRID = inverter_pb2.Type.TYPE_HYBRID
"""Hybrid inverter."""
def component_type_from_protobuf(
component_category: components_pb2.ComponentCategory.ValueType,
component_metadata: inverter_pb2.Metadata,
) -> ComponentType | None:
"""Convert a protobuf InverterType message to Component enum.
For internal-only use by the `microgrid` package.
Args:
component_category: category the type belongs to.
component_metadata: protobuf metadata to fetch type from.
Returns:
Enum value corresponding to the protobuf message.
"""
# ComponentType values in the protobuf definition are not unique across categories
# as of v0.11.0, so we need to check the component category first, before doing any
# component type checks.
if (
component_category
== components_pb2.ComponentCategory.COMPONENT_CATEGORY_INVERTER
):
if not any(int(t.value) == int(component_metadata.type) for t in InverterType):
return None
return InverterType(component_metadata.type)
return None
@dataclass(frozen=True)
class Fuse:
"""Fuse data class."""
max_current: float
"""Rated current of the fuse."""
@dataclass(frozen=True)
class ComponentMetadata:
"""Base class for component metadata classes."""
fuse: Fuse | None = None
"""The fuse at the grid connection point."""
@dataclass(frozen=True)
class GridMetadata(ComponentMetadata):
"""Metadata for a grid connection point."""
def component_metadata_from_protobuf(
component_category: components_pb2.ComponentCategory.ValueType,
component_metadata: grid_pb2.Metadata,
) -> GridMetadata | None:
"""Convert a protobuf GridMetadata message to GridMetadata class.
For internal-only use by the `microgrid` package.
Args:
component_category: category the type belongs to.
component_metadata: protobuf metadata to fetch type from.
Returns:
GridMetadata instance corresponding to the protobuf message.
"""
if component_category == components_pb2.ComponentCategory.COMPONENT_CATEGORY_GRID:
max_current = component_metadata.rated_fuse_current
fuse = Fuse(max_current)
return GridMetadata(fuse)
return None
@dataclass(frozen=True)
class Component:
"""Metadata for a single microgrid component."""
component_id: ComponentId
"""The ID of this component."""
category: ComponentCategory | int
"""The category of this component."""
type: ComponentType | None = None
"""The type of this component."""
metadata: ComponentMetadata | None = None
"""The metadata of this component."""
def is_valid(self) -> bool:
"""Check if this instance contains valid data.
Returns:
`True` if `id > 0` and `type` is a valid `ComponentCategory`, or if `id
== 0` and `type` is `GRID`, `False` otherwise
"""
return (
int(self.component_id) > 0
and any(t == self.category for t in ComponentCategory)
) or (int(self.component_id) == 0 and self.category == ComponentCategory.GRID)
def __hash__(self) -> int:
"""Compute a hash of this instance, obtained by hashing the `component_id` field.
Returns:
Hash of this instance.
"""
return hash(self.component_id)
class ComponentMetricId(Enum):
"""An enum representing the various metrics available in the microgrid."""
ACTIVE_POWER = "active_power"
"""Active power."""
ACTIVE_POWER_PHASE_1 = "active_power_phase_1"
"""Active power in phase 1."""
ACTIVE_POWER_PHASE_2 = "active_power_phase_2"
"""Active power in phase 2."""
ACTIVE_POWER_PHASE_3 = "active_power_phase_3"
"""Active power in phase 3."""
REACTIVE_POWER = "reactive_power"
"""Reactive power."""
REACTIVE_POWER_PHASE_1 = "reactive_power_phase_1"
"""Reactive power in phase 1."""
REACTIVE_POWER_PHASE_2 = "reactive_power_phase_2"
"""Reactive power in phase 2."""
REACTIVE_POWER_PHASE_3 = "reactive_power_phase_3"
"""Reactive power in phase 3."""
CURRENT_PHASE_1 = "current_phase_1"
"""Current in phase 1."""
CURRENT_PHASE_2 = "current_phase_2"
"""Current in phase 2."""
CURRENT_PHASE_3 = "current_phase_3"
"""Current in phase 3."""
VOLTAGE_PHASE_1 = "voltage_phase_1"
"""Voltage in phase 1."""
VOLTAGE_PHASE_2 = "voltage_phase_2"
"""Voltage in phase 2."""
VOLTAGE_PHASE_3 = "voltage_phase_3"
"""Voltage in phase 3."""
FREQUENCY = "frequency"
SOC = "soc"
"""State of charge."""
SOC_LOWER_BOUND = "soc_lower_bound"
"""Lower bound of state of charge."""
SOC_UPPER_BOUND = "soc_upper_bound"
"""Upper bound of state of charge."""
CAPACITY = "capacity"
"""Capacity."""
POWER_INCLUSION_LOWER_BOUND = "power_inclusion_lower_bound"
"""Power inclusion lower bound."""
POWER_EXCLUSION_LOWER_BOUND = "power_exclusion_lower_bound"
"""Power exclusion lower bound."""
POWER_EXCLUSION_UPPER_BOUND = "power_exclusion_upper_bound"
"""Power exclusion upper bound."""
POWER_INCLUSION_UPPER_BOUND = "power_inclusion_upper_bound"
"""Power inclusion upper bound."""
ACTIVE_POWER_INCLUSION_LOWER_BOUND = "active_power_inclusion_lower_bound"
"""Active power inclusion lower bound."""
ACTIVE_POWER_EXCLUSION_LOWER_BOUND = "active_power_exclusion_lower_bound"
"""Active power exclusion lower bound."""
ACTIVE_POWER_EXCLUSION_UPPER_BOUND = "active_power_exclusion_upper_bound"
"""Active power exclusion upper bound."""
ACTIVE_POWER_INCLUSION_UPPER_BOUND = "active_power_inclusion_upper_bound"
"""Active power inclusion upper bound."""
TEMPERATURE = "temperature"
"""Temperature."""