2020-12-16 22:39:41 +00:00
|
|
|
"""Config flow for Neato Botvac."""
|
2021-03-18 12:21:46 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-10-06 11:05:51 +00:00
|
|
|
import logging
|
|
|
|
|
2019-10-10 06:08:11 +00:00
|
|
|
import voluptuous as vol
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2020-12-16 22:39:41 +00:00
|
|
|
from homeassistant.const import CONF_TOKEN
|
|
|
|
from homeassistant.helpers import config_entry_oauth2_flow
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2020-12-16 22:39:41 +00:00
|
|
|
from .const import NEATO_DOMAIN
|
2019-10-06 11:05:51 +00:00
|
|
|
|
|
|
|
|
2020-12-16 22:39:41 +00:00
|
|
|
class OAuth2FlowHandler(
|
|
|
|
config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=NEATO_DOMAIN
|
|
|
|
):
|
|
|
|
"""Config flow to handle Neato Botvac OAuth2 authentication."""
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2020-12-16 22:39:41 +00:00
|
|
|
DOMAIN = NEATO_DOMAIN
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2020-12-16 22:39:41 +00:00
|
|
|
@property
|
|
|
|
def logger(self) -> logging.Logger:
|
|
|
|
"""Return logger."""
|
|
|
|
return logging.getLogger(__name__)
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2021-03-18 12:21:46 +00:00
|
|
|
async def async_step_user(self, user_input: dict | None = None) -> dict:
|
2020-12-16 22:39:41 +00:00
|
|
|
"""Create an entry for the flow."""
|
|
|
|
current_entries = self._async_current_entries()
|
|
|
|
if current_entries and CONF_TOKEN in current_entries[0].data:
|
|
|
|
# Already configured
|
2019-10-06 11:05:51 +00:00
|
|
|
return self.async_abort(reason="already_configured")
|
|
|
|
|
2020-12-16 22:39:41 +00:00
|
|
|
return await super().async_step_user(user_input=user_input)
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2020-12-16 22:39:41 +00:00
|
|
|
async def async_step_reauth(self, data) -> dict:
|
|
|
|
"""Perform reauth upon migration of old entries."""
|
|
|
|
return await self.async_step_reauth_confirm()
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2021-03-18 12:21:46 +00:00
|
|
|
async def async_step_reauth_confirm(self, user_input: dict | None = None) -> dict:
|
2020-12-16 22:39:41 +00:00
|
|
|
"""Confirm reauth upon migration of old entries."""
|
|
|
|
if user_input is None:
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="reauth_confirm", data_schema=vol.Schema({})
|
|
|
|
)
|
|
|
|
return await self.async_step_user()
|
|
|
|
|
|
|
|
async def async_oauth_create_entry(self, data: dict) -> dict:
|
|
|
|
"""Create an entry for the flow. Update an entry if one already exist."""
|
|
|
|
current_entries = self._async_current_entries()
|
|
|
|
if current_entries and CONF_TOKEN not in current_entries[0].data:
|
|
|
|
# Update entry
|
|
|
|
self.hass.config_entries.async_update_entry(
|
|
|
|
current_entries[0], title=self.flow_impl.name, data=data
|
|
|
|
)
|
|
|
|
self.hass.async_create_task(
|
|
|
|
self.hass.config_entries.async_reload(current_entries[0].entry_id)
|
|
|
|
)
|
|
|
|
return self.async_abort(reason="reauth_successful")
|
|
|
|
return self.async_create_entry(title=self.flow_impl.name, data=data)
|