Reduce boilerplate to setup config entry platforms A-C (#49681)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
J. Nick Koston 2021-04-26 07:46:55 -10:00 committed by GitHub
parent 922eec0909
commit 51be2f860a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 119 additions and 409 deletions

View File

@ -1,5 +1,4 @@
"""Support for the Abode Security System."""
from asyncio import gather
from copy import deepcopy
from functools import partial
@ -131,10 +130,7 @@ async def async_setup_entry(hass, config_entry):
hass.data[DOMAIN] = AbodeSystem(abode, polling)
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)
await setup_hass_events(hass)
await hass.async_add_executor_job(setup_hass_services, hass)
@ -149,14 +145,9 @@ async def async_unload_entry(hass, config_entry):
hass.services.async_remove(DOMAIN, SERVICE_CAPTURE_IMAGE)
hass.services.async_remove(DOMAIN, SERVICE_TRIGGER_AUTOMATION)
tasks = []
for platform in PLATFORMS:
tasks.append(
hass.config_entries.async_forward_entry_unload(config_entry, platform)
)
await gather(*tasks)
unload_ok = await hass.config_entries.async_unload_platforms(
config_entry, PLATFORMS
)
await hass.async_add_executor_job(hass.data[DOMAIN].abode.events.stop)
await hass.async_add_executor_job(hass.data[DOMAIN].abode.logout)
@ -164,7 +155,7 @@ async def async_unload_entry(hass, config_entry):
hass.data[DOMAIN].logout_listener()
hass.data.pop(DOMAIN)
return True
return unload_ok
def setup_hass_services(hass):

View File

@ -1,5 +1,4 @@
"""The AccuWeather component."""
import asyncio
from datetime import timedelta
import logging
@ -46,23 +45,15 @@ async def async_setup_entry(hass, config_entry) -> bool:
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
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
)
hass.data[DOMAIN][config_entry.entry_id][UNDO_UPDATE_LISTENER]()

View File

@ -1,5 +1,4 @@
"""The Rollease Acmeda Automate integration."""
import asyncio
from homeassistant import config_entries, core
@ -23,10 +22,7 @@ async def async_setup_entry(
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][config_entry.entry_id] = hub
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
@ -37,14 +33,10 @@ async def async_unload_entry(
"""Unload a config entry."""
hub = hass.data[DOMAIN][config_entry.entry_id]
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 not await hub.async_reset():
return False

View File

@ -1,6 +1,5 @@
"""Advantage Air climate integration."""
import asyncio
from datetime import timedelta
import logging
@ -58,24 +57,14 @@ async def async_setup_entry(hass, entry):
"async_change": async_change,
}
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 Advantage Air Config."""
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)

View File

@ -1,5 +1,4 @@
"""The AEMET OpenData component."""
import asyncio
import logging
from aemet_opendata.interface import AEMET
@ -32,24 +31,17 @@ 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)
return True
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.entry_id)

View File

@ -1,5 +1,4 @@
"""Support for Agent."""
import asyncio
from agent import AgentError
from agent.a import Agent
@ -47,24 +46,14 @@ async def async_setup_entry(hass, config_entry):
sw_version=agent_client.version,
)
for forward in FORWARDS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, forward)
)
hass.config_entries.async_setup_platforms(config_entry, FORWARDS)
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, forward)
for forward in FORWARDS
]
)
)
unload_ok = await hass.config_entries.async_unload_platforms(config_entry, FORWARDS)
await hass.data[AGENT_DOMAIN][config_entry.entry_id][CONNECTION].close()

View File

@ -1,5 +1,4 @@
"""The Airly integration."""
import asyncio
from datetime import timedelta
import logging
from math import ceil
@ -69,24 +68,17 @@ async def async_setup_entry(hass, config_entry):
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][config_entry.entry_id] = 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)
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)

View File

@ -1,5 +1,4 @@
"""The AirNow integration."""
import asyncio
import datetime
import logging
@ -60,24 +59,15 @@ 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)

View File

@ -1,5 +1,4 @@
"""The airvisual component."""
import asyncio
from datetime import timedelta
from math import ceil
@ -258,10 +257,7 @@ async def async_setup_entry(hass, config_entry):
hass, config_entry.data[CONF_API_KEY]
)
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
@ -310,14 +306,10 @@ async def async_migrate_entry(hass, config_entry):
async def async_unload_entry(hass, config_entry):
"""Unload an AirVisual 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_COORDINATOR].pop(config_entry.entry_id)
remove_listener = hass.data[DOMAIN][DATA_LISTENER].pop(config_entry.entry_id)

View File

@ -1,5 +1,4 @@
"""Support for AlarmDecoder devices."""
import asyncio
from datetime import timedelta
import logging
@ -125,10 +124,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await open_connection()
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
@ -136,14 +133,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a AlarmDecoder entry."""
hass.data[DOMAIN][entry.entry_id][DATA_RESTART] = False
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

View File

@ -1,5 +1,4 @@
"""Support for Ambient Weather Station Service."""
import asyncio
from aioambient import Client
from aioambient.errors import WebsocketError
@ -369,14 +368,7 @@ async def async_unload_entry(hass, config_entry):
ambient = hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id)
hass.async_create_task(ambient.ws_disconnect())
tasks = [
hass.config_entries.async_forward_entry_unload(config_entry, platform)
for platform in PLATFORMS
]
await asyncio.gather(*tasks)
return True
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
async def async_migrate_entry(hass, config_entry):
@ -475,12 +467,9 @@ class AmbientStation:
# attempt forward setup of the config entry (because it will have
# already been done):
if not self._entry_setup_complete:
for platform in PLATFORMS:
self._hass.async_create_task(
self._hass.config_entries.async_forward_entry_setup(
self._config_entry, platform
)
)
self._hass.config_entries.async_setup_platforms(
self._config_entry, PLATFORMS
)
self._entry_setup_complete = True
self._ws_reconnect_delay = DEFAULT_SOCKET_MIN_RETRY

View File

@ -71,14 +71,8 @@ async def async_setup_entry(hass, entry):
async def async_unload_entry(hass, entry):
"""Unload an Apple TV 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:
manager = hass.data[DOMAIN].pop(entry.unique_id)
await manager.disconnect()

View File

@ -1,5 +1,4 @@
"""Support for ASUSWRT devices."""
import asyncio
import voluptuous as vol
@ -125,10 +124,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
router.async_on_close(entry.add_update_listener(update_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 def async_close_connection(event):
"""Close AsusWrt connection on HA Stop."""
@ -148,14 +144,8 @@ 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)
if unload_ok:
hass.data[DOMAIN][entry.entry_id]["stop_listener"]()
router = hass.data[DOMAIN][entry.entry_id][DATA_ASUSWRT]

View File

@ -9,7 +9,7 @@ from homeassistant.components.climate import DOMAIN as CLIMATE
from homeassistant.components.sensor import DOMAIN as SENSOR
from homeassistant.components.water_heater import DOMAIN as WATER_HEATER
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, asyncio
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
@ -52,24 +52,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
if entry.unique_id is None:
hass.config_entries.async_update_entry(entry, unique_id=atag.id)
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 Atag 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

View File

@ -52,14 +52,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data[DOMAIN][entry.entry_id][DATA_AUGUST].async_stop()
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)
@ -85,10 +78,7 @@ async def async_setup_august(hass, config_entry, august_gateway):
}
await data[DATA_AUGUST].async_setup()
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

View File

@ -1,6 +1,5 @@
"""The aurora component."""
import asyncio
from datetime import timedelta
import logging
@ -69,24 +68,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
AURORA_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)

View File

@ -28,23 +28,17 @@ async def async_setup_entry(hass, config_entry) -> bool:
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][config_entry.entry_id] = 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)
return True
async def async_unload_entry(hass, config_entry) -> bool:
"""Unload Awair configuration."""
tasks = []
for platform in PLATFORMS:
tasks.append(
hass.config_entries.async_forward_entry_unload(config_entry, platform)
)
unload_ok = await hass.config_entries.async_unload_platforms(
config_entry, PLATFORMS
)
unload_ok = all(await gather(*tasks))
if unload_ok:
hass.data[DOMAIN].pop(config_entry.entry_id)

View File

@ -264,20 +264,9 @@ class AxisNetworkDevice:
"""Reset this device to default state."""
self.disconnect_from_stream()
unload_ok = all(
await asyncio.gather(
*[
self.hass.config_entries.async_forward_entry_unload(
self.config_entry, platform
)
for platform in PLATFORMS
]
)
return await self.hass.config_entries.async_unload_platforms(
self.config_entry, PLATFORMS
)
if not unload_ok:
return False
return True
async def get_device(hass, host, port, username, password):

View File

@ -1,5 +1,4 @@
"""The BleBox devices integration."""
import asyncio
import logging
from blebox_uniapi.error import Error
@ -43,24 +42,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
domain_entry = domain.setdefault(entry.entry_id, {})
product = domain_entry.setdefault(PRODUCT, product)
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)

View File

@ -1,5 +1,4 @@
"""Support for Blink Home Camera System."""
import asyncio
from copy import deepcopy
import logging
@ -86,10 +85,7 @@ async def async_setup_entry(hass, entry):
if not hass.data[DOMAIN][entry.entry_id].available:
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)
def blink_refresh(event_time=None):
"""Call blink to refresh info."""
@ -130,14 +126,7 @@ def _async_import_options_from_data_if_missing(hass, entry):
async def async_unload_entry(hass, entry):
"""Unload Blink 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

View File

@ -1,7 +1,6 @@
"""Reads vehicle status from BMW connected drive portal."""
from __future__ import annotations
import asyncio
import logging
from bimmer_connected.account import ConnectedDriveAccount
@ -138,11 +137,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
await _async_update_all()
for platform in PLATFORMS:
if platform != NOTIFY_DOMAIN:
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 platform != NOTIFY_DOMAIN]
)
# set up notify platform, no entry support for notify platform yet,
# have to use discovery to load platform.
@ -161,14 +158,8 @@ 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
if platform != NOTIFY_DOMAIN
]
)
unload_ok = await hass.config_entries.async_unload_platforms(
entry, [platform for platform in PLATFORMS if platform != NOTIFY_DOMAIN]
)
# Only remove services if it is the last account and not read only

View File

@ -1,5 +1,4 @@
"""The Bond integration."""
import asyncio
from asyncio import TimeoutError as AsyncIOTimeoutError
from aiohttp import ClientError, ClientTimeout
@ -75,24 +74,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_async_remove_old_device_identifiers(config_entry_id, device_registry, 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: 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)
data = hass.data[DOMAIN][entry.entry_id]
data[_STOP_CANCEL]()

View File

@ -1,5 +1,4 @@
"""The Bravia TV component."""
import asyncio
from bravia_tv import BraviaRC
@ -23,23 +22,15 @@ async def async_setup_entry(hass, config_entry):
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
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
)
hass.data[DOMAIN][config_entry.entry_id][UNDO_UPDATE_LISTENER]()

View File

@ -1,5 +1,4 @@
"""Support for Broadlink devices."""
import asyncio
from contextlib import suppress
from functools import partial
import logging
@ -112,12 +111,9 @@ class BroadlinkDevice:
self.reset_jobs.append(config.add_update_listener(self.async_update))
# Forward entry setup to related domains.
tasks = (
self.hass.config_entries.async_forward_entry_setup(config, domain)
for domain in get_domains(self.api.type)
self.hass.config_entries.async_setup_platforms(
config, get_domains(self.api.type)
)
for entry_setup in tasks:
self.hass.async_create_task(entry_setup)
return True
@ -129,12 +125,9 @@ class BroadlinkDevice:
while self.reset_jobs:
self.reset_jobs.pop()()
tasks = (
self.hass.config_entries.async_forward_entry_unload(self.config, domain)
for domain in get_domains(self.api.type)
return await self.hass.config_entries.async_unload_platforms(
self.config, get_domains(self.api.type)
)
results = await asyncio.gather(*tasks)
return all(results)
async def async_auth(self):
"""Authenticate to the device."""

View File

@ -1,5 +1,4 @@
"""The Brother component."""
import asyncio
from datetime import timedelta
import logging
@ -37,24 +36,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id] = coordinator
hass.data[DOMAIN][SNMP] = snmp_engine
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][DATA_CONFIG_ENTRY].pop(entry.entry_id)
if not hass.data[DOMAIN][DATA_CONFIG_ENTRY]:

View File

@ -1,5 +1,4 @@
"""Support for Canary devices."""
import asyncio
from datetime import timedelta
import logging
@ -104,24 +103,14 @@ 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)
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][entry.entry_id][DATA_UNDO_UPDATE_LISTENER]()

View File

@ -1,7 +1,6 @@
"""The ClimaCell integration."""
from __future__ import annotations
import asyncio
from datetime import timedelta
import logging
from math import ceil
@ -162,23 +161,15 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
hass.data[DOMAIN][config_entry.entry_id] = 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)
return True
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""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
)
hass.data[DOMAIN].pop(config_entry.entry_id)

View File

@ -1,5 +1,4 @@
"""The Control4 integration."""
import asyncio
import json
import logging
@ -107,10 +106,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
entry_data[CONF_CONFIG_LISTENER] = entry.add_update_listener(update_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
@ -123,14 +119,8 @@ async def update_listener(hass, 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][CONF_CONFIG_LISTENER]()
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)

View File

@ -1,5 +1,4 @@
"""The Coronavirus integration."""
import asyncio
from datetime import timedelta
import logging
@ -48,24 +47,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if not coordinator.last_update_success:
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 config entry."""
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_coordinator(

View File

@ -2,7 +2,7 @@
from __future__ import annotations
import asyncio
from collections.abc import Mapping
from collections.abc import Iterable, Mapping
from contextvars import ContextVar
import functools
import logging
@ -999,6 +999,14 @@ class ConfigEntries:
return True
@callback
def async_setup_platforms(
self, entry: ConfigEntry, platforms: Iterable[str]
) -> None:
"""Forward the setup of an entry to platforms."""
for platform in platforms:
self.hass.async_create_task(self.async_forward_entry_setup(entry, platform))
async def async_forward_entry_setup(self, entry: ConfigEntry, domain: str) -> bool:
"""Forward the setup of an entry to a different component.
@ -1021,6 +1029,19 @@ class ConfigEntries:
await entry.async_setup(self.hass, integration=integration)
return True
async def async_unload_platforms(
self, entry: ConfigEntry, platforms: Iterable[str]
) -> bool:
"""Forward the unloading of an entry to platforms."""
return all(
await asyncio.gather(
*[
self.async_forward_entry_unload(entry, platform)
for platform in platforms
]
)
)
async def async_forward_entry_unload(self, entry: ConfigEntry, domain: str) -> bool:
"""Forward the unloading of an entry to a different component."""
# It was never loaded.

View File

@ -1,8 +1,6 @@
"""The NEW_NAME integration."""
from __future__ import annotations
import asyncio
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
@ -18,24 +16,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# TODO Store an API object for your platforms to access
# hass.data[DOMAIN][entry.entry_id] = MyApi(...)
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)

View File

@ -1,8 +1,6 @@
"""The NEW_NAME integration."""
from __future__ import annotations
import asyncio
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
@ -18,24 +16,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# TODO Store an API object for your platforms to access
# hass.data[DOMAIN][entry.entry_id] = MyApi(...)
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)

View File

@ -1,7 +1,6 @@
"""The NEW_NAME integration."""
from __future__ import annotations
import asyncio
from typing import Any
import voluptuous as vol
@ -75,24 +74,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
aiohttp_client.async_get_clientsession(hass), session
)
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)

View File

@ -257,14 +257,12 @@ async def test_remove_entry(hass, manager):
async def mock_setup_entry(hass, entry):
"""Mock setting up entry."""
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "light")
)
hass.config_entries.async_setup_platforms(entry, ["light"])
return True
async def mock_unload_entry(hass, entry):
"""Mock unloading an entry."""
result = await hass.config_entries.async_forward_entry_unload(entry, "light")
result = await hass.config_entries.async_unload_platforms(entry, ["light"])
assert result
return result