Add Neato application credentials platform and deprecate configuration.yaml (#72175)

This commit is contained in:
Allen Porter 2022-05-19 21:22:37 -07:00 committed by GitHub
parent 36e9088e6b
commit c028db00de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 21 deletions

View File

@ -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

View File

@ -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.

View File

@ -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,
),
)

View File

@ -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"]
}

View File

@ -9,6 +9,7 @@ APPLICATION_CREDENTIALS = [
"geocaching",
"google",
"home_connect",
"neato",
"netatmo",
"spotify",
"xbox",

View File

@ -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"},
},
)