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

trigger_and_get_schedule and get_schedule methods should accept unit #67

Open
Flix6x opened this issue Sep 15, 2023 · 0 comments
Open

Comments

@Flix6x
Copy link
Contributor

Flix6x commented Sep 15, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant