Add small async_get_current_platform helper method (#50014)

This commit is contained in:
Franck Nijhof 2021-05-03 16:46:42 +02:00 committed by GitHub
parent 6e98b020ac
commit efb1bb08a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -57,8 +57,7 @@ async def async_setup_entry(
"""Set up WLED light based on a config entry."""
coordinator: WLEDDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
platform = entity_platform.current_platform.get()
platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(
SERVICE_EFFECT,
{

View File

@ -696,6 +696,15 @@ current_platform: ContextVar[EntityPlatform | None] = ContextVar(
)
@callback
def async_get_current_platform() -> EntityPlatform:
"""Get the current platform from context."""
platform = current_platform.get()
if platform is None:
raise RuntimeError("Cannot get non-set current platform")
return platform
@callback
def async_get_platforms(
hass: HomeAssistant, integration_name: str