Reduce config entry setup/unload boilerplate V-Z (#49789)

This commit is contained in:
J. Nick Koston 2021-04-27 10:51:11 -10:00 committed by GitHub
parent a57761103c
commit f9a2c1cfd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 34 additions and 184 deletions

View File

@ -1,5 +1,4 @@
"""Support for Velbus devices.""" """Support for Velbus devices."""
import asyncio
import logging import logging
import velbus import velbus
@ -111,17 +110,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Remove the velbus connection.""" """Remove the velbus connection."""
await asyncio.wait( unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
)
hass.data[DOMAIN][entry.entry_id]["cntrl"].stop() hass.data[DOMAIN][entry.entry_id]["cntrl"].stop()
hass.data[DOMAIN].pop(entry.entry_id) hass.data[DOMAIN].pop(entry.entry_id)
if not hass.data[DOMAIN]: if not hass.data[DOMAIN]:
hass.data.pop(DOMAIN) hass.data.pop(DOMAIN)
return True return unload_ok
class VelbusEntity(Entity): class VelbusEntity(Entity):

View File

@ -1,7 +1,6 @@
"""Support for Verisure devices.""" """Support for Verisure devices."""
from __future__ import annotations from __future__ import annotations
import asyncio
from contextlib import suppress from contextlib import suppress
import os import os
from typing import Any from typing import Any
@ -137,25 +136,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN][entry.entry_id] = coordinator hass.data[DOMAIN][entry.entry_id] = coordinator
# Set up all platforms for this device/entry. # Set up all platforms for this device/entry.
for platform in PLATFORMS: hass.config_entries.async_setup_platforms(entry, PLATFORMS)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload Verisure config entry.""" """Unload Verisure config entry."""
unload_ok = all( unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
await asyncio.gather(
*(
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
)
)
)
if not unload_ok: if not unload_ok:
return False return False

View File

@ -1,5 +1,4 @@
"""VeSync integration.""" """VeSync integration."""
import asyncio
import logging import logging
from pyvesync import VeSync from pyvesync import VeSync
@ -153,14 +152,7 @@ async def async_setup_entry(hass, config_entry):
async def async_unload_entry(hass, entry): async def async_unload_entry(hass, entry):
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = all( unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
)
)
if unload_ok: if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id) hass.data[DOMAIN].pop(entry.entry_id)

View File

@ -1,5 +1,4 @@
"""The Vilfo Router integration.""" """The Vilfo Router integration."""
import asyncio
from datetime import timedelta from datetime import timedelta
import logging import logging
@ -36,24 +35,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = vilfo_router hass.data[DOMAIN][entry.entry_id] = vilfo_router
for platform in PLATFORMS: hass.config_entries.async_setup_platforms(entry, PLATFORMS)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = all( unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
)
)
if unload_ok: if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id) hass.data[DOMAIN].pop(entry.entry_id)

View File

@ -1,7 +1,6 @@
"""The vizio component.""" """The vizio component."""
from __future__ import annotations from __future__ import annotations
import asyncio
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import Any from typing import Any
@ -69,25 +68,16 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
await coordinator.async_refresh() await coordinator.async_refresh()
hass.data[DOMAIN][CONF_APPS] = coordinator hass.data[DOMAIN][CONF_APPS] = coordinator
for platform in PLATFORMS: hass.config_entries.async_setup_platforms(config_entry, PLATFORMS)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, platform)
)
return True return True
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = all( unload_ok = await hass.config_entries.async_unload_platforms(
await asyncio.gather( config_entry, PLATFORMS
*[
hass.config_entries.async_forward_entry_unload(config_entry, platform)
for platform in PLATFORMS
]
)
) )
# Exclude this config entry because its not unloaded yet # Exclude this config entry because its not unloaded yet
if not any( if not any(
entry.state == ENTRY_STATE_LOADED entry.state == ENTRY_STATE_LOADED

View File

@ -1,5 +1,4 @@
"""The Volumio integration.""" """The Volumio integration."""
import asyncio
from pyvolumio import CannotConnectError, Volumio from pyvolumio import CannotConnectError, Volumio
@ -30,24 +29,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
DATA_INFO: info, DATA_INFO: info,
} }
for platform in PLATFORMS: hass.config_entries.async_setup_platforms(entry, PLATFORMS)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = all( unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
)
)
if unload_ok: if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id) hass.data[DOMAIN].pop(entry.entry_id)

View File

@ -1,5 +1,4 @@
"""The waze_travel_time component.""" """The waze_travel_time component."""
import asyncio
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -9,21 +8,10 @@ PLATFORMS = ["sensor"]
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Load the saved entities.""" """Load the saved entities."""
for platform in PLATFORMS: hass.config_entries.async_setup_platforms(config_entry, PLATFORMS)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, platform)
)
return True return True
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
return all( return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(config_entry, platform)
for platform in PLATFORMS
]
)
)

View File

@ -1,5 +1,4 @@
"""Component for wiffi support.""" """Component for wiffi support."""
import asyncio
from datetime import timedelta from datetime import timedelta
import errno import errno
import logging import logging
@ -54,10 +53,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
_LOGGER.error("Port %s already in use", config_entry.data[CONF_PORT]) _LOGGER.error("Port %s already in use", config_entry.data[CONF_PORT])
raise ConfigEntryNotReady from exc raise ConfigEntryNotReady from exc
for platform in PLATFORMS: hass.config_entries.async_setup_platforms(config_entry, PLATFORMS)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, platform)
)
return True return True
@ -72,13 +68,8 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry):
api: WiffiIntegrationApi = hass.data[DOMAIN][config_entry.entry_id] api: WiffiIntegrationApi = hass.data[DOMAIN][config_entry.entry_id]
await api.server.close_server() await api.server.close_server()
unload_ok = all( unload_ok = await hass.config_entries.async_unload_platforms(
await asyncio.gather( config_entry, PLATFORMS
*[
hass.config_entries.async_forward_entry_unload(config_entry, platform)
for platform in PLATFORMS
]
)
) )
if unload_ok: if unload_ok:
api = hass.data[DOMAIN].pop(config_entry.entry_id) api = hass.data[DOMAIN].pop(config_entry.entry_id)

View File

@ -1,5 +1,4 @@
"""The WiLight integration.""" """The WiLight integration."""
import asyncio
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -26,10 +25,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data[DOMAIN][entry.entry_id] = parent hass.data[DOMAIN][entry.entry_id] = parent
# Set up all platforms for this device/entry. # Set up all platforms for this device/entry.
for platform in PLATFORMS: hass.config_entries.async_setup_platforms(entry, PLATFORMS)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
return True return True
@ -38,19 +34,14 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload WiLight config entry.""" """Unload WiLight config entry."""
# Unload entities for this entry/device. # Unload entities for this entry/device.
await asyncio.gather( unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
*(
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
)
)
# Cleanup # Cleanup
parent = hass.data[DOMAIN][entry.entry_id] parent = hass.data[DOMAIN][entry.entry_id]
await parent.async_reset() await parent.async_reset()
del hass.data[DOMAIN][entry.entry_id] del hass.data[DOMAIN][entry.entry_id]
return True return unload_ok
class WiLightDevice(Entity): class WiLightDevice(Entity):

View File

@ -1,7 +1,6 @@
"""Support for WLED.""" """Support for WLED."""
from __future__ import annotations from __future__ import annotations
import asyncio
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import Any from typing import Any
@ -52,10 +51,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
) )
# Set up all platforms for this device/entry. # Set up all platforms for this device/entry.
for platform in PLATFORMS: hass.config_entries.async_setup_platforms(entry, PLATFORMS)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
return True return True
@ -64,15 +60,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload WLED config entry.""" """Unload WLED config entry."""
# Unload entities for this entry/device. # Unload entities for this entry/device.
unload_ok = all( unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
await asyncio.gather(
*(
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
)
)
)
if unload_ok: if unload_ok:
del hass.data[DOMAIN][entry.entry_id] del hass.data[DOMAIN][entry.entry_id]

View File

@ -23,11 +23,7 @@ from .const import (
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
PLATFORMS = ["sensor"]
async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the Wolf SmartSet Service component."""
hass.data[DOMAIN] = {}
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
@ -78,21 +74,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
await coordinator.async_refresh() await coordinator.async_refresh()
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = {} hass.data[DOMAIN][entry.entry_id] = {}
hass.data[DOMAIN][entry.entry_id][PARAMETERS] = parameters hass.data[DOMAIN][entry.entry_id][PARAMETERS] = parameters
hass.data[DOMAIN][entry.entry_id][COORDINATOR] = coordinator hass.data[DOMAIN][entry.entry_id][COORDINATOR] = coordinator
hass.data[DOMAIN][entry.entry_id][DEVICE_ID] = device_id hass.data[DOMAIN][entry.entry_id][DEVICE_ID] = device_id
hass.async_create_task( hass.config_entries.async_setup_platforms(entry, PLATFORMS)
hass.config_entries.async_forward_entry_setup(entry, "sensor")
)
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = await hass.config_entries.async_forward_entry_unload(entry, "sensor") unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok: if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id) hass.data[DOMAIN].pop(entry.entry_id)

View File

@ -1,7 +1,6 @@
"""The xbox integration.""" """The xbox integration."""
from __future__ import annotations from __future__ import annotations
import asyncio
from contextlib import suppress from contextlib import suppress
from dataclasses import dataclass from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
@ -102,24 +101,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"coordinator": coordinator, "coordinator": coordinator,
} }
for platform in PLATFORMS: hass.config_entries.async_setup_platforms(entry, PLATFORMS)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = all( unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
)
)
if unload_ok: if unload_ok:
# Unsub from coordinator updates # Unsub from coordinator updates
hass.data[DOMAIN][entry.entry_id]["sensor_unsub"]() hass.data[DOMAIN][entry.entry_id]["sensor_unsub"]()

View File

@ -1,5 +1,4 @@
"""Support for Xiaomi Gateways.""" """Support for Xiaomi Gateways."""
import asyncio
from datetime import timedelta from datetime import timedelta
import logging import logging
@ -188,10 +187,7 @@ async def async_setup_entry(
else: else:
platforms = GATEWAY_PLATFORMS_NO_KEY platforms = GATEWAY_PLATFORMS_NO_KEY
for platform in platforms: hass.config_entries.async_setup_platforms(entry, platforms)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
return True return True
@ -205,14 +201,7 @@ async def async_unload_entry(
else: else:
platforms = GATEWAY_PLATFORMS_NO_KEY platforms = GATEWAY_PLATFORMS_NO_KEY
unload_ok = all( unload_ok = await hass.config_entries.async_unload_platforms(entry, platforms)
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in platforms
]
)
)
if unload_ok: if unload_ok:
hass.data[DOMAIN][GATEWAYS_KEY].pop(entry.entry_id) hass.data[DOMAIN][GATEWAYS_KEY].pop(entry.entry_id)

View File

@ -198,11 +198,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await device.async_setup() await device.async_setup()
async def _load_platforms(): async def _load_platforms():
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
for platform in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
# Move options from data for imported entries # Move options from data for imported entries
# Initialize options with default values for other entries # Initialize options with default values for other entries
@ -244,15 +240,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = all( unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
)
)
if unload_ok: if unload_ok:
data = hass.data[DOMAIN][DATA_CONFIG_ENTRIES].pop(entry.entry_id) data = hass.data[DOMAIN][DATA_CONFIG_ENTRIES].pop(entry.entry_id)
remove_init_dispatcher = data.get(DATA_REMOVE_INIT_DISPATCHER) remove_init_dispatcher = data.get(DATA_REMOVE_INIT_DISPATCHER)

View File

@ -1,5 +1,4 @@
"""Zerproc lights integration.""" """Zerproc lights integration."""
import asyncio
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -25,10 +24,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
if DATA_ADDRESSES not in hass.data[DOMAIN]: if DATA_ADDRESSES not in hass.data[DOMAIN]:
hass.data[DOMAIN][DATA_ADDRESSES] = set() hass.data[DOMAIN][DATA_ADDRESSES] = set()
for platform in PLATFORMS: hass.config_entries.async_setup_platforms(entry, PLATFORMS)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
return True return True
@ -42,11 +38,4 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data.pop(DOMAIN, None) hass.data.pop(DOMAIN, None)
return all( return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
)
)