Skip to content

Commit

Permalink
localapi: add handle_empty_response
Browse files Browse the repository at this point in the history
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
  • Loading branch information
Noltari committed Mar 4, 2024
1 parent a68a014 commit 1a7a6fe
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions aioairzone/localapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ def __init__(
self.webserver: WebServer | None = None
self.zones: dict[str, Zone] = {}

def handle_empty_response(self, function: str, request: str) -> None:
"""Handle Airzone API empty response."""
error_str = f"{function}: empty {request} API response"
if self._first_update:
raise APIError(error_str)
_LOGGER.error(error_str)

@staticmethod
def handle_errors(errors: list[dict[str, str]]) -> None:
"""Handle API errors."""
Expand Down Expand Up @@ -196,10 +203,7 @@ def update_dhw(self, data: dict[str, Any]) -> None:
def update_systems(self, data: dict[str, Any] | None) -> None:
"""Gather Systems data."""
if data is None:
if self._first_update:
raise APIError("update_systems: empty Systems API response")
# Mitigate incomplete API responses
_LOGGER.warning("update_systems: empty Systems API response")
self.handle_empty_response("update_systems", "Systems")
return
api_systems = data.get(API_SYSTEMS)
if api_systems is None:
Expand Down Expand Up @@ -280,21 +284,21 @@ async def update_features(self) -> None:
if dhw is not None:
self.update_dhw(dhw)
else:
_LOGGER.error("update_features: empty DHW API response")
self.handle_empty_response("update_features", "DHW")

if self.api_features & ApiFeature.SYSTEMS:
systems = await self.get_hvac_systems()
if systems is not None:
self.update_systems(systems)
else:
_LOGGER.error("update_features: empty Systems API response")
self.handle_empty_response("update_features", "Systems")

if self.api_features & ApiFeature.WEBSERVER:
webserver = await self.get_webserver()
if webserver is not None:
self.update_webserver(webserver)
else:
_LOGGER.error("update_features: empty WebServer API response")
self.handle_empty_response("update_features", "WebServer")

async def validate(self) -> str | None:
"""Validate Airzone API."""
Expand All @@ -303,6 +307,8 @@ async def validate(self) -> str | None:
await self.check_features(False)

response = await self.get_hvac()
if response is None:
raise APIError("validate: empty HVAC API response")
if self.options.system_id == DEFAULT_SYSTEM_ID:
if API_SYSTEMS not in response:
raise InvalidHost(f"validate: {API_SYSTEMS} not in API response")
Expand All @@ -319,10 +325,7 @@ async def update(self) -> None:

hvac = await self.get_hvac()
if hvac is None:
if self._first_update:
raise APIError("update: empty HVAC API response")
# Mitigate incomplete API responses
_LOGGER.warning("update: empty HVAC API response")
self.handle_empty_response("update", "HVAC")
return

for system in self.systems.values():
Expand Down Expand Up @@ -466,7 +469,9 @@ async def get_hvac_systems(
self._api_raw_data[RAW_SYSTEMS] = res
return res

async def get_hvac(self, params: dict[str, Any] | None = None) -> dict[str, Any]:
async def get_hvac(
self, params: dict[str, Any] | None = None
) -> dict[str, Any] | None:
"""Return Airzone HVAC zones."""
if not params:
params = {
Expand Down

0 comments on commit 1a7a6fe

Please sign in to comment.