Skip to content

Commit 9f81de5

Browse files
authored
Fix warning detect block loop in HA
1 parent 53ab81e commit 9f81de5

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

custom_components/hebcal/sensor.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Platform to get Hebcal Times And Hebcalh information for Home Assistant.
33
"""
44
import codecs
5+
import aiofiles
56
import datetime
67
import json
78
import logging
@@ -48,7 +49,7 @@
4849

4950
_LOGGER = logging.getLogger(__name__)
5051

51-
version = "2.0.8"
52+
version = "2.4.0"
5253

5354
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
5455
{
@@ -253,7 +254,7 @@ async def create_db_file(self):
253254
self.temp_data = []
254255
self.file_time_stamp = datetime.date.today()
255256
self.temp_data.append({"update_date": str(self.file_time_stamp)})
256-
if self._jerusalem_candle :
257+
if self._jerusalem_candle:
257258
self.candle = 40
258259
try:
259260
h_url = HEBCAL_DATE_URL.format(str(LANGUAGE_DATA[self._language][-1]), str(self.start), str(self.end),
@@ -262,7 +263,8 @@ async def create_db_file(self):
262263
if not self._tzeit_hakochavim:
263264
h_url = HEBCAL_DATE_URL_HAVDALAH.format(str(LANGUAGE_DATA[self._language][-1]), str(self.start),
264265
str(self.end), str(self._latitude),
265-
str(self._longitude), str(self._timezone), str(self._havdalah), str(self.candle))
266+
str(self._longitude), str(self._timezone), str(self._havdalah),
267+
str(self.candle))
266268
async with aiohttp.ClientSession() as session:
267269
html = await fetch(
268270
session, h_url, )
@@ -273,19 +275,12 @@ async def create_db_file(self):
273275
zmanim_temp = json.loads(html)['times']
274276
zmanim_temp.update({'title': 'day_zmanim'})
275277
temp_db['items'].append(zmanim_temp)
276-
with codecs.open(
278+
async with aiofiles.open(
277279
self.config_path + "hebcal_data_full.json", "w", encoding="utf-8"
278280
) as outfile:
279-
json.dump(
280-
temp_db,
281-
outfile,
282-
skipkeys=False,
283-
ensure_ascii=False,
284-
indent=4,
285-
separators=None,
286-
default=None,
287-
sort_keys=True,
288-
)
281+
temp_data = json.dumps(temp_db, skipkeys=False, ensure_ascii=False, indent=4, separators=None,
282+
default=None, sort_keys=True)
283+
await outfile.write(temp_data)
289284
await self.filter_db(temp_db["items"], "new")
290285
async with aiohttp.ClientSession() as session:
291286
html = await fetch(
@@ -296,29 +291,23 @@ async def create_db_file(self):
296291

297292
except Exception as e:
298293
_LOGGER.error("Error while create DB: %s. Restore From Backup", str(e))
299-
with open(
294+
async with aiofiles.open(
300295
self.config_path + "hebcal_data.json", encoding="utf-8"
301296
) as data_file:
302-
self.hebcal_db = json.loads(data_file.read())
297+
temp_data_db = await data_file.read()
298+
self.hebcal_db = json.loads(temp_data_db)
303299
await self.filter_db(self.hebcal_db, "update")
304300
self.file_time_stamp = datetime.datetime.strptime(
305301
self.hebcal_db[0]["update_date"], "%Y-%m-%d"
306302
).date()
307303
if len(self.temp_data) > 2:
308304
self.hebcal_db = self.temp_data
309-
with codecs.open(
305+
async with aiofiles.open(
310306
self.config_path + "hebcal_data.json", "w", encoding="utf-8"
311307
) as outfile:
312-
json.dump(
313-
self.hebcal_db,
314-
outfile,
315-
skipkeys=False,
316-
ensure_ascii=False,
317-
indent=4,
318-
separators=None,
319-
default=None,
320-
sort_keys=True,
321-
)
308+
temp_data = json.dumps(self.hebcal_db, skipkeys=False, ensure_ascii=False, indent=4, separators=None,
309+
default=None, sort_keys=True)
310+
await outfile.write(temp_data)
322311

323312
async def filter_db(self, temp_db, state):
324313
"""Filters the database"""
@@ -477,7 +466,7 @@ async def add_english_date(self, data):
477466
@callback
478467
def check_candles_time(self, candles):
479468
sunset = datetime.datetime.strptime(self.sunset_time(str(candles)[:10], 0)
480-
, "%Y-%m-%dT%H:%M:%S")
469+
, "%Y-%m-%dT%H:%M:%S")
481470
return sunset > candles
482471

483472
@callback

0 commit comments

Comments
 (0)