From b2474314e90466ae6cc8fe9986c49f42ca70196a Mon Sep 17 00:00:00 2001 From: JoseRa9 Date: Wed, 3 Jul 2024 00:45:54 +0200 Subject: [PATCH] Add Core response and Core state --- README.md | 28 ++++++++++++++++++++++++++++ pyosmanager/pyosmanager.py | 13 +++++++++++-- pyosmanager/responses.py | 13 +++++++++++++ setup.py | 2 +- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cefd43e..f14c154 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,10 @@ if __name__ == "__main__": True if the server is healthy +- `get_core_state() -> dict` + +Retrieve the core state + - `get_devices() -> list[DeviceResponse]` Retrieve a list of devices @@ -51,3 +55,27 @@ Retrieve the device consumption by name - `get_surplus() -> float:` Retrieve the surplus value + +- `set_surplus_margin(margin: float) -> float:` + +Set the surplus margin + +- `set_grid_margin(margin: float) -> float:` + +Set the grid margin + +- `set_idle_power(idle_power: float) -> float:` + +Set the idle power + +- `set_device_max_consumption(device_name: str, max_consumption: float) -> float:` + +Set the max consumption for a device + +- `set_device_expected_consumption(device_name: str, expected_consumption: float) -> float:` + +Set the expected consumption for a device + +- `set_device_cooldown(device_name: str, cooldown: int) -> int:` + +Set the cooldown for a device diff --git a/pyosmanager/pyosmanager.py b/pyosmanager/pyosmanager.py index d65afe7..ead0b69 100644 --- a/pyosmanager/pyosmanager.py +++ b/pyosmanager/pyosmanager.py @@ -6,7 +6,7 @@ import aiohttp import backoff -from pyosmanager.responses import DeviceResponse +from pyosmanager.responses import CoreResponse, DeviceResponse logger = logging.getLogger(__name__) @@ -72,7 +72,7 @@ async def __post_data(self, endpoint, data): """ try: async with self.session.post( - f"{self.base_url}/{endpoint}", json=data + f"{self.base_url}/api/{endpoint}", json=data ) as response: response.raise_for_status() data = await response.json() @@ -94,6 +94,15 @@ async def is_healthy(self) -> bool: except APIError: return False + async def get_core_state(self) -> dict: + """ + Get the core state of the system. + + :return: Core state of the system + """ + core = await self.__get_data("core") + return CoreResponse(**core) + async def get_devices(self) -> list[DeviceResponse]: """ Get a list of all devices. diff --git a/pyosmanager/responses.py b/pyosmanager/responses.py index 802756b..adf96a4 100644 --- a/pyosmanager/responses.py +++ b/pyosmanager/responses.py @@ -19,3 +19,16 @@ class DeviceResponse: powered: bool cooldown: int | None enabled: bool + + +@dataclass +class CoreResponse: + """ + This class is used to represent the response of the API + when querying for the core. + """ + + surplus: float + surplus_margin: float + grid_margin: float + idle_power: float diff --git a/setup.py b/setup.py index 34261ad..ab10393 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name="pyosmanager", packages=["pyosmanager"], - version="0.2.1", + version="0.2.2", license="MIT", description="Python client for Open Surplus Manager", long_description=long_description,