Przeglądaj źródła

init: load config in executor job

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.
Jason Rumney 1 rok temu
rodzic
commit
ae24344f47

+ 33 - 8
custom_components/tuya_local/__init__.py

@@ -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":
@@ -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
@@ -196,7 +203,10 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
     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,
@@ -246,7 +256,10 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
         # 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,
@@ -324,7 +337,10 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
         # 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,
@@ -394,7 +410,10 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
         # 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,
@@ -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
@@ -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

+ 4 - 1
custom_components/tuya_local/helpers/config.py

@@ -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