Remove deprecated Spotify YAML configuration (#74604)

This commit is contained in:
Franck Nijhof 2022-07-08 08:30:01 +02:00 committed by GitHub
parent 97426911a3
commit ba0b98ef32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 101 deletions

View file

@ -3,25 +3,14 @@ from __future__ import annotations
from dataclasses import dataclass
from datetime import timedelta
import logging
from typing import Any
import aiohttp
import requests
from spotipy import Spotify, SpotifyException
import voluptuous as vol
from homeassistant.components.application_credentials import (
ClientCredential,
async_import_client_credential,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_CREDENTIALS,
CONF_CLIENT_ID,
CONF_CLIENT_SECRET,
Platform,
)
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv
@ -29,7 +18,6 @@ from homeassistant.helpers.config_entry_oauth2_flow import (
OAuth2Session,
async_get_config_entry_implementation,
)
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .browse_media import async_browse_media
@ -40,23 +28,7 @@ from .util import (
spotify_uri_from_media_browser_url,
)
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = vol.Schema(
vol.All(
cv.deprecated(DOMAIN),
{
DOMAIN: vol.Schema(
{
vol.Inclusive(CONF_CLIENT_ID, ATTR_CREDENTIALS): cv.string,
vol.Inclusive(CONF_CLIENT_SECRET, ATTR_CREDENTIALS): cv.string,
}
)
},
),
extra=vol.ALLOW_EXTRA,
)
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
PLATFORMS = [Platform.MEDIA_PLAYER]
@ -79,31 +51,6 @@ class HomeAssistantSpotifyData:
session: OAuth2Session
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Spotify integration."""
if DOMAIN not in config:
return True
if CONF_CLIENT_ID in config[DOMAIN]:
await async_import_client_credential(
hass,
DOMAIN,
ClientCredential(
config[DOMAIN][CONF_CLIENT_ID],
config[DOMAIN][CONF_CLIENT_SECRET],
),
)
_LOGGER.warning(
"Configuration of Spotify integration in YAML is deprecated and "
"will be removed in a future release; Your existing OAuth "
"Application Credentials have been imported into the UI "
"automatically and can be safely removed from your "
"configuration.yaml file"
)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Spotify from a config entry."""
implementation = await async_get_config_entry_implementation(hass, entry)

View file

@ -2,14 +2,20 @@
from http import HTTPStatus
from unittest.mock import patch
import pytest
from spotipy import SpotifyException
from homeassistant import data_entry_flow, setup
from homeassistant import data_entry_flow
from homeassistant.components import zeroconf
from homeassistant.components.application_credentials import (
ClientCredential,
async_import_client_credential,
)
from homeassistant.components.spotify.const import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@ -24,6 +30,19 @@ BLANK_ZEROCONF_INFO = zeroconf.ZeroconfServiceInfo(
)
@pytest.fixture
async def component_setup(hass: HomeAssistant) -> None:
"""Fixture for setting up the integration."""
result = await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
await async_import_client_credential(
hass, DOMAIN, ClientCredential("client", "secret"), "cred"
)
assert result
async def test_abort_if_no_configuration(hass):
"""Check flow aborts when no configuration is present."""
result = await hass.config_entries.flow.async_init(
@ -54,18 +73,13 @@ async def test_zeroconf_abort_if_existing_entry(hass):
async def test_full_flow(
hass, hass_client_no_auth, aioclient_mock, current_request_with_host
hass,
component_setup,
hass_client_no_auth,
aioclient_mock,
current_request_with_host,
):
"""Check a full flow."""
assert await setup.async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {CONF_CLIENT_ID: "client", CONF_CLIENT_SECRET: "secret"},
"http": {"base_url": "https://example.com"},
},
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
@ -114,7 +128,7 @@ async def test_full_flow(
}
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["data"]["auth_implementation"] == DOMAIN
assert result["data"]["auth_implementation"] == "cred"
result["data"]["token"].pop("expires_at")
assert result["data"]["name"] == "frenck"
assert result["data"]["token"] == {
@ -126,18 +140,13 @@ async def test_full_flow(
async def test_abort_if_spotify_error(
hass, hass_client_no_auth, aioclient_mock, current_request_with_host
hass,
component_setup,
hass_client_no_auth,
aioclient_mock,
current_request_with_host,
):
"""Check Spotify errors causes flow to abort."""
await setup.async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {CONF_CLIENT_ID: "client", CONF_CLIENT_SECRET: "secret"},
"http": {"base_url": "https://example.com"},
},
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
@ -174,23 +183,18 @@ async def test_abort_if_spotify_error(
async def test_reauthentication(
hass, hass_client_no_auth, aioclient_mock, current_request_with_host
hass,
component_setup,
hass_client_no_auth,
aioclient_mock,
current_request_with_host,
):
"""Test Spotify reauthentication."""
await setup.async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {CONF_CLIENT_ID: "client", CONF_CLIENT_SECRET: "secret"},
"http": {"base_url": "https://example.com"},
},
)
old_entry = MockConfigEntry(
domain=DOMAIN,
unique_id=123,
version=1,
data={"id": "frenck", "auth_implementation": DOMAIN},
data={"id": "frenck", "auth_implementation": "cred"},
)
old_entry.add_to_hass(hass)
@ -236,7 +240,7 @@ async def test_reauthentication(
spotify_mock.return_value.current_user.return_value = {"id": "frenck"}
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["data"]["auth_implementation"] == DOMAIN
assert result["data"]["auth_implementation"] == "cred"
result["data"]["token"].pop("expires_at")
assert result["data"]["token"] == {
"refresh_token": "mock-refresh-token",
@ -247,23 +251,18 @@ async def test_reauthentication(
async def test_reauth_account_mismatch(
hass, hass_client_no_auth, aioclient_mock, current_request_with_host
hass,
component_setup,
hass_client_no_auth,
aioclient_mock,
current_request_with_host,
):
"""Test Spotify reauthentication with different account."""
await setup.async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {CONF_CLIENT_ID: "client", CONF_CLIENT_SECRET: "secret"},
"http": {"base_url": "https://example.com"},
},
)
old_entry = MockConfigEntry(
domain=DOMAIN,
unique_id=123,
version=1,
data={"id": "frenck", "auth_implementation": DOMAIN},
data={"id": "frenck", "auth_implementation": "cred"},
)
old_entry.add_to_hass(hass)