Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve performances #51

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/aiocomelit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
SCENARIO,
SLEEP,
STATE_COVER,
STATE_ERROR,
STATE_ON,
VEDO,
WATT,
Expand Down Expand Up @@ -99,12 +98,14 @@ async def _get_page_result(

_LOGGER.debug("GET response %s [%s]", await response.text(), self.host)

if response.status != 200:
raise CannotRetrieveData

if not reply_json:
_LOGGER.debug("GET response is empty [%s]", self.host)
return response.status

data = await response.json() if response.status == 200 else {}
return response.status, data
return response.status, await response.json()

async def _post_page_result(
self, page: str, payload: dict[str, Any]
Expand Down Expand Up @@ -158,6 +159,7 @@ async def _login(self, payload: dict[str, Any], host_type: str) -> bool:
self._session.cookie_jar.update_cookies(cookies)

if await self._check_logged_in(host_type):
await asyncio.sleep(SLEEP)
return True

_LOGGER.warning(
Expand Down Expand Up @@ -210,14 +212,10 @@ async def set_device_status(
return reply_status == 200

async def get_device_status(self, device_type: str, index: int) -> int:
"""Get device status, -1 means API call failed."""
await asyncio.sleep(SLEEP)
"""Get device status."""
reply_status, reply_json = await self._get_page_result(
f"/user/icon_status.json?type={device_type}"
)
if reply_status != 200:
return STATE_ERROR

_LOGGER.debug(
"Device %s[%s] status: %s", device_type, index, reply_json["status"][index]
)
Expand All @@ -233,6 +231,9 @@ async def get_all_devices(self) -> dict[str, dict[int, ComelitSerialBridgeObject

_LOGGER.debug("Getting all devices for host %s", self.host)

ureg = pint.UnitRegistry(cache_folder=":auto:")
ureg.default_format = "~"

for dev_type in (CLIMATE, COVER, LIGHT, IRRIGATION, OTHER, SCENARIO):
reply_status, reply_json = await self._get_page_result(
f"/user/icon_desc.json?type={dev_type}"
Expand All @@ -248,8 +249,6 @@ async def get_all_devices(self) -> dict[str, dict[int, ComelitSerialBridgeObject
"/user/counter.json"
)
devices = {}
ureg = pint.UnitRegistry()
ureg.default_format = "~"
for i in range(reply_json["num"]):
# Guard against "scenario", that has 32 devices even if none is configured
if reply_json["desc"][i] == "":
Expand Down Expand Up @@ -317,7 +316,6 @@ async def set_zone_status(self, index: int, action: str) -> bool:

async def get_zone_status(self, index: int) -> ComelitVedoObject:
"""Get zone status."""
await asyncio.sleep(SLEEP * 2)
reply_status, reply_json = await self._get_page_result("/user/area_stat.json")
if reply_status != 200:
raise CannotRetrieveData
Expand Down
1 change: 0 additions & 1 deletion src/aiocomelit/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

# Statuses
STATE_COVER: list[str] = ["stopped", "opening", "closing"]
STATE_ERROR = -1
STATE_OFF = 0
STATE_ON = 1

Expand Down