22import json
33import logging
44import re
5+ import time
56import voluptuous as vol
67from datetime import datetime
78from datetime import timedelta
2728DEFAULT_NAME = 'Pollen HU'
2829DEFAULT_SSL = True
2930
31+ HTTP_TIMEOUT = 10 # secs
32+ MAX_RETRIES = 3
3033SCAN_INTERVAL = timedelta (hours = 1 )
3134
3235PLATFORM_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+
4854async 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
93113class 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 ):
0 commit comments