Skip to content

Commit

Permalink
Fix block I/O in the event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbe-B committed Dec 27, 2024
1 parent 70843b2 commit 50ab64f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 4 additions & 3 deletions custom_components/maestro_mcz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

pollling_interval = entry.options.get(CONF_POLLING_INTERVAL, DEFAULT_POLLING_INTERVAL)

mocked_files = has_mocked_files()
mocked_files = await has_mocked_files()

if mocked_files is None:
maestroapi: MaestroControllerInterface = MaestroController(
Expand Down Expand Up @@ -77,10 +77,11 @@ async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> Non
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)

def has_mocked_files() -> list[str] | None:
async def has_mocked_files() -> list[str] | None:
try:
loop = asyncio.get_running_loop()
folder_path = "config/custom_components/maestro_mcz/mocked"
files_in_dir = os.listdir(folder_path)
files_in_dir = await loop.run_in_executor(None, os.listdir, folder_path)
if(files_in_dir is not None):
return [folder_path + "/" + file for file in files_in_dir]
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dataclasses
import json
import asyncio

from ..responses.state import State
from ..responses.status import Status
Expand Down Expand Up @@ -51,7 +52,8 @@ async def StoveInfo(self):
if(self._file_paths is not None):
for _file_path in self._file_paths:
try:
f = open(_file_path)
loop = asyncio.get_running_loop()
f = await loop.run_in_executor(None, open, _file_path)
data = json.load(f)
devices = data["data"]["devices"]
for stove in list(devices.values()):
Expand Down

0 comments on commit 50ab64f

Please sign in to comment.