Skip to content

Commit

Permalink
localapi: check if HTTP quirks are needed from version
Browse files Browse the repository at this point in the history
The API version with the fix isn't known yet, so this commit just adds the
functionality.

Signed-off-by: Álvaro Fernández Rojas <[email protected]>
  • Loading branch information
Noltari committed Sep 10, 2024
1 parent 53daef9 commit 9ba691d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions aioairzone/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from typing import Final

from packaging.version import Version

API_ACS_MAX_TEMP: Final[str] = "acs_maxtemp"
API_ACS_MIN_TEMP: Final[str] = "acs_mintemp"
API_ACS_ON: Final[str] = "acs_power"
Expand Down Expand Up @@ -204,6 +206,7 @@

HTTP_CALL_TIMEOUT: Final[int] = 10
HTTP_MAX_REQUESTS: Final[int] = 1
HTTP_QUIRK_VERSION: Final[Version] = Version("9.99") # Fix version is still unknown

RAW_DEMO: Final[str] = "demo"
RAW_DHW: Final[str] = "dhw"
Expand Down
19 changes: 14 additions & 5 deletions aioairzone/localapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from aiohttp import ClientConnectorError, ClientSession, ClientTimeout
from aiohttp.client_reqrep import ClientResponse
from packaging.version import Version

from .common import OperationMode, get_system_zone_id, json_dumps
from .const import (
Expand Down Expand Up @@ -56,6 +57,7 @@
DEFAULT_SYSTEM_ID,
HTTP_CALL_TIMEOUT,
HTTP_MAX_REQUESTS,
HTTP_QUIRK_VERSION,
RAW_DEMO,
RAW_DHW,
RAW_HVAC,
Expand Down Expand Up @@ -136,6 +138,7 @@ def __init__(
self.api_features_lock = Lock()
self.hotwater: HotWater | None = None
self.http = AirzoneHttp()
self.http_quirks_needed = True
self.options = options
self.systems: dict[int, System] = {}
self.version: str | None = None
Expand Down Expand Up @@ -174,6 +177,10 @@ def handle_errors(errors: list[dict[str, str]]) -> None:
raise ZoneNotProvided(f"{key}: {val}")
raise APIError(f"{key}: {val}")

def http_quirks_enabled(self) -> bool:
"""API expects HTTP headers + body on the same TCP segment."""
return self.options.http_quirks or self.http_quirks_needed

async def aiohttp_request(
self, method: str, path: str, data: Any | None = None
) -> dict[str, Any] | None:
Expand Down Expand Up @@ -251,7 +258,7 @@ async def http_request(
"""Device HTTP request."""
_LOGGER.debug("http_request: /%s (params=%s)", path, data)

if self.options.http_quirks:
if self.http_quirks_enabled():
return await self.http_quirks_request(method, path, data)
return await self.aiohttp_request(method, path, data)

Expand Down Expand Up @@ -322,11 +329,13 @@ async def check_feature_systems(self, update: bool) -> None:
async def check_feature_version(self) -> None:
"""Check Version feature."""
try:
version = await self.get_version()
if version is None:
version_data = await self.get_version()
if version_data is None:
raise APIError("check_feature_version: empty API response")
if API_VERSION in version:
self.version = version[API_VERSION]
version_str = version_data.get(API_VERSION)
if version_str is not None:
self.version = version_str
self.http_quirks_needed = Version(version_str) < HTTP_QUIRK_VERSION
except InvalidMethod:
pass

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
aiohttp>=3.9.0b0;python_version>='3.12'
aiohttp<=3.8.5;python_version<'3.12'
packaging

0 comments on commit 9ba691d

Please sign in to comment.