Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The bluesound component."""
2 
3 from dataclasses import dataclass
4 
5 from pyblu import Player, SyncStatus
6 from pyblu.errors import PlayerUnreachableError
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_HOST, CONF_PORT, Platform
10 from homeassistant.core import HomeAssistant
11 from homeassistant.exceptions import ConfigEntryNotReady
12 from homeassistant.helpers import config_validation as cv
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 from homeassistant.helpers.typing import ConfigType
15 
16 from .const import DOMAIN
17 from .services import setup_services
18 
19 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
20 
21 PLATFORMS = [Platform.MEDIA_PLAYER]
22 
23 
24 @dataclass
26  """Bluesound data class."""
27 
28  player: Player
29  sync_status: SyncStatus
30 
31 
32 type BluesoundConfigEntry = ConfigEntry[BluesoundRuntimeData]
33 
34 
35 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
36  """Set up the Bluesound."""
37  if DOMAIN not in hass.data:
38  hass.data[DOMAIN] = []
39  setup_services(hass)
40 
41  return True
42 
43 
45  hass: HomeAssistant, config_entry: BluesoundConfigEntry
46 ) -> bool:
47  """Set up the Bluesound entry."""
48  host = config_entry.data[CONF_HOST]
49  port = config_entry.data[CONF_PORT]
50  session = async_get_clientsession(hass)
51  async with Player(host, port, session=session, default_timeout=10) as player:
52  try:
53  sync_status = await player.sync_status(timeout=1)
54  except PlayerUnreachableError as ex:
55  raise ConfigEntryNotReady(f"Error connecting to {host}:{port}") from ex
56 
57  config_entry.runtime_data = BluesoundRuntimeData(player, sync_status)
58 
59  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
60 
61  return True
62 
63 
64 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
65  """Unload a config entry."""
66  return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, BluesoundConfigEntry config_entry)
Definition: __init__.py:46
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:64
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:35
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)