Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Snapcast Integration."""
2 
3 import logging
4 
5 import snapcast.control
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_HOST, CONF_PORT
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryNotReady
11 
12 from .const import DOMAIN, PLATFORMS
13 from .server import HomeAssistantSnapcast
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 
18 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
19  """Set up Snapcast from a config entry."""
20  host = entry.data[CONF_HOST]
21  port = entry.data[CONF_PORT]
22  try:
23  server = await snapcast.control.create_server(
24  hass.loop, host, port, reconnect=True
25  )
26  except OSError as ex:
27  raise ConfigEntryNotReady(
28  f"Could not connect to Snapcast server at {host}:{port}"
29  ) from ex
30 
31  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = HomeAssistantSnapcast(
32  hass, server, f"{host}:{port}", entry.entry_id
33  )
34 
35  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
36 
37  return True
38 
39 
40 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
41  """Unload a config entry."""
42  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
43  snapcast_data = hass.data[DOMAIN].pop(entry.entry_id)
44  # disconnect from server
45  await snapcast_data.disconnect()
46  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:18
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:40