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."""
import asyncio
import logging
import velbus
@ -111,17 +110,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Remove the velbus connection."""
await asyncio.wait(
[
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]["cntrl"].stop()
hass.data[DOMAIN].pop(entry.entry_id)
if not hass.data[DOMAIN]:
hass.data.pop(DOMAIN)
return True
return unload_ok
class VelbusEntity(Entity):

View File

@ -1,7 +1,6 @@
"""Support for Verisure devices."""
from __future__ import annotations
import asyncio
from contextlib import suppress
import os
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
# Set up all platforms for this device/entry.
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 Verisure 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 not unload_ok:
return False

View File

@ -1,5 +1,4 @@
"""VeSync integration."""
import asyncio
import logging
from pyvesync import VeSync
@ -153,14 +152,7 @@ async def async_setup_entry(hass, config_entry):
async def async_unload_entry(hass, entry):
"""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 Vilfo Router integration."""
import asyncio
from datetime import timedelta
import logging
@ -36,24 +35,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = vilfo_router
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,7 +1,6 @@
"""The vizio component."""
from __future__ import annotations
import asyncio
from datetime import timedelta
import logging
from typing import Any
@ -69,25 +68,16 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
await coordinator.async_refresh()
hass.data[DOMAIN][CONF_APPS] = 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
)
# Exclude this config entry because its not unloaded yet
if not any(
entry.state == ENTRY_STATE_LOADED

View File

@ -1,5 +1,4 @@
"""The Volumio integration."""
import asyncio
from pyvolumio import CannotConnectError, Volumio
@ -30,24 +29,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
DATA_INFO: info,
}
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 waze_travel_time component."""
import asyncio
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
@ -9,21 +8,10 @@ PLATFORMS = ["sensor"]
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Load the saved entities."""
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."""
return all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(config_entry, platform)
for platform in PLATFORMS
]
)
)
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)

View File

@ -1,5 +1,4 @@
"""Component for wiffi support."""
import asyncio
from datetime import timedelta
import errno
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])
raise ConfigEntryNotReady from exc
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
@ -72,13 +68,8 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry):
api: WiffiIntegrationApi = hass.data[DOMAIN][config_entry.entry_id]
await api.server.close_server()
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:
api = hass.data[DOMAIN].pop(config_entry.entry_id)

View File

@ -1,5 +1,4 @@
"""The WiLight integration."""
import asyncio
from homeassistant.config_entries import ConfigEntry
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
# Set up all platforms for this device/entry.
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
@ -38,19 +34,14 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload WiLight config entry."""
# Unload entities for this entry/device.
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)
# Cleanup
parent = hass.data[DOMAIN][entry.entry_id]
await parent.async_reset()
del hass.data[DOMAIN][entry.entry_id]
return True
return unload_ok
class WiLightDevice(Entity):

View File

@ -1,7 +1,6 @@
"""Support for WLED."""
from __future__ import annotations
import asyncio
from datetime import timedelta
import logging
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.
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
@ -64,15 +60,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload WLED config entry."""
# Unload entities for this entry/device.
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][entry.entry_id]

View File

@ -23,11 +23,7 @@ from .const import (
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the Wolf SmartSet Service component."""
hass.data[DOMAIN] = {}
return True
PLATFORMS = ["sensor"]
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()
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = {}
hass.data[DOMAIN][entry.entry_id][PARAMETERS] = parameters
hass.data[DOMAIN][entry.entry_id][COORDINATOR] = coordinator
hass.data[DOMAIN][entry.entry_id][DEVICE_ID] = device_id
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "sensor")
)
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 = await hass.config_entries.async_forward_entry_unload(entry, "sensor")
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 xbox integration."""
from __future__ import annotations
import asyncio
from contextlib import suppress
from dataclasses import dataclass
from datetime import timedelta
@ -102,24 +101,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"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:
# Unsub from coordinator updates
hass.data[DOMAIN][entry.entry_id]["sensor_unsub"]()

View File

@ -1,5 +1,4 @@
"""Support for Xiaomi Gateways."""
import asyncio
from datetime import timedelta
import logging
@ -188,10 +187,7 @@ async def async_setup_entry(
else:
platforms = GATEWAY_PLATFORMS_NO_KEY
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
@ -205,14 +201,7 @@ async def async_unload_entry(
else:
platforms = GATEWAY_PLATFORMS_NO_KEY
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][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()
async def _load_platforms():
for platform in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
# Move options from data for imported 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):
"""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][DATA_CONFIG_ENTRIES].pop(entry.entry_id)
remove_init_dispatcher = data.get(DATA_REMOVE_INIT_DISPATCHER)

View File

@ -1,5 +1,4 @@
"""Zerproc lights integration."""
import asyncio
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
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]:
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
@ -42,11 +38,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)