From f2a41060acc46b52b440b27c61efa3a4e8fc4e49 Mon Sep 17 00:00:00 2001 From: Yuval Mejahez <38878996+rt400@users.noreply.github.com> Date: Fri, 5 Jul 2024 07:59:48 +0300 Subject: [PATCH] Update sensor.py Fix Block Loop Detected warning in HA log --- custom_components/school_holidays/sensor.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/custom_components/school_holidays/sensor.py b/custom_components/school_holidays/sensor.py index 3869834..b7c6f03 100644 --- a/custom_components/school_holidays/sensor.py +++ b/custom_components/school_holidays/sensor.py @@ -4,7 +4,7 @@ Document will come soon... """ import logging -import codecs +import aiofiles import datetime import json import pathlib @@ -17,7 +17,7 @@ from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.components.sensor import ENTITY_ID_FORMAT -__version__ = '2.0.3' +__version__ = '2.1.0' _LOGGER = logging.getLogger(__name__) @@ -120,9 +120,10 @@ async def create_db_file(self): html = await fetch(session, 'https://raw.githubusercontent.com/rt400/School-Vacation/master/data.json') data = json.loads(html) - with codecs.open(self.config_path + 'school_data.json', 'w', encoding='utf-8') as outfile: - json.dump(data, outfile, skipkeys=False, ensure_ascii=False, - indent=4, separators=None, default=None, sort_keys=True) + async with aiofiles.open(self.config_path + 'school_data.json', 'w', encoding='utf-8') as outfile: + temp_data = json.dumps(data, skipkeys=False, ensure_ascii=False, indent=4, separators=None, + default=None, sort_keys=True) + await outfile.write(temp_data) self.school_db = data except Exception as e: _LOGGER.error(e)