From 3f3f77c6e63f98b5f3ed935cefb01121ce6e5764 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 27 Apr 2021 08:42:21 -1000 Subject: [PATCH] Reduce config entry setup/unload boilerplate N-P (#49777) --- homeassistant/components/neato/__init__.py | 13 ++---------- homeassistant/components/nest/__init__.py | 15 ++----------- homeassistant/components/netatmo/__init__.py | 15 ++----------- homeassistant/components/nexia/__init__.py | 15 ++----------- .../components/nightscout/__init__.py | 16 ++------------ homeassistant/components/notion/__init__.py | 14 ++----------- homeassistant/components/nuheat/__init__.py | 15 ++----------- homeassistant/components/nuki/__init__.py | 15 ++----------- homeassistant/components/nut/__init__.py | 15 ++----------- homeassistant/components/nws/__init__.py | 16 +++----------- homeassistant/components/nzbget/__init__.py | 16 ++------------ .../components/omnilogic/__init__.py | 15 ++----------- .../components/ondilo_ico/__init__.py | 15 ++----------- homeassistant/components/onewire/__init__.py | 9 ++------ homeassistant/components/onvif/__init__.py | 16 ++------------ .../components/opentherm_gw/__init__.py | 16 +++++--------- homeassistant/components/openuv/__init__.py | 14 +++---------- .../components/openweathermap/__init__.py | 21 +++---------------- .../components/ovo_energy/__init__.py | 10 ++++----- .../components/owntracks/__init__.py | 9 ++++---- homeassistant/components/ozw/__init__.py | 9 +------- .../components/panasonic_viera/__init__.py | 16 +++----------- .../components/philips_js/__init__.py | 14 ++----------- homeassistant/components/pi_hole/__init__.py | 15 +++---------- homeassistant/components/picnic/__init__.py | 15 ++----------- homeassistant/components/plaato/__init__.py | 18 ++++------------ homeassistant/components/plex/__init__.py | 9 ++------ homeassistant/components/plugwise/gateway.py | 14 +++---------- homeassistant/components/point/__init__.py | 6 ++---- .../components/poolsense/__init__.py | 17 ++------------- .../components/powerwall/__init__.py | 15 ++----------- .../components/progettihwsw/__init__.py | 15 ++----------- homeassistant/components/ps4/__init__.py | 11 +++++----- .../pvpc_hourly_pricing/__init__.py | 9 +++----- .../components/pvpc_hourly_pricing/const.py | 2 +- .../components/owntracks/test_config_flow.py | 4 ++-- 36 files changed, 90 insertions(+), 389 deletions(-) diff --git a/homeassistant/components/neato/__init__.py b/homeassistant/components/neato/__init__.py index 036d91534f4..b009e876a7b 100644 --- a/homeassistant/components/neato/__init__.py +++ b/homeassistant/components/neato/__init__.py @@ -1,5 +1,4 @@ """Support for Neato botvac connected vacuum cleaners.""" -import asyncio from datetime import timedelta import logging @@ -92,22 +91,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data[NEATO_LOGIN] = hub - 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) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigType) -> bool: """Unload config entry.""" - unload_functions = ( - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ) - - unload_ok = all(await asyncio.gather(*unload_functions)) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[NEATO_DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/nest/__init__.py b/homeassistant/components/nest/__init__.py index 42b167ee851..d58ad4863ed 100644 --- a/homeassistant/components/nest/__init__.py +++ b/homeassistant/components/nest/__init__.py @@ -1,6 +1,5 @@ """Support for Nest devices.""" -import asyncio import logging from google_nest_sdm.event import EventMessage @@ -191,10 +190,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): hass.data[DOMAIN].pop(DATA_NEST_UNAVAILABLE, None) hass.data[DOMAIN][DATA_SUBSCRIBER] = subscriber - 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) return True @@ -207,14 +203,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): _LOGGER.debug("Stopping nest subscriber") subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER] subscriber.stop_async() - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(DATA_SUBSCRIBER) hass.data[DOMAIN].pop(DATA_NEST_UNAVAILABLE, None) diff --git a/homeassistant/components/netatmo/__init__.py b/homeassistant/components/netatmo/__init__.py index 131542acb0e..1f452f1ccd4 100644 --- a/homeassistant/components/netatmo/__init__.py +++ b/homeassistant/components/netatmo/__init__.py @@ -1,5 +1,4 @@ """The Netatmo integration.""" -import asyncio import logging import secrets @@ -111,10 +110,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): await data_handler.async_setup() hass.data[DOMAIN][entry.entry_id][DATA_HANDLER] = data_handler - 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) async def unregister_webhook(_): if CONF_WEBHOOK_ID not in entry.data: @@ -213,14 +209,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): await hass.data[DOMAIN][entry.entry_id][DATA_HANDLER].async_cleanup() - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/nexia/__init__.py b/homeassistant/components/nexia/__init__.py index 07f6230eb0d..da3e00b2d6a 100644 --- a/homeassistant/components/nexia/__init__.py +++ b/homeassistant/components/nexia/__init__.py @@ -1,5 +1,4 @@ """Support for Nexia / Trane XL Thermostats.""" -import asyncio from datetime import timedelta from functools import partial import logging @@ -73,24 +72,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): UPDATE_COORDINATOR: coordinator, } - 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) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/nightscout/__init__.py b/homeassistant/components/nightscout/__init__.py index dd940531735..8608386c483 100644 --- a/homeassistant/components/nightscout/__init__.py +++ b/homeassistant/components/nightscout/__init__.py @@ -1,5 +1,4 @@ """The Nightscout integration.""" -import asyncio from asyncio import TimeoutError as AsyncIOTimeoutError from aiohttp import ClientError @@ -43,25 +42,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): entry_type="service", ) - 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) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) - + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/notion/__init__.py b/homeassistant/components/notion/__init__.py index ca0ccf08c89..edadca64ec4 100644 --- a/homeassistant/components/notion/__init__.py +++ b/homeassistant/components/notion/__init__.py @@ -99,24 +99,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await coordinator.async_config_entry_first_refresh() - 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) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a Notion config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN][DATA_COORDINATOR].pop(entry.entry_id) diff --git a/homeassistant/components/nuheat/__init__.py b/homeassistant/components/nuheat/__init__.py index c04bfe64720..db50a9a70d9 100644 --- a/homeassistant/components/nuheat/__init__.py +++ b/homeassistant/components/nuheat/__init__.py @@ -1,5 +1,4 @@ """Support for NuHeat thermostats.""" -import asyncio from datetime import timedelta import logging @@ -75,24 +74,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = (thermostat, coordinator) - 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) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/nuki/__init__.py b/homeassistant/components/nuki/__init__.py index 173beca0c4a..f937bddf623 100644 --- a/homeassistant/components/nuki/__init__.py +++ b/homeassistant/components/nuki/__init__.py @@ -1,6 +1,5 @@ """The nuki component.""" -import asyncio from datetime import timedelta import logging @@ -122,24 +121,14 @@ async def async_setup_entry(hass, entry): # Fetch initial data so we have data when entities subscribe await coordinator.async_refresh() - 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) return True async def async_unload_entry(hass, entry): """Unload the Nuki entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/nut/__init__.py b/homeassistant/components/nut/__init__.py index f526e49c6b8..77458b2cfb7 100644 --- a/homeassistant/components/nut/__init__.py +++ b/homeassistant/components/nut/__init__.py @@ -1,5 +1,4 @@ """The nut component.""" -import asyncio from datetime import timedelta import logging @@ -95,10 +94,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): UNDO_UPDATE_LISTENER: undo_listener, } - 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) return True @@ -169,14 +165,7 @@ def find_resources_in_config_entry(config_entry): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]() diff --git a/homeassistant/components/nws/__init__.py b/homeassistant/components/nws/__init__.py index 021b996c945..386a426c1d1 100644 --- a/homeassistant/components/nws/__init__.py +++ b/homeassistant/components/nws/__init__.py @@ -1,7 +1,6 @@ """The National Weather Service integration.""" from __future__ import annotations -import asyncio from collections.abc import Awaitable import datetime import logging @@ -155,23 +154,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): await coordinator_forecast.async_refresh() await coordinator_forecast_hourly.async_refresh() - 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) + return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) if len(hass.data[DOMAIN]) == 0: diff --git a/homeassistant/components/nzbget/__init__.py b/homeassistant/components/nzbget/__init__.py index 3e85839e5d7..7b250d393ea 100644 --- a/homeassistant/components/nzbget/__init__.py +++ b/homeassistant/components/nzbget/__init__.py @@ -1,6 +1,4 @@ """The NZBGet integration.""" -import asyncio - import voluptuous as vol from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry @@ -103,10 +101,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: DATA_UNDO_UPDATE_LISTENER: undo_listener, } - 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) _async_register_services(hass, coordinator) @@ -115,14 +110,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN][entry.entry_id][DATA_UNDO_UPDATE_LISTENER]() diff --git a/homeassistant/components/omnilogic/__init__.py b/homeassistant/components/omnilogic/__init__.py index 8c5d460e549..f50efb7eafb 100644 --- a/homeassistant/components/omnilogic/__init__.py +++ b/homeassistant/components/omnilogic/__init__.py @@ -1,5 +1,4 @@ """The Omnilogic integration.""" -import asyncio import logging from omnilogic import LoginException, OmniLogic, OmniLogicException @@ -57,24 +56,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): OMNI_API: api, } - 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) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/ondilo_ico/__init__.py b/homeassistant/components/ondilo_ico/__init__.py index 0975802b9b2..2b8b2cc22b7 100644 --- a/homeassistant/components/ondilo_ico/__init__.py +++ b/homeassistant/components/ondilo_ico/__init__.py @@ -1,5 +1,4 @@ """The Ondilo ICO integration.""" -import asyncio from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant @@ -29,24 +28,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = api.OndiloClient(hass, entry, implementation) - 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) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/onewire/__init__.py b/homeassistant/components/onewire/__init__.py index cd6d594fafb..4bf0382a92c 100644 --- a/homeassistant/components/onewire/__init__.py +++ b/homeassistant/components/onewire/__init__.py @@ -67,13 +67,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry): async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(config_entry, platform) - for platform in PLATFORMS - ] - ) + unload_ok = await hass.config_entries.async_unload_platforms( + config_entry, PLATFORMS ) if unload_ok: hass.data[DOMAIN].pop(config_entry.unique_id) diff --git a/homeassistant/components/onvif/__init__.py b/homeassistant/components/onvif/__init__.py index 46303781673..f90ccb16760 100644 --- a/homeassistant/components/onvif/__init__.py +++ b/homeassistant/components/onvif/__init__.py @@ -1,6 +1,4 @@ """The ONVIF integration.""" -import asyncio - from onvif.exceptions import ONVIFAuthError, ONVIFError, ONVIFTimeoutError from homeassistant.components.ffmpeg import CONF_EXTRA_ARGUMENTS @@ -88,10 +86,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): if device.capabilities.events: platforms += ["binary_sensor", "sensor"] - 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) entry.async_on_unload( hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, device.async_stop) @@ -110,14 +105,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): platforms += ["binary_sensor", "sensor"] await device.events.async_stop() - return all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in platforms - ] - ) - ) + return await hass.config_entries.async_unload_platforms(entry, platforms) async def _get_snapshot_auth(device): diff --git a/homeassistant/components/opentherm_gw/__init__.py b/homeassistant/components/opentherm_gw/__init__.py index 8686997e748..e3ec9ddef13 100644 --- a/homeassistant/components/opentherm_gw/__init__.py +++ b/homeassistant/components/opentherm_gw/__init__.py @@ -1,5 +1,4 @@ """Support for OpenTherm Gateway devices.""" -import asyncio from datetime import date, datetime import logging @@ -81,6 +80,8 @@ CONFIG_SCHEMA = vol.Schema( extra=vol.ALLOW_EXTRA, ) +PLATFORMS = [COMP_BINARY_SENSOR, COMP_CLIMATE, COMP_SENSOR] + async def options_updated(hass, entry): """Handle options update.""" @@ -112,10 +113,7 @@ async def async_setup_entry(hass, config_entry): # Schedule directly on the loop to avoid blocking HA startup. hass.loop.create_task(gateway.connect_and_subscribe()) - for comp in [COMP_BINARY_SENSOR, COMP_CLIMATE, COMP_SENSOR]: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(config_entry, comp) - ) + hass.config_entries.async_setup_platforms(config_entry, PLATFORMS) register_services(hass) return True @@ -400,14 +398,10 @@ def register_services(hass): async def async_unload_entry(hass, entry): """Cleanup and disconnect from gateway.""" - await asyncio.gather( - hass.config_entries.async_forward_entry_unload(entry, COMP_BINARY_SENSOR), - hass.config_entries.async_forward_entry_unload(entry, COMP_CLIMATE), - hass.config_entries.async_forward_entry_unload(entry, COMP_SENSOR), - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) gateway = hass.data[DATA_OPENTHERM_GW][DATA_GATEWAYS][entry.data[CONF_ID]] await gateway.cleanup() - return True + return unload_ok class OpenThermGatewayDevice: diff --git a/homeassistant/components/openuv/__init__.py b/homeassistant/components/openuv/__init__.py index aeefe435845..e1af166a3c2 100644 --- a/homeassistant/components/openuv/__init__.py +++ b/homeassistant/components/openuv/__init__.py @@ -69,10 +69,7 @@ async def async_setup_entry(hass, config_entry): LOGGER.error("Config entry failed: %s", err) raise ConfigEntryNotReady from err - for platform in PLATFORMS: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(config_entry, platform) - ) + hass.config_entries.async_setup_platforms(config_entry, PLATFORMS) @_verify_domain_control async def update_data(service): @@ -107,13 +104,8 @@ async def async_setup_entry(hass, config_entry): async def async_unload_entry(hass, config_entry): """Unload an OpenUV config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(config_entry, platform) - for platform in PLATFORMS - ] - ) + unload_ok = await hass.config_entries.async_unload_platforms( + config_entry, PLATFORMS ) if unload_ok: hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id) diff --git a/homeassistant/components/openweathermap/__init__.py b/homeassistant/components/openweathermap/__init__.py index f6d47d1dcae..49846a0ad0a 100644 --- a/homeassistant/components/openweathermap/__init__.py +++ b/homeassistant/components/openweathermap/__init__.py @@ -1,5 +1,4 @@ """The openweathermap component.""" -import asyncio import logging from pyowm import OWM @@ -31,12 +30,6 @@ from .weather_update_coordinator import WeatherUpdateCoordinator _LOGGER = logging.getLogger(__name__) -async def async_setup(hass: HomeAssistant, config: dict) -> bool: - """Set up the OpenWeatherMap component.""" - hass.data.setdefault(DOMAIN, {}) - return True - - async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry): """Set up OpenWeatherMap as config entry.""" name = config_entry.data[CONF_NAME] @@ -61,10 +54,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry): ENTRY_WEATHER_COORDINATOR: weather_coordinator, } - for platform in PLATFORMS: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(config_entry, platform) - ) + hass.config_entries.async_setup_platforms(config_entry, PLATFORMS) update_listener = config_entry.add_update_listener(async_update_options) hass.data[DOMAIN][config_entry.entry_id][UPDATE_LISTENER] = update_listener @@ -101,13 +91,8 @@ async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry): async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(config_entry, platform) - for platform in PLATFORMS - ] - ) + unload_ok = await hass.config_entries.async_unload_platforms( + config_entry, PLATFORMS ) if unload_ok: update_listener = hass.data[DOMAIN][config_entry.entry_id][UPDATE_LISTENER] diff --git a/homeassistant/components/ovo_energy/__init__.py b/homeassistant/components/ovo_energy/__init__.py index 749f7b7e249..d94e337e3d3 100644 --- a/homeassistant/components/ovo_energy/__init__.py +++ b/homeassistant/components/ovo_energy/__init__.py @@ -25,6 +25,8 @@ from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN _LOGGER = logging.getLogger(__name__) +PLATFORMS = ["sensor"] + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up OVO Energy from a config entry.""" @@ -75,9 +77,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await coordinator.async_config_entry_first_refresh() # Setup components - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "sensor") - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True @@ -85,11 +85,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigType) -> bool: """Unload OVO Energy config entry.""" # Unload sensors - await hass.config_entries.async_forward_entry_unload(entry, "sensor") + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) del hass.data[DOMAIN][entry.entry_id] - return True + return unload_ok class OVOEnergyEntity(CoordinatorEntity): diff --git a/homeassistant/components/owntracks/__init__.py b/homeassistant/components/owntracks/__init__.py index d3091d7d027..d51566718d6 100644 --- a/homeassistant/components/owntracks/__init__.py +++ b/homeassistant/components/owntracks/__init__.py @@ -32,6 +32,7 @@ CONF_MQTT_TOPIC = "mqtt_topic" CONF_REGION_MAPPING = "region_mapping" CONF_EVENTS_ONLY = "events_only" BEACON_DEV_ID = "beacon" +PLATFORMS = ["device_tracker"] DEFAULT_OWNTRACKS_TOPIC = "owntracks/#" @@ -101,9 +102,7 @@ async def async_setup_entry(hass, entry): DOMAIN, "OwnTracks", webhook_id, handle_webhook ) - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "device_tracker") - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) hass.data[DOMAIN]["unsub"] = hass.helpers.dispatcher.async_dispatcher_connect( DOMAIN, async_handle_message @@ -115,10 +114,10 @@ async def async_setup_entry(hass, entry): async def async_unload_entry(hass, entry): """Unload an OwnTracks config entry.""" hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID]) - await hass.config_entries.async_forward_entry_unload(entry, "device_tracker") + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) hass.data[DOMAIN]["unsub"]() - return True + return unload_ok async def async_remove_entry(hass, entry): diff --git a/homeassistant/components/ozw/__init__.py b/homeassistant/components/ozw/__init__.py index f3d827a57ff..17ab4ca7eb8 100644 --- a/homeassistant/components/ozw/__init__.py +++ b/homeassistant/components/ozw/__init__.py @@ -301,14 +301,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): # noqa: C async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" # cleanup platforms - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if not unload_ok: return False diff --git a/homeassistant/components/panasonic_viera/__init__.py b/homeassistant/components/panasonic_viera/__init__.py index 67cf07dc433..8f0a0e89d45 100644 --- a/homeassistant/components/panasonic_viera/__init__.py +++ b/homeassistant/components/panasonic_viera/__init__.py @@ -1,5 +1,4 @@ """The Panasonic Viera integration.""" -import asyncio from functools import partial import logging from urllib.request import URLError @@ -104,25 +103,16 @@ async def async_setup_entry(hass, config_entry): data={**config, ATTR_DEVICE_INFO: device_info}, ) - for platform in PLATFORMS: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(config_entry, platform) - ) + hass.config_entries.async_setup_platforms(config_entry, PLATFORMS) return True async def async_unload_entry(hass, config_entry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(config_entry, platform) - for platform in PLATFORMS - ] - ) + unload_ok = await hass.config_entries.async_unload_platforms( + config_entry, PLATFORMS ) - if unload_ok: hass.data[DOMAIN].pop(config_entry.entry_id) diff --git a/homeassistant/components/philips_js/__init__.py b/homeassistant/components/philips_js/__init__.py index bf17284d777..cc78402dda9 100644 --- a/homeassistant/components/philips_js/__init__.py +++ b/homeassistant/components/philips_js/__init__.py @@ -43,24 +43,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = coordinator - 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) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/pi_hole/__init__.py b/homeassistant/components/pi_hole/__init__.py index bc486a0c901..7e897887d8d 100644 --- a/homeassistant/components/pi_hole/__init__.py +++ b/homeassistant/components/pi_hole/__init__.py @@ -1,5 +1,4 @@ """The pi_hole component.""" -import asyncio import logging from hole import Hole @@ -126,23 +125,15 @@ async def async_setup_entry(hass, entry): DATA_KEY_COORDINATOR: coordinator, } - for platform in _async_platforms(entry): - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, platform) - ) + hass.config_entries.async_setup_platforms(entry, _async_platforms(entry)) return True async def async_unload_entry(hass, entry): """Unload Pi-hole entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in _async_platforms(entry) - ] - ) + unload_ok = await hass.config_entries.async_unload_platforms( + entry, _async_platforms(entry) ) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/picnic/__init__.py b/homeassistant/components/picnic/__init__.py index 003111088e1..055faadb784 100644 --- a/homeassistant/components/picnic/__init__.py +++ b/homeassistant/components/picnic/__init__.py @@ -1,5 +1,4 @@ """The Picnic integration.""" -import asyncio from python_picnic_api import PicnicAPI @@ -35,24 +34,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): CONF_COORDINATOR: picnic_coordinator, } - for component in PLATFORMS: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, component) - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, component) - for component in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/plaato/__init__.py b/homeassistant/components/plaato/__init__.py index 9ed8d85f232..d73b997398a 100644 --- a/homeassistant/components/plaato/__init__.py +++ b/homeassistant/components/plaato/__init__.py @@ -1,6 +1,5 @@ """Support for Plaato devices.""" -import asyncio from datetime import timedelta import logging @@ -94,11 +93,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): else: await async_setup_coordinator(hass, entry) - for platform in PLATFORMS: - if entry.options.get(platform, True): - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, platform) - ) + hass.config_entries.async_setup_platforms( + entry, [platform for platform in PLATFORMS if entry.options.get(platform, True)] + ) return True @@ -177,14 +174,7 @@ async def async_unload_coordinator(hass: HomeAssistant, entry: ConfigEntry): async def async_unload_platforms(hass: HomeAssistant, entry: ConfigEntry, platforms): """Unload platforms.""" - unloaded = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in platforms - ] - ) - ) + unloaded = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unloaded: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/plex/__init__.py b/homeassistant/components/plex/__init__.py index ec2c6480776..c534384a7eb 100644 --- a/homeassistant/components/plex/__init__.py +++ b/homeassistant/components/plex/__init__.py @@ -1,5 +1,4 @@ """Support to embed Plex.""" -import asyncio from functools import partial import logging @@ -232,15 +231,11 @@ async def async_unload_entry(hass, entry): for unsub in dispatchers: unsub() - tasks = [ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - await asyncio.gather(*tasks) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) hass.data[PLEX_DOMAIN][SERVERS].pop(server_id) - return True + return unload_ok async def async_options_updated(hass, entry): diff --git a/homeassistant/components/plugwise/gateway.py b/homeassistant/components/plugwise/gateway.py index 3f805f1475d..a6d8960edf2 100644 --- a/homeassistant/components/plugwise/gateway.py +++ b/homeassistant/components/plugwise/gateway.py @@ -136,10 +136,7 @@ async def async_setup_entry_gw(hass: HomeAssistant, entry: ConfigEntry) -> bool: if single_master_thermostat is None: platforms = SENSOR_PLATFORMS - 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) return True @@ -154,13 +151,8 @@ async def _update_listener(hass: HomeAssistant, entry: ConfigEntry): async def async_unload_entry_gw(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS_GATEWAY - ] - ) + unload_ok = await hass.config_entries.async_unload_platforms( + entry, PLATFORMS_GATEWAY ) hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]() diff --git a/homeassistant/components/point/__init__.py b/homeassistant/components/point/__init__.py index 38561d42abc..6128b6ae162 100644 --- a/homeassistant/components/point/__init__.py +++ b/homeassistant/components/point/__init__.py @@ -139,13 +139,11 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): session = hass.data[DOMAIN].pop(entry.entry_id) await session.remove_webhook() - for platform in PLATFORMS: - await hass.config_entries.async_forward_entry_unload(entry, platform) - + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if not hass.data[DOMAIN]: hass.data.pop(DOMAIN) - return True + return unload_ok async def handle_webhook(hass, webhook_id, request): diff --git a/homeassistant/components/poolsense/__init__.py b/homeassistant/components/poolsense/__init__.py index 4fee2d01a73..89e340ee95e 100644 --- a/homeassistant/components/poolsense/__init__.py +++ b/homeassistant/components/poolsense/__init__.py @@ -1,5 +1,4 @@ """The PoolSense integration.""" -import asyncio from datetime import timedelta import logging @@ -46,28 +45,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = coordinator - 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) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) - + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) - return unload_ok diff --git a/homeassistant/components/powerwall/__init__.py b/homeassistant/components/powerwall/__init__.py index e3c08e74770..1792ca19fc8 100644 --- a/homeassistant/components/powerwall/__init__.py +++ b/homeassistant/components/powerwall/__init__.py @@ -1,5 +1,4 @@ """The Tesla Powerwall integration.""" -import asyncio from datetime import timedelta import logging @@ -154,10 +153,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): await coordinator.async_config_entry_first_refresh() - 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) return True @@ -210,14 +206,7 @@ def _fetch_powerwall_data(power_wall): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) hass.data[DOMAIN][entry.entry_id][POWERWALL_HTTP_SESSION].close() diff --git a/homeassistant/components/progettihwsw/__init__.py b/homeassistant/components/progettihwsw/__init__.py index bb8757e0962..78ea16bb26c 100644 --- a/homeassistant/components/progettihwsw/__init__.py +++ b/homeassistant/components/progettihwsw/__init__.py @@ -1,5 +1,4 @@ """Automation manager for boards manufactured by ProgettiHWSW Italy.""" -import asyncio from ProgettiHWSW.ProgettiHWSWAPI import ProgettiHWSWAPI from ProgettiHWSW.input import Input @@ -23,24 +22,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): # Check board validation again to load new values to API. await hass.data[DOMAIN][entry.entry_id].check_board() - 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) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS - ] - ) - ) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/ps4/__init__.py b/homeassistant/components/ps4/__init__.py index 51583b5f4bc..65940b9dc48 100644 --- a/homeassistant/components/ps4/__init__.py +++ b/homeassistant/components/ps4/__init__.py @@ -38,6 +38,8 @@ PS4_COMMAND_SCHEMA = vol.Schema( } ) +PLATFORMS = ["media_player"] + class PS4Data: """Init Data Class.""" @@ -59,18 +61,15 @@ async def async_setup(hass, config): return True -async def async_setup_entry(hass, config_entry): +async def async_setup_entry(hass, entry): """Set up PS4 from a config entry.""" - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(config_entry, "media_player") - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True async def async_unload_entry(hass, entry): """Unload a PS4 config entry.""" - await hass.config_entries.async_forward_entry_unload(entry, "media_player") - return True + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) async def async_migrate_entry(hass, entry): diff --git a/homeassistant/components/pvpc_hourly_pricing/__init__.py b/homeassistant/components/pvpc_hourly_pricing/__init__.py index 5930da52313..dfb7282aae9 100644 --- a/homeassistant/components/pvpc_hourly_pricing/__init__.py +++ b/homeassistant/components/pvpc_hourly_pricing/__init__.py @@ -6,7 +6,7 @@ from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant import homeassistant.helpers.config_validation as cv -from .const import ATTR_TARIFF, DEFAULT_NAME, DEFAULT_TARIFF, DOMAIN, PLATFORM, TARIFFS +from .const import ATTR_TARIFF, DEFAULT_NAME, DEFAULT_TARIFF, DOMAIN, PLATFORMS, TARIFFS UI_CONFIG_SCHEMA = vol.Schema( { @@ -44,13 +44,10 @@ async def async_setup(hass: HomeAssistant, config: dict): async def async_setup_entry(hass: HomeAssistant, entry: config_entries.ConfigEntry): """Set up pvpc hourly pricing from a config entry.""" - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, PLATFORM) - ) - + hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True async def async_unload_entry(hass: HomeAssistant, entry: config_entries.ConfigEntry): """Unload a config entry.""" - return await hass.config_entries.async_forward_entry_unload(entry, PLATFORM) + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/pvpc_hourly_pricing/const.py b/homeassistant/components/pvpc_hourly_pricing/const.py index d75ad9fe35c..9e11bc57d6d 100644 --- a/homeassistant/components/pvpc_hourly_pricing/const.py +++ b/homeassistant/components/pvpc_hourly_pricing/const.py @@ -2,7 +2,7 @@ from aiopvpc import TARIFFS DOMAIN = "pvpc_hourly_pricing" -PLATFORM = "sensor" +PLATFORMS = ["sensor"] ATTR_TARIFF = "tariff" DEFAULT_NAME = "PVPC" DEFAULT_TARIFF = TARIFFS[1] diff --git a/tests/components/owntracks/test_config_flow.py b/tests/components/owntracks/test_config_flow.py index 93d4bf5385b..c56770099ef 100644 --- a/tests/components/owntracks/test_config_flow.py +++ b/tests/components/owntracks/test_config_flow.py @@ -154,8 +154,8 @@ async def test_unload(hass): assert entry.data["webhook_id"] in hass.data["webhook"] with patch( - "homeassistant.config_entries.ConfigEntries.async_forward_entry_unload", - return_value=None, + "homeassistant.config_entries.ConfigEntries.async_unload_platforms", + return_value=True, ) as mock_unload: assert await hass.config_entries.async_unload(entry.entry_id)