From ba0b98ef3219fd5d47b83cfc3611aa02663e730b Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 8 Jul 2022 08:30:01 +0200 Subject: [PATCH] Remove deprecated Spotify YAML configuration (#74604) --- homeassistant/components/spotify/__init__.py | 57 +----------- tests/components/spotify/test_config_flow.py | 91 ++++++++++---------- 2 files changed, 47 insertions(+), 101 deletions(-) diff --git a/homeassistant/components/spotify/__init__.py b/homeassistant/components/spotify/__init__.py index bf4e9d8deae..560620beed4 100644 --- a/homeassistant/components/spotify/__init__.py +++ b/homeassistant/components/spotify/__init__.py @@ -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) diff --git a/tests/components/spotify/test_config_flow.py b/tests/components/spotify/test_config_flow.py index a47d25aa06d..8f3b5799374 100644 --- a/tests/components/spotify/test_config_flow.py +++ b/tests/components/spotify/test_config_flow.py @@ -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)