Skip to content

trigger_and_get_schedule and get_schedule methods should accept unit #67

@Flix6x

Description

@Flix6x

Let's keep it simple, as we only need support for a few cases:

def convert_units(values: list[int | float], from_unit: str, to_unit: str) -> dict:
    """Convert values between W, kW and MW, as required."
    if from_unit == "MW" and to_unit == "W":
        values = [v * 10**6 for v in values]
    elif (from_unit == "MW" and to_unit == "kW) or (from_unit == "kW" and to_unit == "W"):
        values = [v * 10**3 for v in values]
    elif from_unit == to_unit:
        pass
    elif (from_unit == "W" and to_unit == "kW) or (from_unit == "kW" and to_unit == "MW"):
        values = [v * 10**-3 for v in values]
    elif from_unit == "W" and to_unit == "MW":
        values = [v * 10**-6 for v in values]
    else:
         raise NotImplementedError(f"Power conversion from {from_unit} to {to_unit} is not supported.")
    return values

async def get_schedule(
    ...
    unit: str,  # <----------
) -> dict:
    ...
    check_for_status(status, 200)

    # Convert to the requested unit
    from_unit = schedule["unit"]  # from the schedule
    to_unit = unit  # from the function argument
    schedule["values"] = convert_units(schedule["values"], from_unit, to_unit)

    return schedule

Potential follow-up: open a FlexMeasures issue to handle this server-side. Then, after the feature lands and the host server has been upgraded, it should be possible to simply pass the desired unit in the [GET] schedule request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions