From b10534359be835a39fca382d6d0c500e30083b21 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 27 Apr 2021 06:49:13 -1000 Subject: [PATCH] Reduce config entry setup/unload boilerplate K-M (#49775) --- .../components/keenetic_ndms2/__init__.py | 12 ++++------ homeassistant/components/kmtronic/__init__.py | 15 ++---------- homeassistant/components/kodi/__init__.py | 15 ++---------- .../components/konnected/__init__.py | 15 ++---------- .../components/kostal_plenticore/__init__.py | 22 +++-------------- homeassistant/components/kulersky/__init__.py | 15 ++---------- homeassistant/components/lcn/__init__.py | 15 +++--------- homeassistant/components/lifx/__init__.py | 12 ++++------ homeassistant/components/litejet/__init__.py | 16 ++----------- .../components/litterrobot/__init__.py | 15 ++---------- homeassistant/components/local_ip/__init__.py | 9 +++---- homeassistant/components/local_ip/const.py | 2 +- homeassistant/components/locative/__init__.py | 7 +++--- .../components/logi_circle/__init__.py | 10 +++----- .../components/luftdaten/__init__.py | 8 +++---- .../components/lutron_caseta/__init__.py | 15 +++--------- homeassistant/components/lyric/__init__.py | 15 ++---------- homeassistant/components/mazda/__init__.py | 15 ++---------- homeassistant/components/melcloud/__init__.py | 14 ++++------- homeassistant/components/met/__init__.py | 12 ++++++---- .../components/met_eireann/__init__.py | 12 ++++++---- .../components/meteo_france/__init__.py | 15 ++---------- .../components/metoffice/__init__.py | 15 ++---------- homeassistant/components/mikrotik/__init__.py | 8 +++++-- homeassistant/components/mikrotik/const.py | 2 ++ homeassistant/components/mikrotik/hub.py | 7 ++---- homeassistant/components/mill/__init__.py | 13 ++++------ .../components/minecraft_server/__init__.py | 15 ++++-------- .../components/mobile_app/__init__.py | 15 ++---------- .../components/monoprice/__init__.py | 16 ++----------- .../components/motion_blinds/__init__.py | 15 +++--------- .../components/motioneye/__init__.py | 9 +------ homeassistant/components/mullvad/__init__.py | 16 ++----------- homeassistant/components/myq/__init__.py | 24 +++---------------- .../components/mysensors/__init__.py | 9 ++----- .../kostal_plenticore/test_config_flow.py | 3 --- tests/components/myq/test_config_flow.py | 3 --- 37 files changed, 104 insertions(+), 352 deletions(-) diff --git a/homeassistant/components/keenetic_ndms2/__init__.py b/homeassistant/components/keenetic_ndms2/__init__.py index 6156fb00d02..787e6a5f5f1 100644 --- a/homeassistant/components/keenetic_ndms2/__init__.py +++ b/homeassistant/components/keenetic_ndms2/__init__.py @@ -38,10 +38,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b UNDO_UPDATE_LISTENER: undo_listener, } - 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 @@ -50,8 +47,9 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> """Unload a config entry.""" hass.data[DOMAIN][config_entry.entry_id][UNDO_UPDATE_LISTENER]() - for platform in PLATFORMS: - await hass.config_entries.async_forward_entry_unload(config_entry, platform) + unload_ok = await hass.config_entries.async_unload_platforms( + config_entry, PLATFORMS + ) router: KeeneticRouter = hass.data[DOMAIN][config_entry.entry_id][ROUTER] @@ -59,7 +57,7 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> hass.data[DOMAIN].pop(config_entry.entry_id) - return True + return unload_ok async def update_listener(hass, config_entry): diff --git a/homeassistant/components/kmtronic/__init__.py b/homeassistant/components/kmtronic/__init__.py index a028a62cbc5..3b8da77faab 100644 --- a/homeassistant/components/kmtronic/__init__.py +++ b/homeassistant/components/kmtronic/__init__.py @@ -1,5 +1,4 @@ """The kmtronic integration.""" -import asyncio from datetime import timedelta import logging @@ -59,10 +58,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): DATA_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) update_listener = entry.add_update_listener(async_update_options) hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER] = update_listener @@ -77,14 +73,7 @@ async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry) - 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: update_listener = hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER] update_listener() diff --git a/homeassistant/components/kodi/__init__.py b/homeassistant/components/kodi/__init__.py index d42b4aa2ec4..fe318b103d1 100644 --- a/homeassistant/components/kodi/__init__.py +++ b/homeassistant/components/kodi/__init__.py @@ -1,6 +1,5 @@ """The kodi component.""" -import asyncio import logging from pykodi import CannotConnectError, InvalidAuthError, Kodi, get_kodi_connection @@ -67,24 +66,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): DATA_REMOVE_LISTENER: remove_stop_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 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: data = hass.data[DOMAIN].pop(entry.entry_id) await data[DATA_CONNECTION].close() diff --git a/homeassistant/components/konnected/__init__.py b/homeassistant/components/konnected/__init__.py index db1e20204cd..857521b9fad 100644 --- a/homeassistant/components/konnected/__init__.py +++ b/homeassistant/components/konnected/__init__.py @@ -1,5 +1,4 @@ """Support for Konnected devices.""" -import asyncio import copy import hmac import json @@ -261,10 +260,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): # async_connect will handle retries until it establishes a connection await client.async_connect() - 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) # config entry specific data to enable unload hass.data[DOMAIN][entry.entry_id] = { @@ -275,14 +271,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): 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/kostal_plenticore/__init__.py b/homeassistant/components/kostal_plenticore/__init__.py index f06657fdaa1..f00e6ee1327 100644 --- a/homeassistant/components/kostal_plenticore/__init__.py +++ b/homeassistant/components/kostal_plenticore/__init__.py @@ -1,5 +1,4 @@ """The Kostal Plenticore Solar Inverter integration.""" -import asyncio import logging from kostal.plenticore import PlenticoreApiException @@ -15,14 +14,9 @@ _LOGGER = logging.getLogger(__name__) PLATFORMS = ["sensor"] -async def async_setup(hass: HomeAssistant, config: dict) -> bool: - """Set up the Kostal Plenticore Solar Inverter component.""" - hass.data.setdefault(DOMAIN, {}) - return True - - async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Kostal Plenticore Solar Inverter from a config entry.""" + hass.data.setdefault(DOMAIN, {}) plenticore = Plenticore(hass, entry) @@ -31,24 +25,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data[DOMAIN][entry.entry_id] = plenticore - 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) -> bool: """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: # remove API object plenticore = hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/kulersky/__init__.py b/homeassistant/components/kulersky/__init__.py index 358d13dee56..6409d435bf3 100644 --- a/homeassistant/components/kulersky/__init__.py +++ b/homeassistant/components/kulersky/__init__.py @@ -1,5 +1,4 @@ """Kuler Sky lights integration.""" -import asyncio from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant @@ -16,10 +15,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): if DATA_ADDRESSES not in hass.data[DOMAIN]: hass.data[DOMAIN][DATA_ADDRESSES] = set() - 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 @@ -33,11 +29,4 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): hass.data.pop(DOMAIN, None) - 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) diff --git a/homeassistant/components/lcn/__init__.py b/homeassistant/components/lcn/__init__.py index 9384fbed29d..faf524f6585 100644 --- a/homeassistant/components/lcn/__init__.py +++ b/homeassistant/components/lcn/__init__.py @@ -1,5 +1,4 @@ """Support for LCN devices.""" -import asyncio import logging import pypck @@ -95,10 +94,7 @@ async def async_setup_entry(hass, config_entry): entity_registry.async_clear_config_entry(config_entry.entry_id) # forward config_entry to components - for component in PLATFORMS: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(config_entry, component) - ) + hass.config_entries.async_setup_platforms(config_entry, PLATFORMS) # register service calls for service_name, service in SERVICES: @@ -113,13 +109,8 @@ async def async_setup_entry(hass, config_entry): async def async_unload_entry(hass, config_entry): """Close connection to PCHK host represented by config_entry.""" # forward unloading to platforms - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(config_entry, component) - for component in PLATFORMS - ] - ) + unload_ok = await hass.config_entries.async_unload_platforms( + config_entry, PLATFORMS ) if unload_ok and config_entry.entry_id in hass.data[DOMAIN]: diff --git a/homeassistant/components/lifx/__init__.py b/homeassistant/components/lifx/__init__.py index 6e921a59afe..b0b67450b5e 100644 --- a/homeassistant/components/lifx/__init__.py +++ b/homeassistant/components/lifx/__init__.py @@ -26,6 +26,8 @@ CONFIG_SCHEMA = vol.Schema( DATA_LIFX_MANAGER = "lifx_manager" +PLATFORMS = [LIGHT_DOMAIN] + async def async_setup(hass, config): """Set up the LIFX component.""" @@ -45,17 +47,11 @@ async def async_setup(hass, config): async def async_setup_entry(hass, entry): """Set up LIFX from a config entry.""" - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, LIGHT_DOMAIN) - ) - + hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True async def async_unload_entry(hass, entry): """Unload a config entry.""" hass.data.pop(DATA_LIFX_MANAGER).cleanup() - - await hass.config_entries.async_forward_entry_unload(entry, LIGHT_DOMAIN) - - return True + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/litejet/__init__.py b/homeassistant/components/litejet/__init__.py index f00853af524..b69df5ffd31 100644 --- a/homeassistant/components/litejet/__init__.py +++ b/homeassistant/components/litejet/__init__.py @@ -1,5 +1,4 @@ """Support for the LiteJet lighting system.""" -import asyncio import logging import pylitejet @@ -59,25 +58,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data[DOMAIN] = system - 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 LiteJet 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].close() diff --git a/homeassistant/components/litterrobot/__init__.py b/homeassistant/components/litterrobot/__init__.py index 83bf9f785a2..424a6a92aba 100644 --- a/homeassistant/components/litterrobot/__init__.py +++ b/homeassistant/components/litterrobot/__init__.py @@ -1,5 +1,4 @@ """The Litter-Robot integration.""" -import asyncio from pylitterbot.exceptions import LitterRobotException, LitterRobotLoginException @@ -25,24 +24,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): raise ConfigEntryNotReady from ex if hub.account.robots: - 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/local_ip/__init__.py b/homeassistant/components/local_ip/__init__.py index 637520aa30c..1e8376b6b6f 100644 --- a/homeassistant/components/local_ip/__init__.py +++ b/homeassistant/components/local_ip/__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 DOMAIN, PLATFORM +from .const import DOMAIN, PLATFORMS CONFIG_SCHEMA = vol.Schema( { @@ -34,13 +34,10 @@ async def async_setup(hass: HomeAssistant, config: dict): async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up local_ip 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: 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/local_ip/const.py b/homeassistant/components/local_ip/const.py index e18246a9730..0bac6d874d1 100644 --- a/homeassistant/components/local_ip/const.py +++ b/homeassistant/components/local_ip/const.py @@ -1,6 +1,6 @@ """Local IP constants.""" DOMAIN = "local_ip" -PLATFORM = "sensor" +PLATFORMS = ["sensor"] SENSOR = "address" diff --git a/homeassistant/components/locative/__init__.py b/homeassistant/components/locative/__init__.py index bb2a19c6380..97df92a9f89 100644 --- a/homeassistant/components/locative/__init__.py +++ b/homeassistant/components/locative/__init__.py @@ -25,6 +25,7 @@ _LOGGER = logging.getLogger(__name__) DOMAIN = "locative" TRACKER_UPDATE = f"{DOMAIN}_tracker_update" +PLATFORMS = [DEVICE_TRACKER] ATTR_DEVICE_ID = "device" ATTR_TRIGGER = "trigger" @@ -116,9 +117,7 @@ async def async_setup_entry(hass, entry): DOMAIN, "Locative", entry.data[CONF_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) return True @@ -126,7 +125,7 @@ async def async_unload_entry(hass, entry): """Unload a config entry.""" hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID]) hass.data[DOMAIN]["unsub_device_tracker"].pop(entry.entry_id)() - return await hass.config_entries.async_forward_entry_unload(entry, DEVICE_TRACKER) + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) async_remove_entry = config_entry_flow.webhook_async_remove_entry diff --git a/homeassistant/components/logi_circle/__init__.py b/homeassistant/components/logi_circle/__init__.py index 1311e50f293..9e1a4803e11 100644 --- a/homeassistant/components/logi_circle/__init__.py +++ b/homeassistant/components/logi_circle/__init__.py @@ -179,10 +179,7 @@ async def async_setup_entry(hass, entry): hass.data[DATA_LOGI] = logi_circle - 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 service_handler(service): """Dispatch service calls to target entities.""" @@ -229,8 +226,7 @@ async def async_setup_entry(hass, entry): async def async_unload_entry(hass, entry): """Unload a config entry.""" - 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) logi_circle = hass.data.pop(DATA_LOGI) @@ -238,4 +234,4 @@ async def async_unload_entry(hass, entry): # and clear all locally cached tokens await logi_circle.auth_provider.clear_authorization() - return True + return unload_ok diff --git a/homeassistant/components/luftdaten/__init__.py b/homeassistant/components/luftdaten/__init__.py index ca1b9aed4ff..6db0ad96f64 100644 --- a/homeassistant/components/luftdaten/__init__.py +++ b/homeassistant/components/luftdaten/__init__.py @@ -33,6 +33,8 @@ DATA_LUFTDATEN_CLIENT = "data_luftdaten_client" DATA_LUFTDATEN_LISTENER = "data_luftdaten_listener" DEFAULT_ATTRIBUTION = "Data provided by luftdaten.info" +PLATFORMS = ["sensor"] + SENSOR_HUMIDITY = "humidity" SENSOR_PM10 = "P1" SENSOR_PM2_5 = "P2" @@ -152,9 +154,7 @@ async def async_setup_entry(hass, config_entry): except LuftdatenError as err: raise ConfigEntryNotReady from err - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(config_entry, "sensor") - ) + hass.config_entries.async_setup_platforms(config_entry, PLATFORMS) async def refresh_sensors(event_time): """Refresh Luftdaten data.""" @@ -181,7 +181,7 @@ async def async_unload_entry(hass, config_entry): hass.data[DOMAIN][DATA_LUFTDATEN_CLIENT].pop(config_entry.entry_id) - return await hass.config_entries.async_forward_entry_unload(config_entry, "sensor") + return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS) class LuftDatenData: diff --git a/homeassistant/components/lutron_caseta/__init__.py b/homeassistant/components/lutron_caseta/__init__.py index 89eef781c25..144a9a74c55 100644 --- a/homeassistant/components/lutron_caseta/__init__.py +++ b/homeassistant/components/lutron_caseta/__init__.py @@ -137,10 +137,7 @@ async def async_setup_entry(hass, config_entry): # pico remotes to control other devices. await async_setup_lip(hass, config_entry, bridge.lip_devices) - 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 @@ -283,15 +280,9 @@ async def async_unload_entry(hass, config_entry): if data[BRIDGE_LIP]: await data[BRIDGE_LIP].async_stop() - 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/lyric/__init__.py b/homeassistant/components/lyric/__init__.py index 7a6e00da7d2..9f6d38ad4e7 100644 --- a/homeassistant/components/lyric/__init__.py +++ b/homeassistant/components/lyric/__init__.py @@ -1,7 +1,6 @@ """The Honeywell Lyric integration.""" from __future__ import annotations -import asyncio from datetime import timedelta import logging from typing import Any @@ -117,24 +116,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # Fetch initial data so we have data when entities subscribe 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): """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/mazda/__init__.py b/homeassistant/components/mazda/__init__.py index c640dd2528f..f6e31fa4357 100644 --- a/homeassistant/components/mazda/__init__.py +++ b/homeassistant/components/mazda/__init__.py @@ -1,5 +1,4 @@ """The Mazda Connected Services integration.""" -import asyncio from datetime import timedelta import logging @@ -101,24 +100,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): await coordinator.async_config_entry_first_refresh() # Setup components - 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/melcloud/__init__.py b/homeassistant/components/melcloud/__init__.py index 528854308d6..fcff9ab3304 100644 --- a/homeassistant/components/melcloud/__init__.py +++ b/homeassistant/components/melcloud/__init__.py @@ -63,25 +63,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): conf = entry.data mel_devices = await mel_devices_setup(hass, conf[CONF_TOKEN]) hass.data.setdefault(DOMAIN, {}).update({entry.entry_id: mel_devices}) - 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, config_entry): """Unload a config entry.""" - 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 ) hass.data[DOMAIN].pop(config_entry.entry_id) if not hass.data[DOMAIN]: hass.data.pop(DOMAIN) - return True + return unload_ok class MelCloudDevice: diff --git a/homeassistant/components/met/__init__.py b/homeassistant/components/met/__init__.py index 1e1a203342e..dd932a75957 100644 --- a/homeassistant/components/met/__init__.py +++ b/homeassistant/components/met/__init__.py @@ -27,6 +27,7 @@ from .const import ( URL = "https://aa015h6buqvih86i1.api.met.no/weatherapi/locationforecast/2.0/complete" +PLATFORMS = ["weather"] _LOGGER = logging.getLogger(__name__) @@ -56,20 +57,21 @@ async def async_setup_entry(hass, config_entry): hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][config_entry.entry_id] = coordinator - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(config_entry, "weather") - ) + hass.config_entries.async_setup_platforms(config_entry, PLATFORMS) return True async def async_unload_entry(hass, config_entry): """Unload a config entry.""" - await hass.config_entries.async_forward_entry_unload(config_entry, "weather") + unload_ok = await hass.config_entries.async_unload_platforms( + config_entry, PLATFORMS + ) + hass.data[DOMAIN][config_entry.entry_id].untrack_home() hass.data[DOMAIN].pop(config_entry.entry_id) - return True + return unload_ok class MetDataUpdateCoordinator(DataUpdateCoordinator): diff --git a/homeassistant/components/met_eireann/__init__.py b/homeassistant/components/met_eireann/__init__.py index 365e4dbafb3..c70f436009d 100644 --- a/homeassistant/components/met_eireann/__init__.py +++ b/homeassistant/components/met_eireann/__init__.py @@ -15,6 +15,8 @@ _LOGGER = logging.getLogger(__name__) UPDATE_INTERVAL = timedelta(minutes=60) +PLATFORMS = ["weather"] + async def async_setup_entry(hass, config_entry): """Set up Met Éireann as config entry.""" @@ -47,19 +49,19 @@ async def async_setup_entry(hass, config_entry): hass.data[DOMAIN][config_entry.entry_id] = coordinator - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(config_entry, "weather") - ) + hass.config_entries.async_setup_platforms(config_entry, PLATFORMS) return True async def async_unload_entry(hass, config_entry): """Unload a config entry.""" - await hass.config_entries.async_forward_entry_unload(config_entry, "weather") + unload_ok = await hass.config_entries.async_unload_platforms( + config_entry, PLATFORMS + ) hass.data[DOMAIN].pop(config_entry.entry_id) - return True + return unload_ok class MetEireannWeatherData: diff --git a/homeassistant/components/meteo_france/__init__.py b/homeassistant/components/meteo_france/__init__.py index cdd55c06db7..4ec03e4f5a5 100644 --- a/homeassistant/components/meteo_france/__init__.py +++ b/homeassistant/components/meteo_france/__init__.py @@ -1,5 +1,4 @@ """Support for Meteo-France weather data.""" -import asyncio from datetime import timedelta import logging @@ -173,10 +172,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: 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 @@ -194,14 +190,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): department, ) - 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][UNDO_UPDATE_LISTENER]() hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/metoffice/__init__.py b/homeassistant/components/metoffice/__init__.py index 5dfeceb79f8..9bf9e44b72a 100644 --- a/homeassistant/components/metoffice/__init__.py +++ b/homeassistant/components/metoffice/__init__.py @@ -1,6 +1,5 @@ """The Met Office integration.""" -import asyncio import logging from homeassistant.config_entries import ConfigEntry @@ -56,24 +55,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): if metoffice_data.now is None: raise ConfigEntryNotReady() - 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 not hass.data[DOMAIN]: diff --git a/homeassistant/components/mikrotik/__init__.py b/homeassistant/components/mikrotik/__init__.py index 9a8ee7bdb45..cd96cba327c 100644 --- a/homeassistant/components/mikrotik/__init__.py +++ b/homeassistant/components/mikrotik/__init__.py @@ -21,6 +21,7 @@ from .const import ( DEFAULT_DETECTION_TIME, DEFAULT_NAME, DOMAIN, + PLATFORMS, ) from .hub import MikrotikHub @@ -42,6 +43,7 @@ MIKROTIK_SCHEMA = vol.All( ) ) + CONFIG_SCHEMA = vol.Schema( {DOMAIN: vol.All(cv.ensure_list, [MIKROTIK_SCHEMA])}, extra=vol.ALLOW_EXTRA ) @@ -84,8 +86,10 @@ async def async_setup_entry(hass, config_entry): async def async_unload_entry(hass, config_entry): """Unload a config entry.""" - await hass.config_entries.async_forward_entry_unload(config_entry, "device_tracker") + unload_ok = await hass.config_entries.async_unload_platforms( + config_entry, PLATFORMS + ) hass.data[DOMAIN].pop(config_entry.entry_id) - return True + return unload_ok diff --git a/homeassistant/components/mikrotik/const.py b/homeassistant/components/mikrotik/const.py index d81e8878d1c..1fbe0af5c1b 100644 --- a/homeassistant/components/mikrotik/const.py +++ b/homeassistant/components/mikrotik/const.py @@ -37,6 +37,8 @@ MIKROTIK_SERVICES = { IS_CAPSMAN: "/caps-man/interface/print", } +PLATFORMS = ["device_tracker"] + ATTR_DEVICE_TRACKER = [ "comment", "mac-address", diff --git a/homeassistant/components/mikrotik/hub.py b/homeassistant/components/mikrotik/hub.py index 2f1f89ba60d..63be0a4a358 100644 --- a/homeassistant/components/mikrotik/hub.py +++ b/homeassistant/components/mikrotik/hub.py @@ -31,6 +31,7 @@ from .const import ( IS_WIRELESS, MIKROTIK_SERVICES, NAME, + PLATFORMS, WIRELESS, ) from .errors import CannotConnect, LoginError @@ -385,11 +386,7 @@ class MikrotikHub: await self.hass.async_add_executor_job(self._mk_data.get_hub_details) await self.hass.async_add_executor_job(self._mk_data.update) - self.hass.async_create_task( - self.hass.config_entries.async_forward_entry_setup( - self.config_entry, "device_tracker" - ) - ) + self.hass.config_entries.async_setup_platforms(self.config_entry, PLATFORMS) return True diff --git a/homeassistant/components/mill/__init__.py b/homeassistant/components/mill/__init__.py index e58a7865e28..115bb5eb33c 100644 --- a/homeassistant/components/mill/__init__.py +++ b/homeassistant/components/mill/__init__.py @@ -1,17 +1,14 @@ """The mill component.""" +PLATFORMS = ["climate"] + async def async_setup_entry(hass, entry): """Set up the Mill heater.""" - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "climate") - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True -async def async_unload_entry(hass, config_entry): +async def async_unload_entry(hass, entry): """Unload a config entry.""" - unload_ok = await hass.config_entries.async_forward_entry_unload( - config_entry, "climate" - ) - return unload_ok + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/minecraft_server/__init__.py b/homeassistant/components/minecraft_server/__init__.py index e887f31ae0f..5d507006b05 100644 --- a/homeassistant/components/minecraft_server/__init__.py +++ b/homeassistant/components/minecraft_server/__init__.py @@ -1,7 +1,6 @@ """The Minecraft Server integration.""" from __future__ import annotations -import asyncio from datetime import datetime, timedelta import logging from typing import Any @@ -44,10 +43,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b server.start_periodic_update() # Set up platforms. - 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 @@ -58,18 +54,15 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> server = hass.data[DOMAIN][unique_id] # Unload platforms. - 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 ) # Clean up. server.stop_periodic_update() hass.data[DOMAIN].pop(unique_id) - return True + return unload_ok class MinecraftServer: diff --git a/homeassistant/components/mobile_app/__init__.py b/homeassistant/components/mobile_app/__init__.py index 1321818b91f..0fe1386d7ce 100644 --- a/homeassistant/components/mobile_app/__init__.py +++ b/homeassistant/components/mobile_app/__init__.py @@ -1,5 +1,4 @@ """Integrates Native Apps to Home Assistant.""" -import asyncio from contextlib import suppress from homeassistant.components import cloud, notify as hass_notify @@ -89,10 +88,7 @@ async def async_setup_entry(hass, entry): registration_name = f"Mobile App: {registration[ATTR_DEVICE_NAME]}" webhook_register(hass, DOMAIN, registration_name, webhook_id, handle_webhook) - for domain in PLATFORMS: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, domain) - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) await hass_notify.async_reload(hass, DOMAIN) @@ -101,14 +97,7 @@ async def async_setup_entry(hass, entry): async def async_unload_entry(hass, entry): """Unload a mobile app 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 not unload_ok: return False diff --git a/homeassistant/components/monoprice/__init__.py b/homeassistant/components/monoprice/__init__.py index 61aa8b408cf..f543220b5b9 100644 --- a/homeassistant/components/monoprice/__init__.py +++ b/homeassistant/components/monoprice/__init__.py @@ -1,5 +1,4 @@ """The Monoprice 6-Zone Amplifier integration.""" -import asyncio import logging from pymonoprice import get_monoprice @@ -49,25 +48,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): FIRST_RUN: first_run, } - 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][entry.entry_id][UNDO_UPDATE_LISTENER]() hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/motion_blinds/__init__.py b/homeassistant/components/motion_blinds/__init__.py index 73a27c90140..d2400beb4f5 100644 --- a/homeassistant/components/motion_blinds/__init__.py +++ b/homeassistant/components/motion_blinds/__init__.py @@ -1,5 +1,4 @@ """The motion_blinds component.""" -import asyncio from datetime import timedelta import logging from socket import timeout @@ -159,10 +158,7 @@ async def async_setup_entry( sw_version=motion_gateway.protocol, ) - 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 @@ -171,13 +167,8 @@ async def async_unload_entry( hass: core.HomeAssistant, config_entry: config_entries.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: diff --git a/homeassistant/components/motioneye/__init__.py b/homeassistant/components/motioneye/__init__.py index 5387de8225c..cb5e80b9c98 100644 --- a/homeassistant/components/motioneye/__init__.py +++ b/homeassistant/components/motioneye/__init__.py @@ -225,14 +225,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: config_data = hass.data[DOMAIN].pop(entry.entry_id) await config_data[CONF_CLIENT].async_client_close() diff --git a/homeassistant/components/mullvad/__init__.py b/homeassistant/components/mullvad/__init__.py index 325c0603f32..d89c947a4f3 100644 --- a/homeassistant/components/mullvad/__init__.py +++ b/homeassistant/components/mullvad/__init__.py @@ -1,5 +1,4 @@ """The Mullvad VPN integration.""" -import asyncio from datetime import timedelta import logging @@ -34,25 +33,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: dict): hass.data[DOMAIN] = 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: del hass.data[DOMAIN] diff --git a/homeassistant/components/myq/__init__.py b/homeassistant/components/myq/__init__.py index b25751d7270..fd3a46bbb5a 100644 --- a/homeassistant/components/myq/__init__.py +++ b/homeassistant/components/myq/__init__.py @@ -1,5 +1,4 @@ """The MyQ integration.""" -import asyncio from datetime import timedelta import logging @@ -18,17 +17,10 @@ from .const import DOMAIN, MYQ_COORDINATOR, MYQ_GATEWAY, PLATFORMS, UPDATE_INTER _LOGGER = logging.getLogger(__name__) -async def async_setup(hass: HomeAssistant, config: dict): - """Set up the MyQ component.""" - - hass.data.setdefault(DOMAIN, {}) - - return True - - async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up MyQ from a config entry.""" + hass.data.setdefault(DOMAIN, {}) websession = aiohttp_client.async_get_clientsession(hass) conf = entry.data @@ -58,24 +50,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): hass.data[DOMAIN][entry.entry_id] = {MYQ_GATEWAY: myq, MYQ_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/mysensors/__init__.py b/homeassistant/components/mysensors/__init__.py index c5ed31326a3..812e6bf1670 100644 --- a/homeassistant/components/mysensors/__init__.py +++ b/homeassistant/components/mysensors/__init__.py @@ -239,13 +239,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: gateway = get_mysensors_gateway(hass, entry.entry_id) - unload_ok = all( - await asyncio.gather( - *[ - hass.config_entries.async_forward_entry_unload(entry, platform) - for platform in PLATFORMS_WITH_ENTRY_SUPPORT - ] - ) + unload_ok = await hass.config_entries.async_unload_platforms( + entry, PLATFORMS_WITH_ENTRY_SUPPORT ) if not unload_ok: return False diff --git a/tests/components/kostal_plenticore/test_config_flow.py b/tests/components/kostal_plenticore/test_config_flow.py index 04a69892b43..7ce95f71e8e 100644 --- a/tests/components/kostal_plenticore/test_config_flow.py +++ b/tests/components/kostal_plenticore/test_config_flow.py @@ -23,8 +23,6 @@ async def test_formx(hass): with patch( "homeassistant.components.kostal_plenticore.config_flow.PlenticoreApiClient" ) as mock_api_class, patch( - "homeassistant.components.kostal_plenticore.async_setup", return_value=True - ) as mock_setup, patch( "homeassistant.components.kostal_plenticore.async_setup_entry", return_value=True, ) as mock_setup_entry: @@ -63,7 +61,6 @@ async def test_formx(hass): "password": "test-password", } await hass.async_block_till_done() - assert len(mock_setup.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1 diff --git a/tests/components/myq/test_config_flow.py b/tests/components/myq/test_config_flow.py index 683b6beab8a..3ae2da82f46 100644 --- a/tests/components/myq/test_config_flow.py +++ b/tests/components/myq/test_config_flow.py @@ -23,8 +23,6 @@ async def test_form_user(hass): "homeassistant.components.myq.config_flow.pymyq.login", return_value=True, ), patch( - "homeassistant.components.myq.async_setup", return_value=True - ) as mock_setup, patch( "homeassistant.components.myq.async_setup_entry", return_value=True, ) as mock_setup_entry: @@ -40,7 +38,6 @@ async def test_form_user(hass): "username": "test-username", "password": "test-password", } - assert len(mock_setup.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1