Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Bang & Olufsen integration."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from aiohttp.client_exceptions import (
8  ClientConnectorError,
9  ClientOSError,
10  ServerTimeoutError,
11 )
12 from mozart_api.exceptions import ApiException
13 from mozart_api.mozart_client import MozartClient
14 
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import CONF_HOST, CONF_MODEL, Platform
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import ConfigEntryNotReady
20 from homeassistant.util.ssl import get_default_context
21 
22 from .const import DOMAIN
23 from .websocket import BangOlufsenWebsocket
24 
25 
26 @dataclass
28  """Dataclass for API client and WebSocket client."""
29 
30  websocket: BangOlufsenWebsocket
31  client: MozartClient
32 
33 
34 type BangOlufsenConfigEntry = ConfigEntry[BangOlufsenData]
35 
36 PLATFORMS = [Platform.MEDIA_PLAYER]
37 
38 
39 async def async_setup_entry(hass: HomeAssistant, entry: BangOlufsenConfigEntry) -> bool:
40  """Set up from a config entry."""
41 
42  # Remove casts to str
43  assert entry.unique_id
44 
45  # Create device now as BangOlufsenWebsocket needs a device for debug logging, firing events etc.
46  device_registry = dr.async_get(hass)
47  device_registry.async_get_or_create(
48  config_entry_id=entry.entry_id,
49  identifiers={(DOMAIN, entry.unique_id)},
50  name=entry.title,
51  model=entry.data[CONF_MODEL],
52  )
53 
54  client = MozartClient(host=entry.data[CONF_HOST], ssl_context=get_default_context())
55 
56  # Check API and WebSocket connection
57  try:
58  await client.check_device_connection(True)
59  except* (
60  ClientConnectorError,
61  ClientOSError,
62  ServerTimeoutError,
63  ApiException,
64  TimeoutError,
65  ) as error:
66  await client.close_api_client()
67  raise ConfigEntryNotReady(f"Unable to connect to {entry.title}") from error
68 
69  websocket = BangOlufsenWebsocket(hass, entry, client)
70 
71  # Add the websocket and API client
72  entry.runtime_data = BangOlufsenData(websocket, client)
73 
74  # Start WebSocket connection
75  await client.connect_notifications(remote_control=True, reconnect=True)
76 
77  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
78 
79  return True
80 
81 
83  hass: HomeAssistant, entry: BangOlufsenConfigEntry
84 ) -> bool:
85  """Unload a config entry."""
86  # Close the API client and WebSocket notification listener
87  entry.runtime_data.client.disconnect_notifications()
88  await entry.runtime_data.client.close_api_client()
89 
90  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, BangOlufsenConfigEntry entry)
Definition: __init__.py:84
bool async_setup_entry(HomeAssistant hass, BangOlufsenConfigEntry entry)
Definition: __init__.py:39
ssl.SSLContext get_default_context()
Definition: ssl.py:118