Skip to content

Commit 13650f1

Browse files
Merge pull request #29 from naeramarth7/fix/async_io
fix: make I/O calls async
2 parents e134478 + 162e4fa commit 13650f1

1 file changed

Lines changed: 4 additions & 10 deletions

File tree

custom_components/isc/const.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ def ensure_config(user_input, hass):
153153

154154

155155
# helper to validate user input
156-
def check_data(user_input, hass, own_id=None):
156+
async def check_data(user_input, hass, own_id=None):
157157
"""Check validity of the provided date."""
158158
ret = {}
159159
if(CONF_ICS_URL in user_input):
160160
try:
161-
cal_string = load_data(user_input[CONF_ICS_URL])
161+
cal_string = await async_load_data(hass, user_input[CONF_ICS_URL])
162162
try:
163163
Calendar.from_ical(cal_string)
164164
except Exception:
@@ -231,7 +231,7 @@ def create_form(page, user_input, hass):
231231
return data_schema
232232

233233

234-
def load_data(url):
234+
def _load_data(url):
235235
"""Load data from URL, exported to const to call it from sensor and from config_flow."""
236236
if(url.lower().startswith("file://")):
237237
req = Request(url=url, data=None, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})
@@ -240,10 +240,4 @@ def load_data(url):
240240

241241
async def async_load_data(hass, url):
242242
"""Load data from URL, exported to const to call it from sensor and from config_flow."""
243-
if(url.lower().startswith("file://")):
244-
req = Request(url=url, data=None, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})
245-
ret = urlopen(req)
246-
ret = await hass.async_add_executor_job(ret.read)
247-
return ret.decode('ISO-8859-1')
248-
ret = await hass.async_add_executor_job(partial(requests.get, url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}, allow_redirects=True))
249-
return ret.content
243+
return await hass.async_add_executor_job(_load_data, url)

0 commit comments

Comments
 (0)