Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
feat: повторный опрос статуса устройств, если произошел сбой при обра…
Browse files Browse the repository at this point in the history
…щении к серверу
  • Loading branch information
Александр Тумайкин authored and Александр Тумайкин committed Jun 2, 2021
1 parent d1b7c76 commit 9b1ec1e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 34 deletions.
80 changes: 48 additions & 32 deletions custom_components/electrolux_remote/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import abc
import async_timeout
import time

from aiohttp import ClientSession, ClientError
from asyncio import TimeoutError, get_event_loop
Expand Down Expand Up @@ -31,6 +32,7 @@
ERROR_INCORRECT_LOGIN_OR_PASSWORD = "106"
ERROR_INCORRECT_PHONE = "112" # Слишком короткий номер телефона
ERROR_TOKEN_NOT_FOUND = "121" # Токен не найден
ERROR_WRITE_PARAM_UNAVAILABLE = "130" # Параметр не доступен для записи: <param>
ERROR_USER_NOT_FOUND = "136" # Пользователь не найден
ERROR_DEVICE_UNAVAILABLE = "153" # Ошибка - устройство не в сети или неизвестный тип

Expand All @@ -43,6 +45,7 @@
}

TIMEOUT = 10
SLEEP = 3


class ApiInterface(metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -146,38 +149,51 @@ async def _request(self, url: str, payload: dict) -> dict:
_LOGGER.debug(f"request: {url}")
_LOGGER.debug(f"payload: {payload}")

try:
async with async_timeout.timeout(TIMEOUT, loop=get_event_loop()):
response = await self._session.post(f"{self._host}/{url}", json=payload, headers=HEADERS)
json = await response.json()

_LOGGER.debug(f"response: {json}")

if json is None:
raise InvalidResponse(f"Response error: json is None")

return json
except TimeoutError as exception:
_LOGGER.error(
"Timeout error fetching information from %s - %s",
url,
exception,
)

except (KeyError, TypeError) as exception:
_LOGGER.error(
"Error parsing information from %s - %s",
url,
exception,
)
except (ClientError, gaierror) as exception:
_LOGGER.error(
"Error fetching information from %s - %s",
url,
exception,
)
except Exception as exception: # pylint: disable=broad-except
_LOGGER.error("Something really wrong happened! - %s", exception)
for i in range(0, 3):
while True:
try:
async with async_timeout.timeout(TIMEOUT, loop=get_event_loop()):
response = await self._session.post(f"{self._host}/{url}", json=payload, headers=HEADERS)
json = await response.json()

_LOGGER.debug(f"response: {json}")

if json is None:
raise InvalidResponse(f"Response error: json is None")

return json
except TimeoutError as exception:
_LOGGER.error(
"Timeout error fetching information from %s - %s",
url,
exception,
)

time.sleep(SLEEP)
continue
except (KeyError, TypeError) as exception:
_LOGGER.error(
"Error parsing information from %s - %s",
url,
exception,
)

time.sleep(SLEEP)
continue
except (ClientError, gaierror) as exception:
_LOGGER.error(
"Error fetching information from %s - %s",
url,
exception,
)

time.sleep(SLEEP)
continue
except Exception as exception: # pylint: disable=broad-except
_LOGGER.error("Something really wrong happened! - %s", exception)

time.sleep(SLEEP)
continue

async def _update_device_params(self, params: dict) -> dict:
if self._token is None:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/electrolux_remote/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
NAME = "{{ Electrolux remote }}"
DOMAIN = "electrolux_remote"
DOMAIN_DATA = f"{DOMAIN}_data"
VERSION = "1.0.1"
VERSION = "1.1.0"
ISSUE_URL = "https://github.com/Ailme/home_assistant_electrolux_remote/issues"

CONF_APPCODE = "appcode"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/electrolux_remote/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"@Ailme"
],
"iot_class": "cloud_polling",
"version": "1.0.1"
"version": "1.1.0"
}
5 changes: 5 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
This is **only** intended for development!

{%- elif (version_installed.replace("v", "").split(".")[0] | int) < 1 %}
## Version 1.1.0

### Features
- повторный опрос статуса устройств, если произошел сбой при обращении к серверу

## Version 1.0.1

### Fix
Expand Down

0 comments on commit 9b1ec1e

Please sign in to comment.