diff --git a/homeassistant/components/neato/__init__.py b/homeassistant/components/neato/__init__.py index c1e193b7406..cbfd860a0b1 100644 --- a/homeassistant/components/neato/__init__.py +++ b/homeassistant/components/neato/__init__.py @@ -2,10 +2,14 @@ import logging import aiohttp -from pybotvac import Account, Neato +from pybotvac import Account from pybotvac.exceptions import NeatoException 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 CONF_CLIENT_ID, CONF_CLIENT_SECRET, CONF_TOKEN, Platform from homeassistant.core import HomeAssistant @@ -13,7 +17,7 @@ from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv from homeassistant.helpers.typing import ConfigType -from . import api, config_flow +from . import api from .const import NEATO_CONFIG, NEATO_DOMAIN, NEATO_LOGIN from .hub import NeatoHub @@ -21,14 +25,17 @@ _LOGGER = logging.getLogger(__name__) CONFIG_SCHEMA = vol.Schema( - { - NEATO_DOMAIN: vol.Schema( - { - vol.Required(CONF_CLIENT_ID): cv.string, - vol.Required(CONF_CLIENT_SECRET): cv.string, - } - ) - }, + vol.All( + cv.deprecated(NEATO_DOMAIN), + { + NEATO_DOMAIN: vol.Schema( + { + vol.Required(CONF_CLIENT_ID): cv.string, + vol.Required(CONF_CLIENT_SECRET): cv.string, + } + ) + }, + ), extra=vol.ALLOW_EXTRA, ) @@ -43,18 +50,21 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: return True hass.data[NEATO_CONFIG] = config[NEATO_DOMAIN] - vendor = Neato() - config_flow.OAuth2FlowHandler.async_register_implementation( + await async_import_client_credential( hass, - api.NeatoImplementation( - hass, - NEATO_DOMAIN, + NEATO_DOMAIN, + ClientCredential( config[NEATO_DOMAIN][CONF_CLIENT_ID], config[NEATO_DOMAIN][CONF_CLIENT_SECRET], - vendor.auth_endpoint, - vendor.token_endpoint, ), ) + _LOGGER.warning( + "Configuration of Neato 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 diff --git a/homeassistant/components/neato/api.py b/homeassistant/components/neato/api.py index cd26b009040..f3a4324b7ed 100644 --- a/homeassistant/components/neato/api.py +++ b/homeassistant/components/neato/api.py @@ -7,6 +7,7 @@ from typing import Any import pybotvac from homeassistant import config_entries, core +from homeassistant.components.application_credentials import AuthImplementation from homeassistant.helpers import config_entry_oauth2_flow @@ -35,7 +36,7 @@ class ConfigEntryAuth(pybotvac.OAuthSession): # type: ignore[misc] return self.session.token["access_token"] # type: ignore[no-any-return] -class NeatoImplementation(config_entry_oauth2_flow.LocalOAuth2Implementation): +class NeatoImplementation(AuthImplementation): """Neato implementation of LocalOAuth2Implementation. We need this class because we have to add client_secret and scope to the authorization request. diff --git a/homeassistant/components/neato/application_credentials.py b/homeassistant/components/neato/application_credentials.py new file mode 100644 index 00000000000..2abdb6bcc81 --- /dev/null +++ b/homeassistant/components/neato/application_credentials.py @@ -0,0 +1,28 @@ +"""Application credentials platform for neato.""" + +from pybotvac import Neato + +from homeassistant.components.application_credentials import ( + AuthorizationServer, + ClientCredential, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers import config_entry_oauth2_flow + +from . import api + + +async def async_get_auth_implementation( + hass: HomeAssistant, auth_domain: str, credential: ClientCredential +) -> config_entry_oauth2_flow.AbstractOAuth2Implementation: + """Return auth implementation for a custom auth implementation.""" + vendor = Neato() + return api.NeatoImplementation( + hass, + auth_domain, + credential, + AuthorizationServer( + authorize_url=vendor.auth_endpoint, + token_url=vendor.token_endpoint, + ), + ) diff --git a/homeassistant/components/neato/manifest.json b/homeassistant/components/neato/manifest.json index f12f79c77a8..64c10abec06 100644 --- a/homeassistant/components/neato/manifest.json +++ b/homeassistant/components/neato/manifest.json @@ -5,7 +5,7 @@ "documentation": "https://www.home-assistant.io/integrations/neato", "requirements": ["pybotvac==0.0.23"], "codeowners": ["@dshokouhi", "@Santobert"], - "dependencies": ["auth"], + "dependencies": ["application_credentials"], "iot_class": "cloud_polling", "loggers": ["pybotvac"] } diff --git a/homeassistant/generated/application_credentials.py b/homeassistant/generated/application_credentials.py index 7521c5c30b8..41541943086 100644 --- a/homeassistant/generated/application_credentials.py +++ b/homeassistant/generated/application_credentials.py @@ -9,6 +9,7 @@ APPLICATION_CREDENTIALS = [ "geocaching", "google", "home_connect", + "neato", "netatmo", "spotify", "xbox", diff --git a/tests/components/neato/test_config_flow.py b/tests/components/neato/test_config_flow.py index 48bdf247f51..7e187f1e2fd 100644 --- a/tests/components/neato/test_config_flow.py +++ b/tests/components/neato/test_config_flow.py @@ -27,7 +27,6 @@ async def test_full_flow( "neato", { "neato": {"client_id": CLIENT_ID, "client_secret": CLIENT_SECRET}, - "http": {"base_url": "https://example.com"}, }, ) @@ -99,7 +98,6 @@ async def test_reauth( "neato", { "neato": {"client_id": CLIENT_ID, "client_secret": CLIENT_SECRET}, - "http": {"base_url": "https://example.com"}, }, )