forked from frequenz-floss/frequenz-client-microgrid-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_component_data.py
More file actions
595 lines (461 loc) · 21.3 KB
/
_component_data.py
File metadata and controls
595 lines (461 loc) · 21.3 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# License: MIT
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
"""Component data types for data coming from a microgrid."""
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Self
from frequenz.api.microgrid import microgrid_pb2
from ._component_error import BatteryError, InverterError
from ._component_states import (
BatteryComponentState,
BatteryRelayState,
EVChargerCableState,
EVChargerComponentState,
InverterComponentState,
)
from .id import ComponentId
@dataclass(frozen=True)
class ComponentData(ABC):
"""A private base class for strongly typed component data classes."""
component_id: ComponentId
"""The ID identifying this component in the microgrid."""
timestamp: datetime
"""The timestamp of when the data was measured."""
# The `raw` attribute is excluded from the constructor as it can only be provided
# when instantiating `ComponentData` using the `from_proto` method, which reads
# data from a protobuf message. The whole protobuf message is stored as the `raw`
# attribute. When `ComponentData` is not instantiated from a protobuf message,
# i.e. using the constructor, `raw` will be set to `None`.
raw: microgrid_pb2.ComponentData | None = field(default=None, init=False)
"""Raw component data as decoded from the wire."""
def _set_raw(self, raw: microgrid_pb2.ComponentData) -> None:
"""Store raw protobuf message.
It is preferred to keep the dataclasses immutable (frozen) and make the `raw`
attribute read-only, which is why the approach of writing to `__dict__`
was used, instead of mutating the `self.raw = raw` attribute directly.
Args:
raw: raw component data as decoded from the wire.
"""
self.__dict__["raw"] = raw
@classmethod
@abstractmethod
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
"""Create ComponentData from a protobuf message.
Args:
raw: raw component data as decoded from the wire.
Returns:
The instance created from the protobuf message.
"""
@dataclass(frozen=True)
class MeterData(ComponentData):
"""A wrapper class for holding meter data."""
active_power: float
"""The total active 3-phase AC power, in Watts (W).
Represented in the passive sign convention.
* Positive means consumption from the grid.
* Negative means supply into the grid.
"""
active_power_per_phase: tuple[float, float, float]
"""The per-phase AC active power for phase 1, 2, and 3 respectively, in Watt (W).
Represented in the passive sign convention.
* Positive means consumption from the grid.
* Negative means supply into the grid.
"""
reactive_power: float
"""The total reactive 3-phase AC power, in Volt-Ampere Reactive (VAr).
* Positive power means capacitive (current leading w.r.t. voltage).
* Negative power means inductive (current lagging w.r.t. voltage).
"""
reactive_power_per_phase: tuple[float, float, float]
"""The per-phase AC reactive power, in Volt-Ampere Reactive (VAr).
The provided values are for phase 1, 2, and 3 respectively.
* Positive power means capacitive (current leading w.r.t. voltage).
* Negative power means inductive (current lagging w.r.t. voltage).
"""
current_per_phase: tuple[float, float, float]
"""AC current in Amperes (A) for phase/line 1,2 and 3 respectively.
Represented in the passive sign convention.
* Positive means consumption from the grid.
* Negative means supply into the grid.
"""
voltage_per_phase: tuple[float, float, float]
"""The ac voltage in volts (v) between the line and the neutral wire for phase/line
1,2 and 3 respectively.
"""
frequency: float
"""The AC power frequency in Hertz (Hz)."""
@classmethod
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
"""Create MeterData from a protobuf message.
Args:
raw: raw component data as decoded from the wire.
Returns:
Instance of MeterData created from the protobuf message.
"""
meter_data = cls(
component_id=ComponentId(raw.id),
timestamp=raw.ts.ToDatetime(tzinfo=timezone.utc),
active_power=raw.meter.data.ac.power_active.value,
active_power_per_phase=(
raw.meter.data.ac.phase_1.power_active.value,
raw.meter.data.ac.phase_2.power_active.value,
raw.meter.data.ac.phase_3.power_active.value,
),
reactive_power=raw.meter.data.ac.power_reactive.value,
reactive_power_per_phase=(
raw.meter.data.ac.phase_1.power_reactive.value,
raw.meter.data.ac.phase_2.power_reactive.value,
raw.meter.data.ac.phase_3.power_reactive.value,
),
current_per_phase=(
raw.meter.data.ac.phase_1.current.value,
raw.meter.data.ac.phase_2.current.value,
raw.meter.data.ac.phase_3.current.value,
),
voltage_per_phase=(
raw.meter.data.ac.phase_1.voltage.value,
raw.meter.data.ac.phase_2.voltage.value,
raw.meter.data.ac.phase_3.voltage.value,
),
frequency=raw.meter.data.ac.frequency.value,
)
meter_data._set_raw(raw=raw)
return meter_data
@dataclass(frozen=True)
class BatteryData(ComponentData): # pylint: disable=too-many-instance-attributes
"""A wrapper class for holding battery data."""
soc: float
"""Battery's overall SoC in percent (%)."""
soc_lower_bound: float
"""The SoC below which discharge commands will be blocked by the system,
in percent (%).
"""
soc_upper_bound: float
"""The SoC above which charge commands will be blocked by the system,
in percent (%).
"""
capacity: float
"""The capacity of the battery in Wh (Watt-hour)."""
power_inclusion_lower_bound: float
"""Lower inclusion bound for battery power in watts.
This is the lower limit of the range within which power requests are allowed for the
battery.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
power_exclusion_lower_bound: float
"""Lower exclusion bound for battery power in watts.
This is the lower limit of the range within which power requests are not allowed for
the battery.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
power_inclusion_upper_bound: float
"""Upper inclusion bound for battery power in watts.
This is the upper limit of the range within which power requests are allowed for the
battery.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
power_exclusion_upper_bound: float
"""Upper exclusion bound for battery power in watts.
This is the upper limit of the range within which power requests are not allowed for
the battery.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
temperature: float
"""The (average) temperature reported by the battery, in Celsius (°C)."""
relay_state: BatteryRelayState
"""State of the battery relay."""
component_state: BatteryComponentState
"""State of the battery."""
errors: list[BatteryError]
"""List of errors in protobuf struct."""
@classmethod
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
"""Create BatteryData from a protobuf message.
Args:
raw: raw component data as decoded from the wire.
Returns:
Instance of BatteryData created from the protobuf message.
"""
raw_power = raw.battery.data.dc.power
battery_data = cls(
component_id=ComponentId(raw.id),
timestamp=raw.ts.ToDatetime(tzinfo=timezone.utc),
soc=raw.battery.data.soc.avg,
soc_lower_bound=raw.battery.data.soc.system_inclusion_bounds.lower,
soc_upper_bound=raw.battery.data.soc.system_inclusion_bounds.upper,
capacity=raw.battery.properties.capacity,
power_inclusion_lower_bound=raw_power.system_inclusion_bounds.lower,
power_exclusion_lower_bound=raw_power.system_exclusion_bounds.lower,
power_inclusion_upper_bound=raw_power.system_inclusion_bounds.upper,
power_exclusion_upper_bound=raw_power.system_exclusion_bounds.upper,
temperature=raw.battery.data.temperature.avg,
relay_state=BatteryRelayState.from_pb(raw.battery.state.relay_state),
component_state=BatteryComponentState.from_pb(
raw.battery.state.component_state
),
errors=[BatteryError.from_pb(e) for e in raw.battery.errors],
)
battery_data._set_raw(raw=raw)
return battery_data
@dataclass(frozen=True)
class InverterData(ComponentData): # pylint: disable=too-many-instance-attributes
"""A wrapper class for holding inverter data."""
active_power: float
"""The total active 3-phase AC power, in Watts (W).
Represented in the passive sign convention.
* Positive means consumption from the grid.
* Negative means supply into the grid.
"""
active_power_per_phase: tuple[float, float, float]
"""The per-phase AC active power for phase 1, 2, and 3 respectively, in Watt (W).
Represented in the passive sign convention.
* Positive means consumption from the grid.
* Negative means supply into the grid.
"""
reactive_power: float
"""The total reactive 3-phase AC power, in Volt-Ampere Reactive (VAr).
* Positive power means capacitive (current leading w.r.t. voltage).
* Negative power means inductive (current lagging w.r.t. voltage).
"""
reactive_power_per_phase: tuple[float, float, float]
"""The per-phase AC reactive power, in Volt-Ampere Reactive (VAr).
The provided values are for phase 1, 2, and 3 respectively.
* Positive power means capacitive (current leading w.r.t. voltage).
* Negative power means inductive (current lagging w.r.t. voltage).
"""
current_per_phase: tuple[float, float, float]
"""AC current in Amperes (A) for phase/line 1, 2 and 3 respectively.
Represented in the passive sign convention.
* Positive means consumption from the grid.
* Negative means supply into the grid.
"""
voltage_per_phase: tuple[float, float, float]
"""The AC voltage in Volts (V) between the line and the neutral wire for
phase/line 1, 2 and 3 respectively.
"""
active_power_inclusion_lower_bound: float
"""Lower inclusion bound for inverter power in watts.
This is the lower limit of the range within which power requests are allowed for the
inverter.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
active_power_exclusion_lower_bound: float
"""Lower exclusion bound for inverter power in watts.
This is the lower limit of the range within which power requests are not allowed for
the inverter.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
active_power_inclusion_upper_bound: float
"""Upper inclusion bound for inverter power in watts.
This is the upper limit of the range within which power requests are allowed for the
inverter.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
active_power_exclusion_upper_bound: float
"""Upper exclusion bound for inverter power in watts.
This is the upper limit of the range within which power requests are not allowed for
the inverter.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
frequency: float
"""AC frequency, in Hertz (Hz)."""
component_state: InverterComponentState
"""State of the inverter."""
errors: list[InverterError]
"""List of errors from the component."""
@classmethod
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
"""Create InverterData from a protobuf message.
Args:
raw: raw component data as decoded from the wire.
Returns:
Instance of InverterData created from the protobuf message.
"""
raw_power = raw.inverter.data.ac.power_active
inverter_data = cls(
component_id=ComponentId(raw.id),
timestamp=raw.ts.ToDatetime(tzinfo=timezone.utc),
active_power=raw.inverter.data.ac.power_active.value,
active_power_per_phase=(
raw.inverter.data.ac.phase_1.power_active.value,
raw.inverter.data.ac.phase_2.power_active.value,
raw.inverter.data.ac.phase_3.power_active.value,
),
reactive_power=raw.inverter.data.ac.power_reactive.value,
reactive_power_per_phase=(
raw.inverter.data.ac.phase_1.power_reactive.value,
raw.inverter.data.ac.phase_2.power_reactive.value,
raw.inverter.data.ac.phase_3.power_reactive.value,
),
current_per_phase=(
raw.inverter.data.ac.phase_1.current.value,
raw.inverter.data.ac.phase_2.current.value,
raw.inverter.data.ac.phase_3.current.value,
),
voltage_per_phase=(
raw.inverter.data.ac.phase_1.voltage.value,
raw.inverter.data.ac.phase_2.voltage.value,
raw.inverter.data.ac.phase_3.voltage.value,
),
active_power_inclusion_lower_bound=raw_power.system_inclusion_bounds.lower,
active_power_exclusion_lower_bound=raw_power.system_exclusion_bounds.lower,
active_power_inclusion_upper_bound=raw_power.system_inclusion_bounds.upper,
active_power_exclusion_upper_bound=raw_power.system_exclusion_bounds.upper,
frequency=raw.inverter.data.ac.frequency.value,
component_state=InverterComponentState.from_pb(
raw.inverter.state.component_state
),
errors=[InverterError.from_pb(e) for e in raw.inverter.errors],
)
inverter_data._set_raw(raw=raw)
return inverter_data
@dataclass(frozen=True)
class EVChargerData(ComponentData): # pylint: disable=too-many-instance-attributes
"""A wrapper class for holding ev_charger data."""
active_power: float
"""The total active 3-phase AC power, in Watts (W).
Represented in the passive sign convention.
* Positive means consumption from the grid.
* Negative means supply into the grid.
"""
active_power_per_phase: tuple[float, float, float]
"""The per-phase AC active power for phase 1, 2, and 3 respectively, in Watt (W).
Represented in the passive sign convention.
* Positive means consumption from the grid.
* Negative means supply into the grid.
"""
current_per_phase: tuple[float, float, float]
"""AC current in Amperes (A) for phase/line 1,2 and 3 respectively.
Represented in the passive sign convention.
* Positive means consumption from the grid.
* Negative means supply into the grid.
"""
reactive_power: float
"""The total reactive 3-phase AC power, in Volt-Ampere Reactive (VAr).
* Positive power means capacitive (current leading w.r.t. voltage).
* Negative power means inductive (current lagging w.r.t. voltage).
"""
reactive_power_per_phase: tuple[float, float, float]
"""The per-phase AC reactive power, in Volt-Ampere Reactive (VAr).
The provided values are for phase 1, 2, and 3 respectively.
* Positive power means capacitive (current leading w.r.t. voltage).
* Negative power means inductive (current lagging w.r.t. voltage).
"""
voltage_per_phase: tuple[float, float, float]
"""The AC voltage in Volts (V) between the line and the neutral
wire for phase/line 1,2 and 3 respectively.
"""
active_power_inclusion_lower_bound: float
"""Lower inclusion bound for EV charger power in watts.
This is the lower limit of the range within which power requests are allowed for the
EV charger.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
active_power_exclusion_lower_bound: float
"""Lower exclusion bound for EV charger power in watts.
This is the lower limit of the range within which power requests are not allowed for
the EV charger.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
active_power_inclusion_upper_bound: float
"""Upper inclusion bound for EV charger power in watts.
This is the upper limit of the range within which power requests are allowed for the
EV charger.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
active_power_exclusion_upper_bound: float
"""Upper exclusion bound for EV charger power in watts.
This is the upper limit of the range within which power requests are not allowed for
the EV charger.
See [`frequenz.api.common.metrics_pb2.Metric.system_inclusion_bounds`][] and
[`frequenz.api.common.metrics_pb2.Metric.system_exclusion_bounds`][] for more
details.
"""
frequency: float
"""AC frequency, in Hertz (Hz)."""
cable_state: EVChargerCableState
"""The state of the ev charger's cable."""
component_state: EVChargerComponentState
"""The state of the ev charger."""
@classmethod
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
"""Create EVChargerData from a protobuf message.
Args:
raw: raw component data as decoded from the wire.
Returns:
Instance of EVChargerData created from the protobuf message.
"""
raw_power = raw.ev_charger.data.ac.power_active
ev_charger_data = cls(
component_id=ComponentId(raw.id),
timestamp=raw.ts.ToDatetime(tzinfo=timezone.utc),
active_power=raw_power.value,
active_power_per_phase=(
raw.ev_charger.data.ac.phase_1.power_active.value,
raw.ev_charger.data.ac.phase_2.power_active.value,
raw.ev_charger.data.ac.phase_3.power_active.value,
),
reactive_power=raw.ev_charger.data.ac.power_reactive.value,
reactive_power_per_phase=(
raw.ev_charger.data.ac.phase_1.power_reactive.value,
raw.ev_charger.data.ac.phase_2.power_reactive.value,
raw.ev_charger.data.ac.phase_3.power_reactive.value,
),
current_per_phase=(
raw.ev_charger.data.ac.phase_1.current.value,
raw.ev_charger.data.ac.phase_2.current.value,
raw.ev_charger.data.ac.phase_3.current.value,
),
voltage_per_phase=(
raw.ev_charger.data.ac.phase_1.voltage.value,
raw.ev_charger.data.ac.phase_2.voltage.value,
raw.ev_charger.data.ac.phase_3.voltage.value,
),
active_power_inclusion_lower_bound=raw_power.system_inclusion_bounds.lower,
active_power_exclusion_lower_bound=raw_power.system_exclusion_bounds.lower,
active_power_inclusion_upper_bound=raw_power.system_inclusion_bounds.upper,
active_power_exclusion_upper_bound=raw_power.system_exclusion_bounds.upper,
cable_state=EVChargerCableState.from_pb(raw.ev_charger.state.cable_state),
component_state=EVChargerComponentState.from_pb(
raw.ev_charger.state.component_state
),
frequency=raw.ev_charger.data.ac.frequency.value,
)
ev_charger_data._set_raw(raw=raw)
return ev_charger_data
def is_ev_connected(self) -> bool:
"""Check whether an EV is connected to the charger.
Returns:
When the charger is not in an error state, whether an EV is connected to
the charger.
"""
return self.component_state not in (
EVChargerComponentState.AUTHORIZATION_REJECTED,
EVChargerComponentState.ERROR,
) and self.cable_state in (
EVChargerCableState.EV_LOCKED,
EVChargerCableState.EV_PLUGGED,
)