1 """The spotify integration."""
3 from __future__
import annotations
5 from datetime
import timedelta
6 from typing
import TYPE_CHECKING
9 from spotifyaio
import Device, SpotifyClient, SpotifyConnectionError
18 async_get_config_entry_implementation,
22 from .browse_media
import async_browse_media
23 from .const
import DOMAIN, LOGGER, SPOTIFY_SCOPES
24 from .coordinator
import SpotifyConfigEntry, SpotifyCoordinator
25 from .models
import SpotifyData
27 is_spotify_media_type,
28 resolve_spotify_media_type,
29 spotify_uri_from_media_browser_url,
32 PLATFORMS = [Platform.MEDIA_PLAYER]
37 "spotify_uri_from_media_browser_url",
38 "is_spotify_media_type",
39 "resolve_spotify_media_type",
44 """Set up Spotify from a config entry."""
49 await session.async_ensure_token_valid()
50 except aiohttp.ClientError
as err:
51 raise ConfigEntryNotReady
from err
55 spotify.authenticate(session.token[CONF_ACCESS_TOKEN])
57 async
def _refresh_token() -> str:
58 await session.async_ensure_token_valid()
59 token = session.token[CONF_ACCESS_TOKEN]
61 assert isinstance(token, str)
64 spotify.refresh_token_function = _refresh_token
68 await coordinator.async_config_entry_first_refresh()
70 async
def _update_devices() -> list[Device]:
72 return await spotify.get_devices()
73 except SpotifyConnectionError
as err:
74 raise UpdateFailed
from err
79 name=f
"{entry.title} Devices",
82 update_method=_update_devices,
84 await device_coordinator.async_config_entry_first_refresh()
86 entry.runtime_data =
SpotifyData(coordinator, session, device_coordinator)
88 if not set(session.token[
"scope"].split(
" ")).issuperset(SPOTIFY_SCOPES):
89 raise ConfigEntryAuthFailed
91 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
96 """Unload Spotify config entry."""
97 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, SpotifyConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry 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)
AbstractOAuth2Implementation async_get_config_entry_implementation(HomeAssistant hass, config_entries.ConfigEntry config_entry)