Use _get_reauth_entry in braviatv config flow (#127326)

This commit is contained in:
epenet 2024-10-03 10:16:06 +02:00 committed by GitHub
parent c658dc0ffc
commit 409d7b3652
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,7 +11,12 @@ from pybravia import BraviaAuthError, BraviaClient, BraviaError, BraviaNotSuppor
import voluptuous as vol import voluptuous as vol
from homeassistant.components import ssdp from homeassistant.components import ssdp
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult from homeassistant.config_entries import (
SOURCE_REAUTH,
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
)
from homeassistant.const import CONF_CLIENT_ID, CONF_HOST, CONF_MAC, CONF_NAME, CONF_PIN from homeassistant.const import CONF_CLIENT_ID, CONF_HOST, CONF_MAC, CONF_NAME, CONF_PIN
from homeassistant.helpers import instance_id from homeassistant.helpers import instance_id
from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers.aiohttp_client import async_create_clientsession
@ -33,11 +38,12 @@ class BraviaTVConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 1 VERSION = 1
entry: ConfigEntry
def __init__(self) -> None: def __init__(self) -> None:
"""Initialize config flow.""" """Initialize config flow."""
self.client: BraviaClient | None = None self.client: BraviaClient | None = None
self.device_config: dict[str, Any] = {} self.device_config: dict[str, Any] = {}
self.entry: ConfigEntry | None = None
def create_client(self) -> None: def create_client(self) -> None:
"""Create Bravia TV client from config.""" """Create Bravia TV client from config."""
@ -86,7 +92,6 @@ class BraviaTVConfigFlow(ConfigFlow, domain=DOMAIN):
async def async_reauth_device(self) -> ConfigFlowResult: async def async_reauth_device(self) -> ConfigFlowResult:
"""Reauthorize Bravia TV device from config.""" """Reauthorize Bravia TV device from config."""
assert self.entry
assert self.client assert self.client
await self.async_connect_device() await self.async_connect_device()
@ -147,7 +152,7 @@ class BraviaTVConfigFlow(ConfigFlow, domain=DOMAIN):
self.device_config[CONF_CLIENT_ID] = client_id self.device_config[CONF_CLIENT_ID] = client_id
self.device_config[CONF_NICKNAME] = nickname self.device_config[CONF_NICKNAME] = nickname
try: try:
if self.entry: if self.source == SOURCE_REAUTH:
return await self.async_reauth_device() return await self.async_reauth_device()
return await self.async_create_device() return await self.async_create_device()
except BraviaAuthError: except BraviaAuthError:
@ -183,7 +188,7 @@ class BraviaTVConfigFlow(ConfigFlow, domain=DOMAIN):
if user_input is not None: if user_input is not None:
self.device_config[CONF_PIN] = user_input[CONF_PIN] self.device_config[CONF_PIN] = user_input[CONF_PIN]
try: try:
if self.entry: if self.source == SOURCE_REAUTH:
return await self.async_reauth_device() return await self.async_reauth_device()
return await self.async_create_device() return await self.async_create_device()
except BraviaAuthError: except BraviaAuthError:
@ -247,6 +252,6 @@ class BraviaTVConfigFlow(ConfigFlow, domain=DOMAIN):
self, entry_data: Mapping[str, Any] self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Handle configuration by re-auth.""" """Handle configuration by re-auth."""
self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) self.entry = self._get_reauth_entry()
self.device_config = {**entry_data} self.device_config = {**entry_data}
return await self.async_step_authorize() return await self.async_step_authorize()