Skip to content

Commit

Permalink
init: load setup device in an executor job
Browse files Browse the repository at this point in the history
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
  • Loading branch information
make-all committed Jun 7, 2024
1 parent ae24344 commit 057c356
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions custom_components/tuya_local/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 057c356

Please sign in to comment.