Skip to content

Commit 6c4621f

Browse files
committed
Add class for representing Energy quantities
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 160780f commit 6c4621f

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,74 @@ def as_kilovolts(self) -> float:
466466
The voltage in kilovolts.
467467
"""
468468
return self._base_value / 1e3
469+
470+
471+
class Energy(
472+
Quantity,
473+
exponent_unit_map={
474+
0: "Wh",
475+
3: "kWh",
476+
6: "MWh",
477+
},
478+
):
479+
"""An energy quantity."""
480+
481+
@classmethod
482+
def from_watt_hours(cls, watt_hours: float) -> Self:
483+
"""Initialize a new energy quantity.
484+
485+
Args:
486+
watt_hours: The energy in watt hours.
487+
488+
Returns:
489+
A new energy quantity.
490+
"""
491+
return cls(value=watt_hours, exponent=0)
492+
493+
@classmethod
494+
def from_kilowatt_hours(cls, kilowatt_hours: float) -> Self:
495+
"""Initialize a new energy quantity.
496+
497+
Args:
498+
kilowatt_hours: The energy in kilowatt hours.
499+
500+
Returns:
501+
A new energy quantity.
502+
"""
503+
return cls(value=kilowatt_hours, exponent=3)
504+
505+
@classmethod
506+
def from_megawatt_hours(cls, megawatt_hours: float) -> Self:
507+
"""Initialize a new energy quantity.
508+
509+
Args:
510+
megawatt_hours: The energy in megawatt hours.
511+
512+
Returns:
513+
A new energy quantity.
514+
"""
515+
return cls(value=megawatt_hours, exponent=6)
516+
517+
def as_watt_hours(self) -> float:
518+
"""Return the energy in watt hours.
519+
520+
Returns:
521+
The energy in watt hours.
522+
"""
523+
return self._base_value
524+
525+
def as_kilowatt_hours(self) -> float:
526+
"""Return the energy in kilowatt hours.
527+
528+
Returns:
529+
The energy in kilowatt hours.
530+
"""
531+
return self._base_value / 1e3
532+
533+
def as_megawatt_hours(self) -> float:
534+
"""Return the energy in megawatt hours.
535+
536+
Returns:
537+
The energy in megawatt hours.
538+
"""
539+
return self._base_value / 1e6

tests/timeseries/test_quantities.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55

66
import pytest
77

8-
from frequenz.sdk.timeseries._quantities import Current, Power, Quantity, Voltage
8+
from frequenz.sdk.timeseries._quantities import (
9+
Current,
10+
Energy,
11+
Power,
12+
Quantity,
13+
Voltage,
14+
)
915

1016

1117
class Fz1(
@@ -193,3 +199,19 @@ def test_voltage() -> None:
193199
assert voltage == Voltage.from_kilovolts(0.006)
194200
assert voltage == Voltage.from_volts(6.0)
195201
assert voltage != Voltage.from_volts(5.0)
202+
203+
204+
def test_energy() -> None:
205+
"""Test the energy class."""
206+
energy = Energy.from_watt_hours(0.0000002)
207+
assert f"{energy:.9}" == "0.0000002 Wh"
208+
energy = Energy.from_megawatt_hours(600000.0)
209+
assert f"{energy}" == "600000 MWh"
210+
211+
energy = Energy.from_kilowatt_hours(6.0)
212+
assert energy.as_watt_hours() == 6000.0
213+
assert energy.as_kilowatt_hours() == 6.0
214+
assert energy.as_megawatt_hours() == 0.006
215+
assert energy == Energy.from_megawatt_hours(0.006)
216+
assert energy == Energy.from_kilowatt_hours(6.0)
217+
assert energy != Energy.from_kilowatt_hours(5.0)

0 commit comments

Comments
 (0)