From 156435d1c28e02cc24b11b4b1304e7cc46f7f1e2 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sat, 4 Dec 2021 08:57:24 +0100 Subject: [PATCH] Update integrations to use `async_setup_platforms` (#60956) * Replace forward_entry_setup with setup_platforms * Apply suggestions from code review Co-authored-by: Marvin Wichmann Co-authored-by: Marvin Wichmann --- .../components/ambiclimate/__init__.py | 13 +++++---- homeassistant/components/cast/__init__.py | 13 +++++---- homeassistant/components/ios/__init__.py | 13 +++++---- .../components/nest/legacy/__init__.py | 13 +++++---- .../components/plum_lightpad/__init__.py | 14 ++++++---- homeassistant/components/roon/server.py | 9 ++---- homeassistant/components/withings/__init__.py | 20 ++++++------- homeassistant/components/zwave/__init__.py | 28 +++++++++---------- 8 files changed, 65 insertions(+), 58 deletions(-) diff --git a/homeassistant/components/ambiclimate/__init__.py b/homeassistant/components/ambiclimate/__init__.py index ac6334638a458..18cf8c177d99b 100644 --- a/homeassistant/components/ambiclimate/__init__.py +++ b/homeassistant/components/ambiclimate/__init__.py @@ -1,7 +1,9 @@ """Support for Ambiclimate devices.""" import voluptuous as vol -from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET, Platform +from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv from . import config_flow @@ -19,6 +21,8 @@ extra=vol.ALLOW_EXTRA, ) +PLATFORMS = [Platform.CLIMATE] + async def async_setup(hass, config) -> bool: """Set up Ambiclimate components.""" @@ -34,10 +38,7 @@ async def async_setup(hass, config) -> bool: return True -async def async_setup_entry(hass, entry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Ambiclimate from a config entry.""" - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "climate") - ) - + hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True diff --git a/homeassistant/components/cast/__init__.py b/homeassistant/components/cast/__init__.py index 9ccac6e4f6cda..4401553869f2c 100644 --- a/homeassistant/components/cast/__init__.py +++ b/homeassistant/components/cast/__init__.py @@ -4,6 +4,8 @@ import voluptuous as vol from homeassistant import config_entries +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv from . import home_assistant_cast @@ -15,6 +17,8 @@ _LOGGER = logging.getLogger(__name__) +PLATFORMS = [Platform.MEDIA_PLAYER] + async def async_setup(hass, config): """Set up the Cast component.""" @@ -41,13 +45,12 @@ async def async_setup(hass, config): return True -async def async_setup_entry(hass, entry: config_entries.ConfigEntry): +async def async_setup_entry( + hass: HomeAssistant, entry: config_entries.ConfigEntry +) -> bool: """Set up Cast from a config entry.""" await home_assistant_cast.async_setup_ha_cast(hass, entry) - - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "media_player") - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True diff --git a/homeassistant/components/ios/__init__.py b/homeassistant/components/ios/__init__.py index 048107910d101..0f1cadb55ba9b 100644 --- a/homeassistant/components/ios/__init__.py +++ b/homeassistant/components/ios/__init__.py @@ -6,7 +6,8 @@ from homeassistant import config_entries from homeassistant.components.http import HomeAssistantView -from homeassistant.core import callback +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_validation as cv, discovery from homeassistant.helpers.dispatcher import async_dispatcher_send @@ -209,6 +210,8 @@ CONFIGURATION_FILE = ".ios.conf" +PLATFORMS = [Platform.SENSOR] + def devices_with_push(hass): """Return a dictionary of push enabled targets.""" @@ -272,11 +275,11 @@ async def async_setup(hass, config): return True -async def async_setup_entry(hass, entry): +async def async_setup_entry( + hass: HomeAssistant, entry: config_entries.ConfigEntry +) -> bool: """Set up an iOS entry.""" - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "sensor") - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) hass.http.register_view(iOSIdentifyDeviceView(hass.config.path(CONFIGURATION_FILE))) hass.http.register_view(iOSPushConfigView(hass.data[DOMAIN][CONF_USER][CONF_PUSH])) diff --git a/homeassistant/components/nest/legacy/__init__.py b/homeassistant/components/nest/legacy/__init__.py index 85ff98f6420c7..2259ee0ad5c49 100644 --- a/homeassistant/components/nest/legacy/__init__.py +++ b/homeassistant/components/nest/legacy/__init__.py @@ -17,6 +17,7 @@ CONF_STRUCTURE, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, + Platform, ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import config_validation as cv @@ -29,7 +30,12 @@ _CONFIGURING = {} _LOGGER = logging.getLogger(__name__) -PLATFORMS = ["climate", "camera", "sensor", "binary_sensor"] +PLATFORMS = [ + Platform.BINARY_SENSOR, + Platform.CAMERA, + Platform.CLIMATE, + Platform.SENSOR, +] # Configuration for the legacy nest API SERVICE_CANCEL_ETA = "cancel_eta" @@ -134,10 +140,7 @@ async def async_setup_legacy_entry(hass: HomeAssistant, entry: ConfigEntry) -> b if not await hass.async_add_executor_job(hass.data[DATA_NEST].initialize): return False - for platform in PLATFORMS: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, platform) - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) def validate_structures(target_structures): all_structures = [structure.name for structure in nest.structures] diff --git a/homeassistant/components/plum_lightpad/__init__.py b/homeassistant/components/plum_lightpad/__init__.py index f92d087b79d48..cca40f4e5a96b 100644 --- a/homeassistant/components/plum_lightpad/__init__.py +++ b/homeassistant/components/plum_lightpad/__init__.py @@ -6,7 +6,12 @@ import voluptuous as vol from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, EVENT_HOMEASSISTANT_STOP +from homeassistant.const import ( + CONF_PASSWORD, + CONF_USERNAME, + EVENT_HOMEASSISTANT_STOP, + Platform, +) from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady import homeassistant.helpers.config_validation as cv @@ -32,7 +37,7 @@ extra=vol.ALLOW_EXTRA, ) -PLATFORMS = ["light"] +PLATFORMS = [Platform.LIGHT] async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: @@ -71,10 +76,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = plum - for platform in PLATFORMS: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, platform) - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) def cleanup(event): """Clean up resources.""" diff --git a/homeassistant/components/roon/server.py b/homeassistant/components/roon/server.py index d216dca419dd4..8301bf73fdf7c 100644 --- a/homeassistant/components/roon/server.py +++ b/homeassistant/components/roon/server.py @@ -4,7 +4,7 @@ from roonapi import RoonApi -from homeassistant.const import CONF_API_KEY, CONF_HOST +from homeassistant.const import CONF_API_KEY, CONF_HOST, Platform from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.util.dt import utcnow @@ -12,6 +12,7 @@ _LOGGER = logging.getLogger(__name__) FULL_SYNC_INTERVAL = 30 +PLATFORMS = [Platform.MEDIA_PLAYER] class RoonServer: @@ -51,11 +52,7 @@ async def async_setup(self, tries=0): self.roon_id = core_id if core_id is not None else host # initialize media_player platform - hass.async_create_task( - hass.config_entries.async_forward_entry_setup( - self.config_entry, "media_player" - ) - ) + hass.config_entries.async_setup_platforms(self.config_entry, PLATFORMS) # Initialize Roon background polling asyncio.create_task(self.async_do_loop()) diff --git a/homeassistant/components/withings/__init__.py b/homeassistant/components/withings/__init__.py index 7132b3bff9dc6..800f8b654bbd3 100644 --- a/homeassistant/components/withings/__init__.py +++ b/homeassistant/components/withings/__init__.py @@ -13,13 +13,16 @@ from withings_api.common import NotifyAppli from homeassistant.components import webhook -from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN -from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.webhook import ( async_unregister as async_unregister_webhook, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET, CONF_WEBHOOK_ID +from homeassistant.const import ( + CONF_CLIENT_ID, + CONF_CLIENT_SECRET, + CONF_WEBHOOK_ID, + Platform, +) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.event import async_call_later @@ -36,6 +39,7 @@ ) DOMAIN = const.DOMAIN +PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR] CONFIG_SCHEMA = vol.Schema( { @@ -143,12 +147,7 @@ def async_call_later_callback(now) -> None: # Start subscription check in the background, outside this component's setup. async_call_later(hass, 1, async_call_later_callback) - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, BINARY_SENSOR_DOMAIN) - ) - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, SENSOR_DOMAIN) - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True @@ -162,8 +161,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await asyncio.gather( data_manager.async_unsubscribe_webhook(), - hass.config_entries.async_forward_entry_unload(entry, BINARY_SENSOR_DOMAIN), - hass.config_entries.async_forward_entry_unload(entry, SENSOR_DOMAIN), + hass.config_entries.async_unload_platforms(entry, PLATFORMS), ) async_remove_data_manager(hass, entry) diff --git a/homeassistant/components/zwave/__init__.py b/homeassistant/components/zwave/__init__.py index ef3ab223248d9..f21b75aaffc16 100644 --- a/homeassistant/components/zwave/__init__.py +++ b/homeassistant/components/zwave/__init__.py @@ -15,8 +15,9 @@ ATTR_VIA_DEVICE, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, + Platform, ) -from homeassistant.core import CoreState, callback +from homeassistant.core import CoreState, HomeAssistant, callback from homeassistant.helpers import discovery import homeassistant.helpers.config_validation as cv from homeassistant.helpers.device_registry import ( @@ -98,14 +99,14 @@ DEFAULT_CONF_REFRESH_DELAY = 5 PLATFORMS = [ - "binary_sensor", - "climate", - "cover", - "fan", - "lock", - "light", - "sensor", - "switch", + Platform.BINARY_SENSOR, + Platform.CLIMATE, + Platform.COVER, + Platform.FAN, + Platform.LIGHT, + Platform.LOCK, + Platform.SENSOR, + Platform.SWITCH, ] RENAME_NODE_SCHEMA = vol.Schema( @@ -341,7 +342,9 @@ async def async_setup(hass, config): return True -async def async_setup_entry(hass, config_entry): # noqa: C901 +async def async_setup_entry( # noqa: C901 + hass: HomeAssistant, config_entry: config_entries.ConfigEntry +) -> bool: """Set up Z-Wave from a config entry. Will automatically load components to support devices found on the network. @@ -1021,10 +1024,7 @@ def _finalize_start(): hass.services.async_register(DOMAIN, const.SERVICE_START_NETWORK, start_zwave) - for entry_component in PLATFORMS: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(config_entry, entry_component) - ) + hass.config_entries.async_setup_platforms(config_entry, PLATFORMS) return True