* Squeezebox add config flow and player discovery * Fixes to config flow * Unavailable player detection and recovery * Improved error message for auth failure * Testing for squeezebox config flow * Import configuration.yaml * Support for discovery integration * Internal server discovery * Fix bug restoring previously detected squeezebox player * Tests for user and edit steps in config flow * Tests for import config flow * Additional config flow tests and fixes * Linter fixes * Check that players are found before iterating them * Remove noisy logger message * Update requirements_all after rebase * Use asyncio.Event in discovery task * Use common keys in strings.json * Bump pysqueezebox to v0.2.2 for fixed server discovery using python3.7 * Bump pysqueezebox version to v0.2.3 * Don't trap AbortFlow exception Co-authored-by: J. Nick Koston <nick@koston.org> * Refactor validate_input * Update squeezebox tests * Build data flow schema using function * Fix linter error * Updated en.json * Update homeassistant/components/squeezebox/media_player.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/squeezebox/media_player.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/squeezebox/media_player.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/squeezebox/media_player.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update .coveragerc for squeezebox config flow test * Mock TIMEOUT for faster testing * More schema de-duplication and testing improvements * Apply suggestions from code review Co-authored-by: J. Nick Koston <nick@koston.org> * Testing and config flow improvements * Remove unused exceptions * Remove deprecated logger message * Update homeassistant/components/squeezebox/media_player.py Co-authored-by: J. Nick Koston <nick@koston.org> * Implement suggestions from code review * Add async_unload_entry * Use MockConfigEntry in squeezebox tests * Remove unnecessary config schema * Stop server discovery task when last config entry unloaded * Improvements to async_unload_entry * Fix bug in _discovery arguments * Do not await server discovery in async_setup_entry * Do not await start server discovery in async_setup * Do not start server discovery from async_setup_entry until homeassistant running * Re-detect players when server removed and re-added without restart * Use entry.entry_id instead of unique_id * Update unittests to avoid patching homeassistant code Co-authored-by: J. Nick Koston <nick@koston.org>
86 lines
2.9 KiB
Python
86 lines
2.9 KiB
Python
"""The Logitech Squeezebox integration."""
|
|
|
|
import asyncio
|
|
import logging
|
|
|
|
from pysqueezebox import async_discover
|
|
|
|
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
|
|
from homeassistant.config_entries import SOURCE_DISCOVERY, ConfigEntry
|
|
from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_START
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from .const import DOMAIN, ENTRY_PLAYERS, KNOWN_PLAYERS, PLAYER_DISCOVERY_UNSUB
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
DISCOVERY_TASK = "discovery_task"
|
|
|
|
|
|
async def start_server_discovery(hass):
|
|
"""Start a server discovery task."""
|
|
|
|
def _discovered_server(server):
|
|
asyncio.create_task(
|
|
hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_DISCOVERY},
|
|
data={
|
|
CONF_HOST: server.host,
|
|
CONF_PORT: int(server.port),
|
|
"uuid": server.uuid,
|
|
},
|
|
)
|
|
)
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
if DISCOVERY_TASK not in hass.data[DOMAIN]:
|
|
_LOGGER.debug("Adding server discovery task for squeezebox")
|
|
hass.data[DOMAIN][DISCOVERY_TASK] = hass.async_create_task(
|
|
async_discover(_discovered_server)
|
|
)
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
"""Set up the Logitech Squeezebox component."""
|
|
if hass.is_running:
|
|
asyncio.create_task(start_server_discovery(hass))
|
|
else:
|
|
hass.bus.async_listen_once(
|
|
EVENT_HOMEASSISTANT_START, start_server_discovery(hass)
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
"""Set up Logitech Squeezebox from a config entry."""
|
|
hass.async_create_task(
|
|
hass.config_entries.async_forward_entry_setup(entry, MP_DOMAIN)
|
|
)
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass, entry):
|
|
"""Unload a config entry."""
|
|
# Stop player discovery task for this config entry.
|
|
hass.data[DOMAIN][entry.entry_id][PLAYER_DISCOVERY_UNSUB]()
|
|
|
|
# Remove config entry's players from list of known players
|
|
entry_players = hass.data[DOMAIN][entry.entry_id][ENTRY_PLAYERS]
|
|
if entry_players:
|
|
for player in entry_players:
|
|
_LOGGER.debug("Remove entry player %s from list of known players.", player)
|
|
hass.data[DOMAIN][KNOWN_PLAYERS].remove(player)
|
|
|
|
# Remove stored data for this config entry
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
# Stop server discovery task if this is the last config entry.
|
|
current_entries = hass.config_entries.async_entries(DOMAIN)
|
|
if len(current_entries) == 1 and current_entries[0] == entry:
|
|
_LOGGER.debug("Stopping server discovery task")
|
|
hass.data[DOMAIN][DISCOVERY_TASK].cancel()
|
|
hass.data[DOMAIN].pop(DISCOVERY_TASK)
|
|
|
|
return await hass.config_entries.async_forward_entry_unload(entry, MP_DOMAIN)
|