diff --git a/aioairzone/const.py b/aioairzone/const.py index 3ac2942..d5d5d75 100644 --- a/aioairzone/const.py +++ b/aioairzone/const.py @@ -26,6 +26,10 @@ API_V1 = "api/v1" API_ZONE_ID = "zoneID" +API_ERROR_SYSTEM_ID_OUT_RANGE = "systemid out of range" +API_ERROR_ZONE_ID_NOT_AVAILABLE = "zoneid not avaiable" +API_ERROR_ZONE_ID_OUT_RANGE = "zoneid out of range" + AZD_AIR_DEMAND = "air_demand" AZD_COLD_STAGE = "cold_stage" AZD_COLD_STAGES = "cold_stages" diff --git a/aioairzone/exceptions.py b/aioairzone/exceptions.py index 075b5ac..08dc5eb 100644 --- a/aioairzone/exceptions.py +++ b/aioairzone/exceptions.py @@ -6,13 +6,25 @@ class AirzoneError(Exception): """Base class for aioairzone errors.""" +class APIError(AirzoneError): + """Exception raised when API fails.""" + + class InvalidHost(AirzoneError): """Exception raised when invalid host is requested.""" +class InvalidParam(AirzoneError): + """Exception raised when invalid param is requested.""" + + class InvalidSystem(AirzoneError): """Exception raised when invalid system is requested.""" class InvalidZone(AirzoneError): """Exception raised when invalid zone is requested.""" + + +class ParamUpdateFailure(AirzoneError): + """Exception raised when parameter isn't updated.""" diff --git a/aioairzone/localapi_device.py b/aioairzone/localapi_device.py index 420dc1f..a5ac10c 100644 --- a/aioairzone/localapi_device.py +++ b/aioairzone/localapi_device.py @@ -23,6 +23,9 @@ API_COLD_STAGES, API_COOL_SET_POINT, API_DATA, + API_ERROR_SYSTEM_ID_OUT_RANGE, + API_ERROR_ZONE_ID_NOT_AVAILABLE, + API_ERROR_ZONE_ID_OUT_RANGE, API_ERRORS, API_FLOOR_DEMAND, API_HEAT_STAGE, @@ -68,7 +71,14 @@ AZD_ZONES_NUM, HTTP_CALL_TIMEOUT, ) -from .exceptions import InvalidHost, InvalidSystem, InvalidZone +from .exceptions import ( + APIError, + InvalidHost, + InvalidParam, + InvalidSystem, + InvalidZone, + ParamUpdateFailure, +) _LOGGER = logging.getLogger(__name__) @@ -138,18 +148,61 @@ async def update_airzone(self) -> bool: else: return False - async def get_hvac(self) -> dict[str, Any]: + async def get_hvac(self, system_id: int = 0, zone_id: int = 0) -> dict[str, Any]: """Return Airzone HVAC.""" res = await self.http_request( "POST", f"{API_V1}/{API_HVAC}", { - API_SYSTEM_ID: 0, - API_ZONE_ID: 0, + API_SYSTEM_ID: system_id, + API_ZONE_ID: zone_id, }, ) return res + async def put_hvac( + self, system_id: int, zone_id: int, key: str, value: Any + ) -> dict[str, Any]: + """Return Airzone HVAC.""" + res = await self.http_request( + "PUT", + f"{API_V1}/{API_HVAC}", + { + API_SYSTEM_ID: system_id, + API_ZONE_ID: zone_id, + key: value, + }, + ) + + if API_DATA not in res: + if API_ERRORS in res: + for error in res[API_ERRORS]: + if error == API_ERROR_SYSTEM_ID_OUT_RANGE: + raise InvalidSystem + elif error == API_ERROR_ZONE_ID_OUT_RANGE: + raise InvalidZone + elif error == API_ERROR_ZONE_ID_NOT_AVAILABLE: + raise InvalidZone + else: + _LOGGER.error('HVAC PUT error: "%s"', error) + raise APIError + else: + data: dict = res[API_DATA][0] + + if system_id != data.get(API_SYSTEM_ID): + raise InvalidSystem + + if zone_id != data.get(API_ZONE_ID): + raise InvalidSystem + + if key not in data: + raise InvalidParam + + if value != data[key]: + raise ParamUpdateFailure + + return res + def data(self) -> dict[str, Any]: """Return Airzone device data.""" data = { @@ -282,6 +335,7 @@ def __init__(self, system: System, zone): self.heat_stage = AirzoneStages(zone[API_HEAT_STAGE]) self.humidity = int(zone[API_HUMIDITY]) self.id = int(zone[API_ZONE_ID]) + self.master = None self.mode = OperationMode(zone[API_MODE]) self.name = str(zone[API_NAME]) self.on = bool(zone[API_ON]) @@ -414,6 +468,7 @@ def get_on(self) -> bool: return self.on def get_system_id(self) -> int: + """Return system ID.""" return self.system.get_id() def get_temp(self) -> float: diff --git a/examples/basic.py b/examples/basic.py index fd1ca31..5355dd6 100644 --- a/examples/basic.py +++ b/examples/basic.py @@ -3,6 +3,7 @@ import aiohttp import json +import time from aiohttp.client_exceptions import ClientConnectorError @@ -21,6 +22,11 @@ async def main(): await client.validate_airzone() await client.update_airzone() print(json.dumps(client.data(), indent=4, sort_keys=True)) + + await client.put_hvac(1, 3, "mode", 1) + time.sleep(3) + await client.update_airzone() + print(json.dumps(client.data(), indent=4, sort_keys=True)) except (ClientConnectorError, InvalidHost): print("Invalid host.")