From efb1bb08a47b685ed19f626d39a723b737a5372f Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 3 May 2021 16:46:42 +0200 Subject: [PATCH] Add small async_get_current_platform helper method (#50014) --- homeassistant/components/wled/light.py | 3 +-- homeassistant/helpers/entity_platform.py | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/wled/light.py b/homeassistant/components/wled/light.py index 98e60b9bb81..0151737ece9 100644 --- a/homeassistant/components/wled/light.py +++ b/homeassistant/components/wled/light.py @@ -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, { diff --git a/homeassistant/helpers/entity_platform.py b/homeassistant/helpers/entity_platform.py index 83e72c7491d..6350467c05b 100644 --- a/homeassistant/helpers/entity_platform.py +++ b/homeassistant/helpers/entity_platform.py @@ -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