Skip to content

Commit

Permalink
localapi_device: support PUT requests
Browse files Browse the repository at this point in the history
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
  • Loading branch information
Noltari committed Mar 8, 2022
1 parent d6a1d3b commit f89daef
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
4 changes: 4 additions & 0 deletions aioairzone/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions aioairzone/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
63 changes: 59 additions & 4 deletions aioairzone/localapi_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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__)

Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions examples/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import aiohttp
import json
import time

from aiohttp.client_exceptions import ClientConnectorError

Expand All @@ -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.")

Expand Down

0 comments on commit f89daef

Please sign in to comment.