Skip to content

Commit

Permalink
Improve resiliance in case of error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatt007 committed Jan 11, 2023
1 parent 1bbf8b9 commit bf529b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions custom_components/v_zug/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@

import asyncio
import datetime
import logging

import aiohttp

_LOGGER = logging.getLogger(__name__)


class Api:
"""Api."""

def __init__(self, session: aiohttp.ClientSession, ip_address, attempts=2):
def __init__(self, session: aiohttp.ClientSession, ip_address, attempts=5):
"""Init."""
# Session data:
self.session = session
Expand Down Expand Up @@ -53,8 +56,15 @@ async def _get(self, url):
for _ in range(self.attempts):
resp = await self.session.get(url)
content = await resp.json(content_type=None)
if content == {"error": {"code": 503.01}}:
await asyncio.sleep(0.1)

if "error" in content:
_LOGGER.warning(
"error: %s attempts: %s / %s",
content["error"],
_ + 1,
self.attempts,
)
await asyncio.sleep(0.2)
else:
return content

Expand Down
2 changes: 1 addition & 1 deletion custom_components/v_zug/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"zeroconf": [],
"homekit": {},
"iot_class": "local_polling",
"version": "1.1.5"
"version": "1.1.6"
}

0 comments on commit bf529b1

Please sign in to comment.