4
0
Эх сурвалжийг харах

init: load setup device in an executor job

When IP address is not set (or set to Auto), tinytuya does a network
search in the background, causing warnings about sleeping in the event
loop.  Avoid this by doing the creation in a separate execution job.

Issue #407
Jason Rumney 1 жил өмнө
parent
commit
057c356e1e

+ 16 - 4
custom_components/tuya_local/__init__.py

@@ -40,7 +40,11 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
         # Removal of Auto detection.
         config = {**entry.data, **entry.options, "name": entry.title}
         if config[CONF_TYPE] == CONF_TYPE_AUTO:
-            device = setup_device(hass, config)
+            device = await hass.async_add_executor_job(
+                setup_device,
+                hass,
+                config,
+            )
             config[CONF_TYPE] = await device.async_inferred_type()
             if config[CONF_TYPE] is None:
                 _LOGGER.error(
@@ -66,7 +70,11 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
         # suggest it was removed completely.  But that is probably due to
         # overwriting options without CONF_TYPE.
         if config.get(CONF_TYPE, CONF_TYPE_AUTO) == CONF_TYPE_AUTO:
-            device = setup_device(hass, config)
+            device = await hass.async_add_executor_job(
+                setup_device,
+                hass,
+                config,
+            )
             config[CONF_TYPE] = await device.async_inferred_type()
             if config[CONF_TYPE] is None:
                 _LOGGER.error(
@@ -99,7 +107,11 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
 
         # Special case for kogan_switch.  Consider also v2.
         if config_type == "smartplugv1":
-            device = setup_device(hass, config)
+            device = await hass.async_add_executor_job(
+                setup_device,
+                hass,
+                config,
+            )
             config_type = await device.async_inferred_type()
             if config_type != "smartplugv2":
                 config_type = "smartplugv1"
@@ -491,7 +503,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
     )
     config = {**entry.data, **entry.options, "name": entry.title}
     try:
-        setup_device(hass, config)
+        await hass.async_add_executor_job(setup_device, hass, config)
     except Exception as e:
         raise ConfigEntryNotReady("tuya-local device not ready") from e