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 <erik@montnemery.com>

* Update homeassistant/components/device_automation/__init__.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Add Exception to type hint

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
epenet 2022-06-02 09:06:22 +02:00 committed by GitHub
parent 999b3a4f7b
commit c2fdac2014
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -189,22 +189,42 @@ def _async_set_entity_device_automation_metadata(
automation["metadata"]["secondary"] = bool(entry.entity_category or entry.hidden_by) 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( 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.""" """List device automations."""
try: try:
platform = await async_get_device_automation_platform( platform = await async_get_device_automation_platform(
hass, domain, automation_type hass, domain, automation_type
) )
except InvalidDeviceAutomationConfig: except InvalidDeviceAutomationConfig:
return {} return []
function_name = automation_type.value.get_automations_func 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 for device_id in device_ids
), ),
return_exceptions=return_exceptions, return_exceptions=return_exceptions,
@ -290,7 +310,12 @@ async def _async_get_device_automation_capabilities(
return {} return {}
try: 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: except InvalidDeviceAutomationConfig:
return {} return {}