Skip to content

Commit

Permalink
fix: i/o blocking function of templates
Browse files Browse the repository at this point in the history
* fix: i/o blocking function of templates
  • Loading branch information
xZetsubou authored Nov 22, 2024
1 parent 3c2b926 commit 816bd41
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
14 changes: 10 additions & 4 deletions custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,10 @@ async def async_step_configure_device(self, user_input=None):
)
if self.device_data.pop(EXPORT_CONFIG, False):
dev_config = self.config_entry.data[CONF_DEVICES][dev_id].copy()
templates.export_config(
dev_config, self.device_data[CONF_FRIENDLY_NAME]
await self.hass.async_add_executor_job(
templates.export_config,
dev_config,
self.device_data[CONF_FRIENDLY_NAME],
)
return self.async_create_entry(title="", data={})
# We will restore device details if it's already existed!
Expand Down Expand Up @@ -684,7 +686,9 @@ async def async_step_choose_template(self, user_input=None):
if user_input is not None:
self.use_template = True
filename = user_input.get(TEMPLATES)
_config = templates.import_config(filename)
_config = await self.hass.async_add_executor_job(
templates.import_config, filename
)
dev_conf = self.device_data
dev_conf[CONF_ENTITIES] = _config
dev_conf[CONF_DPS_STRINGS] = self.dps_strings
Expand All @@ -695,7 +699,9 @@ async def async_step_choose_template(self, user_input=None):
self.template_device = self.device_data
self.editing_device = True
return await self.async_step_configure_device()
templates_list = templates.list_templates()
templates_list = await self.hass.async_add_executor_job(
templates.list_templates
)
schema = vol.Schema(
{vol.Required(TEMPLATES): _col_to_select(templates_list, custom_value=True)}
)
Expand Down
31 changes: 15 additions & 16 deletions custom_components/localtuya/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@
# Templates #
###############################
class templates:

def yaml_dump(config, fname: str | None = None) -> JSON_TYPE:
"""Save yaml config."""
try:
with open(fname, "w", encoding="utf-8") as conf_file:
return conf_file.write(dump(config))
except UnicodeDecodeError as exc:
_LOGGER.error("Unable to save file %s: %s", fname, exc)

def list_templates():
"""Return the available templates files."""
dir = os.path.dirname(templates_dir.__file__)
files = {}
for p, d, f in os.walk(dir):
for file in sorted(f):
if fnmatch(file, "*yaml") or fnmatch(file, "*yml"):
# fn = str(file).replace(".yaml", "").replace("_", " ")
files[file] = file
for e in sorted(os.scandir(dir), key=lambda e: e.name):
file: str = e.name.lower()
if e.is_file() and (fnmatch(file, "*yaml") or fnmatch(file, "*yml")):
# fn = str(file).replace(".yaml", "").replace("_", " ")
files[e.name] = e.name
return files

def import_config(filename):
Expand Down Expand Up @@ -75,17 +84,7 @@ def export_config(cls, config: dict, config_name: str):
template_dir = os.path.dirname(templates_dir.__file__)
template_file = os.path.join(template_dir, fname)

asyncio.get_running_loop().run_in_executor(
None, cls.yaml_dump, export_config, template_file
)

def yaml_dump(config, fname: str | None = None) -> JSON_TYPE:
"""Save yaml config."""
try:
with open(fname, "w", encoding="utf-8") as conf_file:
return conf_file.write(dump(config))
except UnicodeDecodeError as exc:
_LOGGER.error("Unable to save file %s: %s", fname, exc)
cls.yaml_dump(export_config, template_file)


################################
Expand Down

0 comments on commit 816bd41

Please sign in to comment.