Skip to content

Commit

Permalink
Add components types from SDK
Browse files Browse the repository at this point in the history
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
  • Loading branch information
Marenz committed Feb 6, 2024
1 parent fdcea3a commit 5115eb5
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ classifiers = [
requires-python = ">= 3.11, < 4"
dependencies = [
"typing-extensions >= 4.5.0, < 5",
"frequenz-api-common >= 0.5.4, < 6",
]
dynamic = ["version"]

Expand Down
4 changes: 4 additions & 0 deletions src/frequenz/client/common/microgrid/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# License: MIT
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH

"""Common code and utilities for Frequenz API clients."""
4 changes: 4 additions & 0 deletions src/frequenz/client/common/microgrid/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# License: MIT
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH

"""Common code and utilities for Frequenz API clients."""
74 changes: 74 additions & 0 deletions src/frequenz/client/common/microgrid/components/components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# License: MIT
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH

"""Defines the components that can be used in a microgrid."""
from __future__ import annotations

from enum import Enum

# pylint: disable=no-name-in-module
from frequenz.api.common.v1.microgrid.components.components_pb2 import (
ComponentCategory as PBComponentCategory,
)

# pylint: enable=no-name-in-module


class ComponentCategory(Enum):
"""Possible types of microgrid component."""

NONE = PBComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
"""Unspecified component category."""

GRID = PBComponentCategory.COMPONENT_CATEGORY_GRID
"""Grid component."""

METER = PBComponentCategory.COMPONENT_CATEGORY_METER
"""Meter component."""

INVERTER = PBComponentCategory.COMPONENT_CATEGORY_INVERTER
"""Inverter component."""

BATTERY = PBComponentCategory.COMPONENT_CATEGORY_BATTERY
"""Battery component."""

EV_CHARGER = PBComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
"""EV charger component."""

CHP = PBComponentCategory.COMPONENT_CATEGORY_CHP
"""CHP component."""


def _component_category_from_protobuf(
component_category: PBComponentCategory.ValueType,
) -> ComponentCategory:
"""Convert a protobuf ComponentCategory message to ComponentCategory enum.
For internal-only use by the `microgrid` package.
Args:
component_category: protobuf enum to convert
Returns:
Enum value corresponding to the protobuf message.
"""
if not any(t.value == component_category for t in ComponentCategory):
return ComponentCategory.NONE

return ComponentCategory(component_category)


def _component_category_to_protobuf(
component_category: ComponentCategory,
) -> PBComponentCategory.ValueType:
"""Convert a ComponentCategory enum to protobuf ComponentCategory message.
For internal-only use by the `microgrid` package.
Args:
component_category: enum to convert
Returns:
Enum value corresponding to the protobuf message.
"""
return component_category.value
18 changes: 18 additions & 0 deletions tests/test_client_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,21 @@
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH

"""Tests for the frequenz.client.common package."""


def test_components() -> None:
"""Test the components."""
# pylint: disable=import-outside-toplevel
from frequenz.client.common.microgrid.components.components import (
ComponentCategory,
_component_category_from_protobuf,
_component_category_to_protobuf,
)

assert ComponentCategory is not None

for category in ComponentCategory:
assert (
_component_category_from_protobuf(_component_category_to_protobuf(category))
== category
)

0 comments on commit 5115eb5

Please sign in to comment.