Skip to content

Commit

Permalink
Fix improt_modules blocking the event loop.
Browse files Browse the repository at this point in the history
* This will handle HA 2024.3.0 later or before however later will only supports 2024.3 and later
* Add import_modules into executor
  • Loading branch information
xZetsubou committed May 3, 2024
1 parent 5ad7962 commit 7d7535a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
4 changes: 1 addition & 3 deletions custom_components/localtuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from homeassistant.helpers.event import async_track_time_interval

from .coordinator import TuyaDevice, HassLocalTuyaData, TuyaCloudApi
from .config_flow import ENTRIES_VERSION, config_schema
from .config_flow import ENTRIES_VERSION
from .const import (
ATTR_UPDATED_AT,
CONF_GATEWAY_ID,
Expand All @@ -51,8 +51,6 @@
RECONNECT_INTERVAL = timedelta(seconds=5)
RECONNECT_TASK = "localtuya_reconnect_interval"

CONFIG_SCHEMA = config_schema()

CONF_DP = "dp"
CONF_VALUE = "value"

Expand Down
46 changes: 19 additions & 27 deletions custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,8 @@ async def async_step_entity(self, user_input=None):
if len(self.entities) == len(self.device_data[CONF_ENTITIES]):
return self._update_entry(self.device_data)

schema = platform_schema(
self.current_entity[CONF_PLATFORM], self.dps_strings, allow_id=False
schema = await platform_schema(
self.hass, self.current_entity[CONF_PLATFORM], self.dps_strings, False
)
return self.async_show_form(
step_id="entity",
Expand Down Expand Up @@ -770,8 +770,8 @@ async def async_step_configure_entity(self, user_input=None):
return await self.async_step_pick_entity_type(user_input)

if self.editing_device:
schema = platform_schema(
self.current_entity[CONF_PLATFORM], self.dps_strings, allow_id=False
schema = await platform_schema(
self.hass, self.current_entity[CONF_PLATFORM], self.dps_strings, False
)
schema = schema_defaults(schema, self.dps_strings, **self.current_entity)
placeholders = {
Expand All @@ -780,7 +780,9 @@ async def async_step_configure_entity(self, user_input=None):
}
else:
available_dps = self.available_dps_strings()
schema = platform_schema(self.selected_platform, available_dps)
schema = await platform_schema(
self.hass, self.selected_platform, available_dps
)
placeholders = {
"entity": "an entity",
"platform": self.selected_platform,
Expand Down Expand Up @@ -1109,7 +1111,9 @@ def gen_dps_strings():
return [f"{dp} (value: ?)" for dp in range(1, 256)]


def platform_schema(platform, dps_strings, allow_id=True, yaml=False):
async def platform_schema(
hass: core.HomeAssistant, platform, dps_strings, allow_id=True, yaml=False
):
"""Generate input validation schema for a platform."""
# decide default value of device by platform.
schema = {}
Expand All @@ -1122,7 +1126,15 @@ def platform_schema(platform, dps_strings, allow_id=True, yaml=False):
schema[
vol.Required(CONF_ENTITY_CATEGORY, default=str(default_category(platform)))
] = _col_to_select(ENTITY_CATEGORY)
return vol.Schema(schema).extend(flow_schema(platform, dps_strings))

try: # requires HA >= 2024.3 -> Later this will be remove and update HACS version requirement.
plat_schema = await hass.async_add_import_executor_job(
flow_schema, platform, dps_strings
)
except AttributeError:
plat_schema = flow_schema(platform, dps_strings)

return vol.Schema(schema).extend(plat_schema)


def default_category(_platform):
Expand Down Expand Up @@ -1154,26 +1166,6 @@ def strip_dps_values(user_input, dps_strings):
return stripped


def config_schema():
"""Build schema used for setting up component."""
entity_schemas = [
platform_schema(plats, range(1, 256), yaml=True) for plats in PLATFORMS.values()
]
return vol.Schema(
{
DOMAIN: vol.All(
cv.ensure_list,
[
DEVICE_SCHEMA.extend(
{vol.Required(CONF_ENTITIES): [vol.Any(*entity_schemas)]}
)
],
)
},
extra=vol.ALLOW_EXTRA,
)


async def validate_input(hass: core.HomeAssistant, entry_id, data):
"""Validate the user input allows us to connect."""
detected_dps = {}
Expand Down

0 comments on commit 7d7535a

Please sign in to comment.