mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Use runtime_data in forked_daapd (#138284)
* Use runtime_data in forked_daapd * Adjust
This commit is contained in:
parent
485da61d3c
commit
1a4a3a0f08
@ -2,18 +2,16 @@
|
||||
|
||||
from pyforked_daapd import ForkedDaapdAPI
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import DOMAIN, HASS_DATA_UPDATER_KEY
|
||||
from .coordinator import ForkedDaapdUpdater
|
||||
from .coordinator import ForkedDaapdConfigEntry, ForkedDaapdUpdater
|
||||
|
||||
PLATFORMS = [Platform.MEDIA_PLAYER]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ForkedDaapdConfigEntry) -> bool:
|
||||
"""Set up forked-daapd from a config entry by forwarding to platform."""
|
||||
host: str = entry.data[CONF_HOST]
|
||||
port: int = entry.data[CONF_PORT]
|
||||
@ -22,24 +20,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async_get_clientsession(hass), host, port, password
|
||||
)
|
||||
forked_daapd_updater = ForkedDaapdUpdater(hass, forked_daapd_api, entry.entry_id)
|
||||
if not hass.data.get(DOMAIN):
|
||||
hass.data[DOMAIN] = {entry.entry_id: {}}
|
||||
hass.data.setdefault(DOMAIN, {}).setdefault(entry.entry_id, {})[
|
||||
HASS_DATA_UPDATER_KEY
|
||||
] = forked_daapd_updater
|
||||
entry.runtime_data = forked_daapd_updater
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(
|
||||
hass: HomeAssistant, entry: ForkedDaapdConfigEntry
|
||||
) -> bool:
|
||||
"""Remove forked-daapd component."""
|
||||
status = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if status and hass.data.get(DOMAIN) and hass.data[DOMAIN].get(entry.entry_id):
|
||||
if websocket_handler := hass.data[DOMAIN][entry.entry_id][
|
||||
HASS_DATA_UPDATER_KEY
|
||||
].websocket_handler:
|
||||
if status:
|
||||
if websocket_handler := entry.runtime_data.websocket_handler:
|
||||
websocket_handler.cancel()
|
||||
del hass.data[DOMAIN][entry.entry_id]
|
||||
if not hass.data[DOMAIN]:
|
||||
del hass.data[DOMAIN]
|
||||
return status
|
||||
|
@ -7,12 +7,7 @@ from typing import Any
|
||||
from pyforked_daapd import ForkedDaapdAPI
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import (
|
||||
ConfigEntry,
|
||||
ConfigFlow,
|
||||
ConfigFlowResult,
|
||||
OptionsFlow,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_PORT
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
@ -28,6 +23,7 @@ from .const import (
|
||||
DEFAULT_TTS_VOLUME,
|
||||
DOMAIN,
|
||||
)
|
||||
from .coordinator import ForkedDaapdConfigEntry
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -115,7 +111,7 @@ class ForkedDaapdFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: ForkedDaapdConfigEntry,
|
||||
) -> ForkedDaapdOptionsFlowHandler:
|
||||
"""Return options flow handler."""
|
||||
return ForkedDaapdOptionsFlowHandler()
|
||||
|
@ -30,9 +30,8 @@ DEFAULT_SERVER_NAME = "My Server"
|
||||
DEFAULT_TTS_PAUSE_TIME = 1.2
|
||||
DEFAULT_TTS_VOLUME = 0.8
|
||||
DEFAULT_UNMUTE_VOLUME = 0.6
|
||||
DOMAIN = "forked_daapd" # key for hass.data
|
||||
DOMAIN = "forked_daapd"
|
||||
FD_NAME = "OwnTone"
|
||||
HASS_DATA_UPDATER_KEY = "UPDATER"
|
||||
KNOWN_PIPES = {"librespot-java"}
|
||||
PIPE_FUNCTION_MAP = {
|
||||
"librespot-java": {
|
||||
|
@ -9,6 +9,7 @@ from typing import Any
|
||||
|
||||
from pyforked_daapd import ForkedDaapdAPI
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import PlatformNotReady
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
@ -22,6 +23,8 @@ from .const import (
|
||||
SIGNAL_UPDATE_QUEUE,
|
||||
)
|
||||
|
||||
type ForkedDaapdConfigEntry = ConfigEntry[ForkedDaapdUpdater]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
WS_NOTIFY_EVENT_TYPES = ["player", "outputs", "volume", "options", "queue", "database"]
|
||||
|
@ -27,7 +27,6 @@ from homeassistant.components.spotify import (
|
||||
resolve_spotify_media_type,
|
||||
spotify_uri_from_media_browser_url,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
@ -54,9 +53,7 @@ from .const import (
|
||||
DEFAULT_TTS_PAUSE_TIME,
|
||||
DEFAULT_TTS_VOLUME,
|
||||
DEFAULT_UNMUTE_VOLUME,
|
||||
DOMAIN,
|
||||
FD_NAME,
|
||||
HASS_DATA_UPDATER_KEY,
|
||||
KNOWN_PIPES,
|
||||
PIPE_FUNCTION_MAP,
|
||||
SIGNAL_ADD_ZONES,
|
||||
@ -73,20 +70,18 @@ from .const import (
|
||||
SUPPORTED_FEATURES_ZONE,
|
||||
TTS_TIMEOUT,
|
||||
)
|
||||
from .coordinator import ForkedDaapdUpdater
|
||||
from .coordinator import ForkedDaapdConfigEntry
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: ForkedDaapdConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up forked-daapd from a config entry."""
|
||||
forked_daapd_updater: ForkedDaapdUpdater = hass.data[DOMAIN][config_entry.entry_id][
|
||||
HASS_DATA_UPDATER_KEY
|
||||
]
|
||||
forked_daapd_updater = config_entry.runtime_data
|
||||
|
||||
host: str = config_entry.data[CONF_HOST]
|
||||
forked_daapd_api = forked_daapd_updater.api
|
||||
@ -115,7 +110,7 @@ async def async_setup_entry(
|
||||
await forked_daapd_updater.async_init()
|
||||
|
||||
|
||||
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
async def update_listener(hass: HomeAssistant, entry: ForkedDaapdConfigEntry) -> None:
|
||||
"""Handle options update."""
|
||||
async_dispatcher_send(
|
||||
hass, SIGNAL_CONFIG_OPTIONS_UPDATE.format(entry.entry_id), entry.options
|
||||
|
Loading…
x
Reference in New Issue
Block a user