Make use of new background task in arcam_fmj (#88351)
parent
289bab6f87
commit
f7609e4f92
|
@ -1,6 +1,5 @@
|
||||||
"""Arcam component."""
|
"""Arcam component."""
|
||||||
import asyncio
|
import asyncio
|
||||||
from contextlib import suppress
|
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
@ -9,7 +8,7 @@ from arcam.fmj.client import Client
|
||||||
import async_timeout
|
import async_timeout
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP, Platform
|
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
|
@ -19,7 +18,6 @@ from .const import (
|
||||||
DEFAULT_SCAN_INTERVAL,
|
DEFAULT_SCAN_INTERVAL,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
DOMAIN_DATA_ENTRIES,
|
DOMAIN_DATA_ENTRIES,
|
||||||
DOMAIN_DATA_TASKS,
|
|
||||||
SIGNAL_CLIENT_DATA,
|
SIGNAL_CLIENT_DATA,
|
||||||
SIGNAL_CLIENT_STARTED,
|
SIGNAL_CLIENT_STARTED,
|
||||||
SIGNAL_CLIENT_STOPPED,
|
SIGNAL_CLIENT_STOPPED,
|
||||||
|
@ -32,37 +30,22 @@ CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
||||||
PLATFORMS = [Platform.MEDIA_PLAYER]
|
PLATFORMS = [Platform.MEDIA_PLAYER]
|
||||||
|
|
||||||
|
|
||||||
async def _await_cancel(task: asyncio.Task) -> None:
|
|
||||||
task.cancel()
|
|
||||||
with suppress(asyncio.CancelledError):
|
|
||||||
await task
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up the component."""
|
"""Set up the component."""
|
||||||
hass.data[DOMAIN_DATA_ENTRIES] = {}
|
hass.data[DOMAIN_DATA_ENTRIES] = {}
|
||||||
hass.data[DOMAIN_DATA_TASKS] = {}
|
|
||||||
|
|
||||||
async def _stop(_: Any) -> None:
|
|
||||||
asyncio.gather(
|
|
||||||
*(_await_cancel(task) for task in hass.data[DOMAIN_DATA_TASKS].values())
|
|
||||||
)
|
|
||||||
|
|
||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _stop)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up config entry."""
|
"""Set up config entry."""
|
||||||
entries = hass.data[DOMAIN_DATA_ENTRIES]
|
entries = hass.data[DOMAIN_DATA_ENTRIES]
|
||||||
tasks = hass.data[DOMAIN_DATA_TASKS]
|
|
||||||
|
|
||||||
client = Client(entry.data[CONF_HOST], entry.data[CONF_PORT])
|
client = Client(entry.data[CONF_HOST], entry.data[CONF_PORT])
|
||||||
entries[entry.entry_id] = client
|
entries[entry.entry_id] = client
|
||||||
|
|
||||||
task = asyncio.create_task(_run_client(hass, client, DEFAULT_SCAN_INTERVAL))
|
entry.async_create_background_task(
|
||||||
tasks[entry.entry_id] = task
|
hass, _run_client(hass, client, DEFAULT_SCAN_INTERVAL), "arcam_fmj"
|
||||||
|
)
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
|
@ -72,10 +55,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Cleanup before removing config entry."""
|
"""Cleanup before removing config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
task = hass.data[DOMAIN_DATA_TASKS].pop(entry.entry_id)
|
|
||||||
await _await_cancel(task)
|
|
||||||
|
|
||||||
hass.data[DOMAIN_DATA_ENTRIES].pop(entry.entry_id)
|
hass.data[DOMAIN_DATA_ENTRIES].pop(entry.entry_id)
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
|
@ -12,4 +12,3 @@ DEFAULT_NAME = "Arcam FMJ"
|
||||||
DEFAULT_SCAN_INTERVAL = 5
|
DEFAULT_SCAN_INTERVAL = 5
|
||||||
|
|
||||||
DOMAIN_DATA_ENTRIES = f"{DOMAIN}.entries"
|
DOMAIN_DATA_ENTRIES = f"{DOMAIN}.entries"
|
||||||
DOMAIN_DATA_TASKS = f"{DOMAIN}.tasks"
|
|
||||||
|
|
Loading…
Reference in New Issue