Add reauth flow to webOS TV integration (#86168)

* Add reauth flow to webOS TV integration

* Remove unnecessary else
This commit is contained in:
Shay Levy 2023-01-18 18:48:38 +02:00 committed by GitHub
parent f2b348dbdf
commit c40c37e9ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 217 additions and 37 deletions

View file

@ -1,6 +1,7 @@
"""Config flow to configure webostv component."""
from __future__ import annotations
from collections.abc import Mapping
import logging
from typing import Any
from urllib.parse import urlparse
@ -8,14 +9,14 @@ from urllib.parse import urlparse
from aiowebostv import WebOsTvPairError
import voluptuous as vol
from homeassistant import config_entries, data_entry_flow
from homeassistant.components import ssdp
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.const import CONF_CLIENT_SECRET, CONF_HOST, CONF_NAME
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.data_entry_flow import AbortFlow, FlowResult
from homeassistant.helpers import config_validation as cv
from . import async_control_connect
from . import async_control_connect, update_client_key
from .const import CONF_SOURCES, DEFAULT_NAME, DOMAIN, WEBOSTV_EXCEPTIONS
from .helpers import async_get_sources
@ -30,7 +31,7 @@ DATA_SCHEMA = vol.Schema(
_LOGGER = logging.getLogger(__name__)
class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
class FlowHandler(ConfigFlow, domain=DOMAIN):
"""WebosTV configuration flow."""
VERSION = 1
@ -40,12 +41,11 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
self._host: str = ""
self._name: str = ""
self._uuid: str | None = None
self._entry: ConfigEntry | None = None
@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> OptionsFlowHandler:
def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)
@ -78,7 +78,7 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
)
self.hass.config_entries.async_update_entry(entry, unique_id=self._uuid)
raise data_entry_flow.AbortFlow("already_configured")
raise AbortFlow("already_configured")
async def async_step_pairing(
self, user_input: dict[str, Any] | None = None
@ -129,11 +129,37 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
self._uuid = uuid
return await self.async_step_pairing()
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
"""Perform reauth upon an WebOsTvPairError."""
self._host = entry_data[CONF_HOST]
self._entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
return await self.async_step_reauth_confirm()
class OptionsFlowHandler(config_entries.OptionsFlow):
async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Dialog that informs the user that reauth is required."""
assert self._entry is not None
if user_input is not None:
try:
client = await async_control_connect(self._host, None)
except WebOsTvPairError:
return self.async_abort(reason="error_pairing")
except WEBOSTV_EXCEPTIONS:
return self.async_abort(reason="reauth_unsuccessful")
update_client_key(self.hass, self._entry, client)
await self.hass.config_entries.async_reload(self._entry.entry_id)
return self.async_abort(reason="reauth_successful")
return self.async_show_form(step_id="reauth_confirm")
class OptionsFlowHandler(OptionsFlow):
"""Handle options."""
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
self.options = config_entry.options