Skip to content

v1alpha8

frequenz.client.common.microgrid.proto.v1alpha8 ¤

Conversion of microgrid objects from/to protobuf v1alpha8.

Functions:¤

frequenz.client.common.microgrid.proto.v1alpha8.lifetime_from_proto ¤

lifetime_from_proto(
    message: Lifetime,
) -> Lifetime | InvalidLifetime

Create a lifetime from a protobuf message, preserving malformed data.

PARAMETER DESCRIPTION
message

The protobuf message to convert.

TYPE: Lifetime

RETURNS DESCRIPTION
Lifetime | InvalidLifetime

A Lifetime when the timestamps form a valid range, or an InvalidLifetime preserving malformed timestamp ordering. A present but empty protobuf message becomes an unbounded Lifetime().

Source code in src/frequenz/client/common/microgrid/proto/v1alpha8/_lifetime.py
def lifetime_from_proto(message: lifetime_pb2.Lifetime) -> Lifetime | InvalidLifetime:
    """Create a lifetime from a protobuf message, preserving malformed data.

    Args:
        message: The protobuf message to convert.

    Returns:
        A [`Lifetime`][....Lifetime] when the timestamps form a valid range, or
            an [`InvalidLifetime`][....InvalidLifetime] preserving malformed
            timestamp ordering. A present but empty protobuf message becomes an
            unbounded `Lifetime()`.
    """
    start = (
        datetime_from_proto(message.start_timestamp)
        if message.HasField("start_timestamp")
        else None
    )
    end = (
        datetime_from_proto(message.end_timestamp)
        if message.HasField("end_timestamp")
        else None
    )
    try:
        return Lifetime(start_time=start, end_time=end)
    except ValueError:
        pass
    return InvalidLifetime(start_time=start, end_time=end)

frequenz.client.common.microgrid.proto.v1alpha8.microgrid_from_proto ¤

microgrid_from_proto(message: Microgrid) -> Microgrid

Convert a protobuf message to a Microgrid object.

PARAMETER DESCRIPTION
message

The protobuf message to convert.

TYPE: Microgrid

RETURNS DESCRIPTION
Microgrid

The corresponding Microgrid object.

Source code in src/frequenz/client/common/microgrid/proto/v1alpha8/_microgrid.py
def microgrid_from_proto(message: microgrid_pb2.Microgrid) -> Microgrid:
    """Convert a protobuf message to a [`Microgrid`][....Microgrid] object.

    Args:
        message: The protobuf message to convert.

    Returns:
        The corresponding [`Microgrid`][....Microgrid] object.
    """
    delivery_area: DeliveryArea | InvalidDeliveryArea | None = None
    if message.HasField("delivery_area"):
        delivery_area = delivery_area_from_proto2(message.delivery_area)

    location: Location | None = None
    if message.HasField("location"):
        location = location_from_proto(message.location)

    # The wrapper uses create_time, but the protobuf field remains create_timestamp.
    return Microgrid(
        id=MicrogridId(message.id),
        enterprise_id=EnterpriseId(message.enterprise_id),
        name=message.name,
        delivery_area=delivery_area,
        location=location,
        create_time=datetime_from_proto(message.create_timestamp),
        _active=_microgrid_status_to_active(message.status),
        _allow_construction=True,
    )