Skip to content

Commit

Permalink
cloudapi: add lock for _api_raw_data
Browse files Browse the repository at this point in the history
  • Loading branch information
Noltari authored Jul 23, 2024
1 parent c66bc51 commit 3d75f5c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions aioairzone_cloud/cloudapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def __init__(
RAW_INSTALLATIONS: {},
RAW_WEBSERVERS: {},
}
self._api_raw_data_lock = Lock()
self._api_semaphore: Semaphore = Semaphore(HTTP_MAX_REQUESTS)
self.aidoos: dict[str, Aidoo] = {}
self.callback_function = None
Expand All @@ -119,6 +120,15 @@ def __init__(
self.websockets: dict[str, AirzoneCloudIWS] = {}
self.zones: dict[str, Zone] = {}

async def set_api_raw_data(self, key: str, subkey: str | None, data: dict[str, Any] | None) -> None:
"""Save API raw data if not empty."""
if data is not None:
async with self._api_raw_data_lock:
if subkey is None:
self._api_raw_data[key] = data
else:
self._api_raw_data[key][subkey] = data

async def api_request(
self, method: str, path: str, json: Any | None = None
) -> dict[str, Any]:
Expand Down Expand Up @@ -171,7 +181,7 @@ async def api_get_device_config(self, device: Device) -> dict[str, Any]:
"GET",
f"{API_V1}/{API_DEVICES}/{url_id}/{API_CONFIG}?{dev_params}",
)
self._api_raw_data[RAW_DEVICES_CONFIG][dev_id] = res
await self.set_api_raw_data(RAW_DEVICES_CONFIG, dev_id, res)

return res

Expand All @@ -189,7 +199,7 @@ async def api_get_device_status(self, device: Device) -> dict[str, Any]:
"GET",
f"{API_V1}/{API_DEVICES}/{url_id}/{API_STATUS}?{dev_params}",
)
self._api_raw_data[RAW_DEVICES_STATUS][dev_id] = res
await self.set_api_raw_data(RAW_DEVICES_STATUS, dev_id, res)

return res

Expand All @@ -202,7 +212,7 @@ async def api_get_installation(self, installation: Installation) -> dict[str, An
"GET",
f"{API_V1}/{API_INSTALLATIONS}/{url_id}",
)
self._api_raw_data[RAW_INSTALLATIONS][inst_id] = res
await self.set_api_raw_data(RAW_INSTALLATIONS, dev_id, res)

return res

Expand All @@ -212,7 +222,7 @@ async def api_get_installations(self) -> dict[str, Any]:
"GET",
f"{API_V1}/{API_INSTALLATIONS}",
)
self._api_raw_data[RAW_INSTALLATIONS_LIST] = res
await self.set_api_raw_data(RAW_INSTALLATIONS_LIST, None, res)

return res

Expand All @@ -222,7 +232,7 @@ async def api_get_user(self) -> dict[str, Any]:
"GET",
f"{API_V1}/{API_USER}",
)
self._api_raw_data[RAW_USER] = res
await self.set_api_raw_data(RAW_USER, None, res)

return res

Expand All @@ -244,7 +254,7 @@ async def api_get_webserver(
"GET",
f"{API_V1}/{API_DEVICES}/{API_WS}/{url_id}/{API_STATUS}?{ws_params}",
)
self._api_raw_data[RAW_WEBSERVERS][ws_id] = res
await self.set_api_raw_data(RAW_WEBSERVERS, ws_id, res)

return res

Expand Down

0 comments on commit 3d75f5c

Please sign in to comment.