Skip to content

Commit

Permalink
init: load config in executor job
Browse files Browse the repository at this point in the history
HA 2024.6 is now warning about local file loading as being blocking, so
load in a separate executor job to avoid blocking the main loop.
  • Loading branch information
make-all committed Jun 7, 2024
1 parent b5e1e38 commit ae24344
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
41 changes: 33 additions & 8 deletions custom_components/tuya_local/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
# Migrate to filename based config_type, to avoid needing to
# parse config files to find the right one.
config = {**entry.data, **entry.options, "name": entry.title}
config_type = get_config(config[CONF_TYPE]).config_type
config_yaml = await hass.async_add_executor_job(
get_config,
config[CONF_TYPE],
)
config_type = config_yaml.config_type

# Special case for kogan_switch. Consider also v2.
if config_type == "smartplugv1":
Expand All @@ -113,7 +117,10 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
if entry.version <= 5:
# Migrate unique ids of existing entities to new format
old_id = entry.unique_id
conf_file = get_config(entry.data[CONF_TYPE])
conf_file = await hass.async_add_executor_job(
get_config,
entry.data[CONF_TYPE],
)
if conf_file is None:
_LOGGER.error(NOT_FOUND, entry.data[CONF_TYPE])
return False
Expand Down Expand Up @@ -196,7 +203,10 @@ def update_unique_id(entity_entry):
if entry.version <= 11:
# Migrate unique ids of existing entities to new format
device_id = entry.unique_id
conf_file = get_config(entry.data[CONF_TYPE])
conf_file = await hass.async_add_executor_job(
get_config,
entry.data[CONF_TYPE],
)
if conf_file is None:
_LOGGER.error(
NOT_FOUND,
Expand Down Expand Up @@ -246,7 +256,10 @@ def update_unique_id12(entity_entry):
# Migrate unique ids of existing entities to new format taking into
# account device_class if name is missing.
device_id = entry.unique_id
conf_file = get_config(entry.data[CONF_TYPE])
conf_file = await hass.async_add_executor_job(
get_config,
entry.data[CONF_TYPE],
)
if conf_file is None:
_LOGGER.error(
NOT_FOUND,
Expand Down Expand Up @@ -324,7 +337,10 @@ def update_unique_id13(entity_entry):
# Migrate unique ids of existing entities to new id taking into
# account translation_key, and standardising naming
device_id = entry.unique_id
conf_file = get_config(entry.data[CONF_TYPE])
conf_file = await hass.async_add_executor_job(
get_config,
entry.data[CONF_TYPE],
)
if conf_file is None:
_LOGGER.error(
NOT_FOUND,
Expand Down Expand Up @@ -394,7 +410,10 @@ def update_unique_id13_2(entity_entry):
# Migrate unique ids of existing entities to new id taking into
# account translation_key, and standardising naming
device_id = entry.unique_id
conf_file = get_config(entry.data[CONF_TYPE])
conf_file = await hass.async_add_executor_job(
get_config,
entry.data[CONF_TYPE],
)
if conf_file is None:
_LOGGER.error(
NOT_FOUND,
Expand Down Expand Up @@ -476,7 +495,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
except Exception as e:
raise ConfigEntryNotReady("tuya-local device not ready") from e

device_conf = get_config(entry.data[CONF_TYPE])
device_conf = await hass.async_add_executor_job(
get_config,
entry.data[CONF_TYPE],
)
if device_conf is None:
_LOGGER.error(NOT_FOUND, config[CONF_TYPE])
return False
Expand All @@ -498,7 +520,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
_LOGGER.debug("Unloading entry for device: %s", get_device_id(entry.data))
config = entry.data
data = hass.data[DOMAIN][get_device_id(config)]
device_conf = get_config(config[CONF_TYPE])
device_conf = await hass.async_add_executor_job(
get_config,
config[CONF_TYPE],
)
if device_conf is None:
_LOGGER.error(NOT_FOUND, config[CONF_TYPE])
return False
Expand Down
5 changes: 4 additions & 1 deletion custom_components/tuya_local/helpers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ async def async_tuya_setup_platform(
device = data["device"]
entities = []

cfg = get_config(discovery_info[CONF_TYPE])
cfg = await hass.async_add_executor_job(
get_config,
discovery_info[CONF_TYPE],
)
if cfg is None:
raise ValueError(f"No device config found for {discovery_info}")
ecfg = cfg.primary_entity
Expand Down

1 comment on commit ae24344

@make-all
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue #1981

Please sign in to comment.