Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The MusicCast integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from aiomusiccast.musiccast_device import MusicCastDevice
8 
9 from homeassistant.components import ssdp
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_HOST, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import CONF_SERIAL, CONF_UPNP_DESC, DOMAIN
16 from .coordinator import MusicCastDataUpdateCoordinator
17 
18 PLATFORMS = [Platform.MEDIA_PLAYER, Platform.NUMBER, Platform.SELECT, Platform.SWITCH]
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 
23 async def get_upnp_desc(hass: HomeAssistant, host: str):
24  """Get the upnp description URL for a given host, using the SSPD scanner."""
25  ssdp_entries = await ssdp.async_get_discovery_info_by_st(hass, "upnp:rootdevice")
26  matches = [w for w in ssdp_entries if w.ssdp_headers.get("_host", "") == host]
27  upnp_desc = None
28  for match in matches:
29  if upnp_desc := match.ssdp_location:
30  break
31 
32  if not upnp_desc:
33  _LOGGER.warning(
34  "The upnp_description was not found automatically, setting a default one"
35  )
36  upnp_desc = f"http://{host}:49154/MediaRenderer/desc.xml"
37  return upnp_desc
38 
39 
40 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
41  """Set up MusicCast from a config entry."""
42 
43  if entry.data.get(CONF_UPNP_DESC) is None:
44  hass.config_entries.async_update_entry(
45  entry,
46  data={
47  CONF_HOST: entry.data[CONF_HOST],
48  CONF_SERIAL: entry.data["serial"],
49  CONF_UPNP_DESC: await get_upnp_desc(hass, entry.data[CONF_HOST]),
50  },
51  )
52 
53  client = MusicCastDevice(
54  entry.data[CONF_HOST],
56  entry.data[CONF_UPNP_DESC],
57  )
58  coordinator = MusicCastDataUpdateCoordinator(hass, client=client)
59  await coordinator.async_config_entry_first_refresh()
60  coordinator.musiccast.build_capabilities()
61 
62  hass.data.setdefault(DOMAIN, {})
63  hass.data[DOMAIN][entry.entry_id] = coordinator
64 
65  await coordinator.musiccast.device.enable_polling()
66 
67  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
68 
69  entry.async_on_unload(entry.add_update_listener(async_reload_entry))
70  return True
71 
72 
73 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
74  """Unload a config entry."""
75  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
76  if unload_ok:
77  hass.data[DOMAIN][entry.entry_id].musiccast.device.disable_polling()
78  hass.data[DOMAIN].pop(entry.entry_id)
79 
80  return unload_ok
81 
82 
83 async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
84  """Reload config entry."""
85  await hass.config_entries.async_reload(entry.entry_id)
None async_reload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:83
def get_upnp_desc(HomeAssistant hass, str host)
Definition: __init__.py:23
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:73
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:40
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)