Skip to content

Commit

Permalink
fix: prevent blocking calls in source __init__ methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneNulschDE committed Nov 3, 2024
1 parent 1de7d67 commit 95ccbd9
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions custom_components/waste_collection_schedule/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
)
from homeassistant.helpers.translation import async_get_translations
from voluptuous.schema_builder import UNDEFINED

from waste_collection_schedule.collection import Collection
from waste_collection_schedule.exceptions import (
SourceArgumentException,
Expand Down Expand Up @@ -591,9 +592,7 @@ async def __get_arg_schema(
default=UNDEFINED if default is None else default,
description=description,
)
] = (
field_type or cv.string
)
] = field_type or cv.string
else:
_LOGGER.debug(
f"Unsupported type: {type(default)}: {arg_name}: {default}: {field_type}"
Expand Down Expand Up @@ -632,7 +631,10 @@ async def __validate_args_user_input(
self._abort_if_unique_id_configured()

try:
instance = module.Source(**args_input)
instance = await self.hass.async_add_executor_job(
self._get_source_instance, module, args_input
)

resp: list[Collection] = await self.hass.async_add_executor_job(
instance.fetch
)
Expand Down Expand Up @@ -666,6 +668,10 @@ async def __validate_args_user_input(
description_placeholders["fetch_error_message"] = str(e)
return errors, description_placeholders, options

def _get_source_instance(self, module, args_input: dict[str, Any]):
kwargs = args_input
return module.Source(**kwargs)

async def async_source_selected(self) -> None:
async def args_method(args_input):
return await self.async_step_args(args_input)
Expand Down Expand Up @@ -778,9 +784,9 @@ async def async_step_customize(
if user_input.get(CONF_DEDICATED_CALENDAR_TITLE, "") and not user_input.get(
CONF_USE_DEDICATED_CALENDAR, False
):
errors[
CONF_DEDICATED_CALENDAR_TITLE
] = "dedicated_calendar_title_without_use_dedicated_calendar"
errors[CONF_DEDICATED_CALENDAR_TITLE] = (
"dedicated_calendar_title_without_use_dedicated_calendar"
)
else:
if CONF_ALIAS in user_input:
self._fetched_types.remove(types[self._customize_index])
Expand Down

0 comments on commit 95ccbd9

Please sign in to comment.