Squeezebox dispatch helper (#37030)
This commit is contained in:
1 parent
a798b508bc
commit
f7325a7d35
4 files changed
+62
-70
No files matched your search
@@ -1,55 +1,18 @@
|
||||
"""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.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN, ENTRY_PLAYERS, KNOWN_PLAYERS, PLAYER_DISCOVERY_UNSUB
|
||||
from .const import DISCOVERY_TASK, DOMAIN, 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
|
||||
|
||||
|
||||
@@ -66,13 +29,6 @@ async def async_unload_entry(hass, 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)
|
||||
|
||||
|
||||
Reference in new issue
Block a user