Skip to content

Commit 7eb6487

Browse files
committed
Enhance connection error handling
1 parent af5a37f commit 7eb6487

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

custom_components/pollen_hu/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "Pollen Information Hungary",
44
"documentation": "https://github.com/amaximus/pollen_hu",
55
"issue_tracker": "https://github.com/amaximus/pollen_hu/issues",
6-
"version": "0.6.0",
6+
"version": "0.7.0",
77
"dependencies": [],
88
"codeowners": ["@amaximus"],
99
"iot_class": "cloud_polling",

custom_components/pollen_hu/sensor.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import logging
44
import re
5+
import time
56
import voluptuous as vol
67
from datetime import datetime
78
from datetime import timedelta
@@ -27,6 +28,8 @@
2728
DEFAULT_NAME = 'Pollen HU'
2829
DEFAULT_SSL = True
2930

31+
HTTP_TIMEOUT = 10 # secs
32+
MAX_RETRIES = 3
3033
SCAN_INTERVAL = timedelta(hours=1)
3134

3235
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
@@ -45,21 +48,37 @@ async def async_setup_platform(hass, config, async_add_devices, discovery_info=N
4548
async_add_devices(
4649
[PollenHUSensor(hass, name, alldominant, pollens, ssl)],update_before_add=True)
4750

51+
def _sleep(secs):
52+
time.sleep(secs)
53+
4854
async def async_get_pdata(self):
4955
pjson = {"pollens": []}
5056
pjson1 = {}
5157
successful_poll = "true"
5258

5359
url = 'https://efop180.antsz.hu/polleninformaciok/'
54-
try:
55-
async with self._session.get(url) as response:
56-
rsp1 = await response.text()
57-
if response.status != 200:
58-
rsp1 = ""
59-
successful_poll = "false"
60-
except(aiohttp.client_exceptions.ClientConnectorError):
60+
_session = async_get_clientsession(self._hass, self._ssl)
61+
for i in range(MAX_RETRIES):
62+
try:
63+
async with _session.get(url, timeout=HTTP_TIMEOUT) as response:
64+
rsp1 = await response.text()
65+
_LOGGER.debug(url + " " + str(response.status))
66+
67+
if not response.status // 100 == 2:
68+
rsp1 = ""
69+
successful_poll = "false"
70+
_LOGGER.debug("donwload not successful")
71+
await self._hass.async_add_executor_job(_sleep, 10)
72+
else:
73+
_LOGGER.debug("rsp1: " + rsp1)
74+
break
75+
_LOGGER.debug(url + " " + str(response.text()))
76+
except Exception as err:
6177
rsp1 = ""
6278
successful_poll = "false"
79+
_LOGGER.debug("Fetch attempt " + str(i+1) + " failed for " + url)
80+
_LOGGER.debug(f'error: {err} of type: {type(err)}')
81+
await self._hass.async_add_executor_job(_sleep, 10)
6382

6483
rsp = rsp1.replace("\n","").replace("\r","")
6584

@@ -88,6 +107,7 @@ async def async_get_pdata(self):
88107
else:
89108
pjson = pjson1
90109
pjson['successful_poll'] = successful_poll
110+
_LOGGER.debug(str(pjson))
91111
return pjson
92112

93113
class PollenHUSensor(Entity):
@@ -104,7 +124,6 @@ def __init__(self, hass, name, alldominant, pollens, ssl):
104124
self._ssl = ssl
105125
self._successful_poll = "true"
106126
self._icon = DEFAULT_ICON
107-
self._session = async_get_clientsession(hass, ssl)
108127

109128
@property
110129
def extra_state_attributes(self):

tracker.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"pollen_hu": {
3-
"version": "0.6.0",
4-
"updated_at": "2023-09-05",
3+
"version": "0.7.0",
4+
"updated_at": "2024-07-17",
55
"visit_repo": "https://github.com/amaximus/pollen_hu",
6-
"changelog": "https://github.com/amaximus/pollen_hu/releases/0.6.0"
6+
"changelog": "https://github.com/amaximus/pollen_hu/releases/0.7.0"
77
}
88
}

0 commit comments

Comments
 (0)