1 """The Cambridge Audio integration."""
3 from __future__
import annotations
8 from aiostreammagic
import StreamMagicClient
9 from aiostreammagic.models
import CallbackType
17 from .const
import CONNECT_TIMEOUT, DOMAIN, STREAM_MAGIC_EXCEPTIONS
19 PLATFORMS: list[Platform] = [Platform.MEDIA_PLAYER, Platform.SELECT, Platform.SWITCH]
21 _LOGGER = logging.getLogger(__name__)
23 type CambridgeAudioConfigEntry = ConfigEntry[StreamMagicClient]
27 hass: HomeAssistant, entry: CambridgeAudioConfigEntry
29 """Set up Cambridge Audio integration from a config entry."""
33 async
def _connection_update_callback(
34 _client: StreamMagicClient, _callback_type: CallbackType
36 """Call when the device is notified of changes."""
37 if _callback_type == CallbackType.CONNECTION:
38 if _client.is_connected():
39 _LOGGER.warning(
"Reconnected to device at %s", entry.data[CONF_HOST])
41 _LOGGER.warning(
"Disconnected from device at %s", entry.data[CONF_HOST])
43 await client.register_state_update_callbacks(_connection_update_callback)
46 async
with asyncio.timeout(CONNECT_TIMEOUT):
47 await client.connect()
48 except STREAM_MAGIC_EXCEPTIONS
as err:
50 translation_domain=DOMAIN,
51 translation_key=
"entry_cannot_connect",
52 translation_placeholders={
56 entry.runtime_data = client
58 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
64 hass: HomeAssistant, entry: CambridgeAudioConfigEntry
66 """Unload a config entry."""
67 if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
68 await entry.runtime_data.disconnect()
bool async_unload_entry(HomeAssistant hass, CambridgeAudioConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, CambridgeAudioConfigEntry entry)
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)