Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit conversion methods to Energy type #18

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Commiting the API definitions and preparing the repo to the pass all CI tests.
- Commiting the electricity trading api specs.
- Rename PublicOrders to PublicTrades
- Add a python client with pyton wrappers for methods & classes and support for streaming orders.
- Add unit conversion methods to the Energy type

## Bug Fixes

Expand Down
40 changes: 40 additions & 0 deletions py/frequenz/client/electricity_trading/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,46 @@ def to_pb(self) -> energy_pb2.Energy:
decimal_mwh.value = str(self.mwh)
return energy_pb2.Energy(mwh=decimal_mwh)

def as_watt_hours(self) -> Decimal:
"""Return the energy in watt hours.

Returns:
The energy in watt hours.
"""
return self.mwh * Decimal(1e6)

def as_kilowatt_hours(self) -> Decimal:
"""Return the energy in kilowatt hours.

Returns:
The energy in kilowatt hours.
"""
return self.mwh * Decimal(1e3)

@classmethod
def from_watt_hours(cls, watt_hours: Decimal) -> Self:
"""Initialize a new energy quantity from watt hours.

Args:
watt_hours: The energy in watt hours.

Returns:
A new energy quantity.
"""
return cls(mwh=watt_hours / Decimal(1e6))

@classmethod
def from_kilowatt_hours(cls, kilowatt_hours: Decimal) -> Self:
"""Initialize a new energy quantity from kilowatt hours.

Args:
kilowatt_hours: The energy in kilowatt hours.

Returns:
A new energy quantity.
"""
return cls(mwh=kilowatt_hours / Decimal(1e3))


class EnergyMarketCodeType(enum.Enum):
"""
Expand Down