From d7ad72a7e3168ba59cb4661e0a9b2012bf354756 Mon Sep 17 00:00:00 2001 From: mk-maddin Date: Tue, 18 Jun 2024 08:41:25 +0200 Subject: [PATCH] fix file load blocking call --- custom_components/wattpilot/button.py | 5 +++-- custom_components/wattpilot/manifest.json | 4 ++-- custom_components/wattpilot/number.py | 5 +++-- custom_components/wattpilot/select.py | 5 +++-- custom_components/wattpilot/sensor.py | 5 +++-- custom_components/wattpilot/switch.py | 5 +++-- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/custom_components/wattpilot/button.py b/custom_components/wattpilot/button.py index 6813f31..466e40d 100644 --- a/custom_components/wattpilot/button.py +++ b/custom_components/wattpilot/button.py @@ -4,6 +4,7 @@ from typing import Final import logging import asyncio +import aiofiles import yaml import os @@ -42,8 +43,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e entites=[] try: _LOGGER.debug("%s - async_setup_entry %s: Reading static yaml configuration", entry.entry_id, platform) - with open(os.path.dirname(os.path.realpath(__file__))+'/'+platform+'.yaml', 'r') as stream: - yaml_cfg=yaml.safe_load(stream) + async with aiofiles.open(os.path.dirname(os.path.realpath(__file__))+'/'+platform+'.yaml', 'r') as y: + yaml_cfg=yaml.safe_load(await y.read()) except Exception as e: _LOGGER.error("%s - async_setup_entry %s: Reading static yaml configuration failed: %s (%s.%s)", entry.entry_id, platform, str(e), e.__class__.__module__, type(e).__name__) return False diff --git a/custom_components/wattpilot/manifest.json b/custom_components/wattpilot/manifest.json index 1467927..0d9e4df 100644 --- a/custom_components/wattpilot/manifest.json +++ b/custom_components/wattpilot/manifest.json @@ -1,10 +1,10 @@ { "domain": "wattpilot", "name": "Fronius Wattpilot", - "version": "0.2.1", + "version": "0.2.2", "config_flow": true, "documentation": "https://github.com/mk-maddin/wattpilot-HA", - "requirements": ["wattpilot>=0.1","pyyaml>=5.3.0", "importlib_metadata>=4.0.0"], + "requirements": ["wattpilot>=0.1", "pyyaml>=5.3.0", "importlib_metadata>=4.0.0", "aiofiles>=23.2.1"], "dependencies": ["sensor","switch","select","number","button","diagnostics"], "codeowners": ["@mk-maddin"], "iot_class": "local_polling" diff --git a/custom_components/wattpilot/number.py b/custom_components/wattpilot/number.py index 4aa19d6..444e78e 100644 --- a/custom_components/wattpilot/number.py +++ b/custom_components/wattpilot/number.py @@ -4,6 +4,7 @@ from typing import Final import logging import asyncio +import aiofiles import yaml import os @@ -42,8 +43,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e entites=[] try: _LOGGER.debug("%s - async_setup_entry %s: Reading static yaml configuration", entry.entry_id, platform) - with open(os.path.dirname(os.path.realpath(__file__))+'/'+platform+'.yaml', 'r') as stream: - yaml_cfg=yaml.safe_load(stream) + async with aiofiles.open(os.path.dirname(os.path.realpath(__file__))+'/'+platform+'.yaml', 'r') as y: + yaml_cfg=yaml.safe_load(await y.read()) except Exception as e: _LOGGER.error("%s - async_setup_entry %s: Reading static yaml configuration failed: %s (%s.%s)", entry.entry_id, platform, str(e), e.__class__.__module__, type(e).__name__) return False diff --git a/custom_components/wattpilot/select.py b/custom_components/wattpilot/select.py index f00b101..d4ce567 100644 --- a/custom_components/wattpilot/select.py +++ b/custom_components/wattpilot/select.py @@ -4,6 +4,7 @@ from typing import Final import logging import asyncio +import aiofiles import yaml import os @@ -42,8 +43,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e entites=[] try: _LOGGER.debug("%s - async_setup_entry %s: Reading static yaml configuration", entry.entry_id, platform) - with open(os.path.dirname(os.path.realpath(__file__))+'/'+platform+'.yaml', 'r') as stream: - yaml_cfg=yaml.safe_load(stream) + async with aiofiles.open(os.path.dirname(os.path.realpath(__file__))+'/'+platform+'.yaml', 'r') as y: + yaml_cfg=yaml.safe_load(await y.read()) except Exception as e: _LOGGER.error("%s - async_setup_entry %s: Reading static yaml configuration failed: %s (%s.%s)", entry.entry_id, platform, str(e), e.__class__.__module__, type(e).__name__) return False diff --git a/custom_components/wattpilot/sensor.py b/custom_components/wattpilot/sensor.py index 0557fa8..b090cd1 100644 --- a/custom_components/wattpilot/sensor.py +++ b/custom_components/wattpilot/sensor.py @@ -4,6 +4,7 @@ from typing import Final import logging import asyncio +import aiofiles import yaml import os @@ -41,8 +42,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e entites=[] try: _LOGGER.debug("%s - async_setup_entry %s: Reading static yaml configuration", entry.entry_id, platform) - with open(os.path.dirname(os.path.realpath(__file__))+'/'+platform+'.yaml', 'r') as stream: - yaml_cfg=yaml.safe_load(stream) + async with aiofiles.open(os.path.dirname(os.path.realpath(__file__))+'/'+platform+'.yaml', 'r') as y: + yaml_cfg=yaml.safe_load(await y.read()) except Exception as e: _LOGGER.error("%s - async_setup_entry %s: Reading static yaml configuration failed: %s (%s.%s)", entry.entry_id, platform, str(e), e.__class__.__module__, type(e).__name__) return False diff --git a/custom_components/wattpilot/switch.py b/custom_components/wattpilot/switch.py index 5fe2770..ff76de3 100644 --- a/custom_components/wattpilot/switch.py +++ b/custom_components/wattpilot/switch.py @@ -4,6 +4,7 @@ from typing import Final import logging import asyncio +import aiofiles import yaml import os @@ -43,8 +44,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e entites=[] try: _LOGGER.debug("%s - async_setup_entry %s: Reading static yaml configuration", entry.entry_id, platform) - with open(os.path.dirname(os.path.realpath(__file__))+'/'+platform+'.yaml', 'r') as stream: - yaml_cfg=yaml.safe_load(stream) + async with aiofiles.open(os.path.dirname(os.path.realpath(__file__))+'/'+platform+'.yaml', 'r') as y: + yaml_cfg=yaml.safe_load(await y.read()) except Exception as e: _LOGGER.error("%s - async_setup_entry %s: Reading static yaml configuration failed: %s (%s.%s)", entry.entry_id, platform, str(e), e.__class__.__module__, type(e).__name__) return False