From c2fdac20146e58cebf02259b946f11e1d6ed3ab9 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 2 Jun 2022 09:06:22 +0200 Subject: [PATCH] Allow non-async functions in device automation (#72147) * Remove async requirement for get_capabilities_func * Add comment * Remove async requirement for get_automations_func * Update homeassistant/components/device_automation/__init__.py Co-authored-by: Erik Montnemery * Update homeassistant/components/device_automation/__init__.py Co-authored-by: Erik Montnemery * Add Exception to type hint Co-authored-by: Erik Montnemery --- .../components/device_automation/__init__.py | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/device_automation/__init__.py b/homeassistant/components/device_automation/__init__.py index 61fd93354fe..99629f3dd23 100644 --- a/homeassistant/components/device_automation/__init__.py +++ b/homeassistant/components/device_automation/__init__.py @@ -189,22 +189,42 @@ def _async_set_entity_device_automation_metadata( automation["metadata"]["secondary"] = bool(entry.entity_category or entry.hidden_by) +async def _async_get_automation_for_device( + hass: HomeAssistant, + platform: DeviceAutomationPlatformType, + function_name: str, + device_id: str, +) -> list[dict[str, Any]]: + """List device automations.""" + automations = getattr(platform, function_name)(hass, device_id) + if asyncio.iscoroutine(automations): + # Using a coroutine to get device automations is deprecated + # enable warning when core is fully migrated + # then remove in Home Assistant Core xxxx.xx + return await automations # type: ignore[no-any-return] + return automations # type: ignore[no-any-return] + + async def _async_get_device_automations_from_domain( - hass, domain, automation_type, device_ids, return_exceptions -): + hass: HomeAssistant, + domain: str, + automation_type: DeviceAutomationType, + device_ids: Iterable[str], + return_exceptions: bool, +) -> list[list[dict[str, Any]] | Exception]: """List device automations.""" try: platform = await async_get_device_automation_platform( hass, domain, automation_type ) except InvalidDeviceAutomationConfig: - return {} + return [] function_name = automation_type.value.get_automations_func - return await asyncio.gather( + return await asyncio.gather( # type: ignore[no-any-return] *( - getattr(platform, function_name)(hass, device_id) + _async_get_automation_for_device(hass, platform, function_name, device_id) for device_id in device_ids ), return_exceptions=return_exceptions, @@ -290,7 +310,12 @@ async def _async_get_device_automation_capabilities( return {} try: - capabilities = await getattr(platform, function_name)(hass, automation) + capabilities = getattr(platform, function_name)(hass, automation) + if asyncio.iscoroutine(capabilities): + # Using a coroutine to get device automation capabitilites is deprecated + # enable warning when core is fully migrated + # then remove in Home Assistant Core xxxx.xx + capabilities = await capabilities except InvalidDeviceAutomationConfig: return {}