Use new reauth helpers in samsungtv (#128729)

This commit is contained in:
epenet 2024-10-19 11:24:41 +02:00 committed by GitHub
parent ed9f40fc4c
commit f17c5bc334
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 28 deletions

View File

@ -10,7 +10,7 @@ from urllib.parse import urlparse
import getmac import getmac
from homeassistant.components import ssdp from homeassistant.components import ssdp
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
CONF_MAC, CONF_MAC,
@ -36,7 +36,6 @@ from .const import (
CONF_SESSION_ID, CONF_SESSION_ID,
CONF_SSDP_MAIN_TV_AGENT_LOCATION, CONF_SSDP_MAIN_TV_AGENT_LOCATION,
CONF_SSDP_RENDERING_CONTROL_LOCATION, CONF_SSDP_RENDERING_CONTROL_LOCATION,
DOMAIN,
ENTRY_RELOAD_COOLDOWN, ENTRY_RELOAD_COOLDOWN,
LEGACY_PORT, LEGACY_PORT,
LOGGER, LOGGER,
@ -135,16 +134,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: SamsungTVConfigEntry) ->
def _access_denied() -> None: def _access_denied() -> None:
"""Access denied callback.""" """Access denied callback."""
LOGGER.debug("Access denied in getting remote object") LOGGER.debug("Access denied in getting remote object")
hass.create_task( entry.async_start_reauth(hass)
hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": SOURCE_REAUTH,
"entry_id": entry.entry_id,
},
data=entry.data,
)
)
bridge.register_reauth_callback(_access_denied) bridge.register_reauth_callback(_access_denied)

View File

@ -105,7 +105,6 @@ class SamsungTVConfigFlow(ConfigFlow, domain=DOMAIN):
def __init__(self) -> None: def __init__(self) -> None:
"""Initialize flow.""" """Initialize flow."""
self._reauth_entry: ConfigEntry | None = None
self._host: str = "" self._host: str = ""
self._mac: str | None = None self._mac: str | None = None
self._udn: str | None = None self._udn: str | None = None
@ -529,9 +528,6 @@ class SamsungTVConfigFlow(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._reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
if entry_data.get(CONF_MODEL) and entry_data.get(CONF_NAME): if entry_data.get(CONF_MODEL) and entry_data.get(CONF_NAME):
self._title = f"{entry_data[CONF_NAME]} ({entry_data[CONF_MODEL]})" self._title = f"{entry_data[CONF_NAME]} ({entry_data[CONF_MODEL]})"
else: else:
@ -543,22 +539,23 @@ class SamsungTVConfigFlow(ConfigFlow, domain=DOMAIN):
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Confirm reauth.""" """Confirm reauth."""
errors = {} errors = {}
assert self._reauth_entry
method = self._reauth_entry.data[CONF_METHOD] reauth_entry = self._get_reauth_entry()
method = reauth_entry.data[CONF_METHOD]
if user_input is not None: if user_input is not None:
if method == METHOD_ENCRYPTED_WEBSOCKET: if method == METHOD_ENCRYPTED_WEBSOCKET:
return await self.async_step_reauth_confirm_encrypted() return await self.async_step_reauth_confirm_encrypted()
bridge = SamsungTVBridge.get_bridge( bridge = SamsungTVBridge.get_bridge(
self.hass, self.hass,
method, method,
self._reauth_entry.data[CONF_HOST], reauth_entry.data[CONF_HOST],
) )
result = await bridge.async_try_connect() result = await bridge.async_try_connect()
if result == RESULT_SUCCESS: if result == RESULT_SUCCESS:
new_data = dict(self._reauth_entry.data) new_data = dict(reauth_entry.data)
new_data[CONF_TOKEN] = bridge.token new_data[CONF_TOKEN] = bridge.token
return self.async_update_reload_and_abort( return self.async_update_reload_and_abort(
self._reauth_entry, reauth_entry,
data=new_data, data=new_data,
) )
if result not in (RESULT_AUTH_MISSING, RESULT_CANNOT_CONNECT): if result not in (RESULT_AUTH_MISSING, RESULT_CANNOT_CONNECT):
@ -587,8 +584,9 @@ class SamsungTVConfigFlow(ConfigFlow, domain=DOMAIN):
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Confirm reauth (encrypted method).""" """Confirm reauth (encrypted method)."""
errors = {} errors = {}
assert self._reauth_entry
await self._async_start_encrypted_pairing(self._reauth_entry.data[CONF_HOST]) reauth_entry = self._get_reauth_entry()
await self._async_start_encrypted_pairing(reauth_entry.data[CONF_HOST])
assert self._authenticator is not None assert self._authenticator is not None
if user_input is not None: if user_input is not None:
@ -598,9 +596,8 @@ class SamsungTVConfigFlow(ConfigFlow, domain=DOMAIN):
and (session_id := await self._authenticator.get_session_id_and_close()) and (session_id := await self._authenticator.get_session_id_and_close())
): ):
return self.async_update_reload_and_abort( return self.async_update_reload_and_abort(
self._reauth_entry, reauth_entry,
data={ data_updates={
**self._reauth_entry.data,
CONF_TOKEN: token, CONF_TOKEN: token,
CONF_SESSION_ID: session_id, CONF_SESSION_ID: session_id,
}, },

View File

@ -7,7 +7,8 @@ from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.components.device_automation.exceptions import ( from homeassistant.components.device_automation.exceptions import (
InvalidDeviceAutomationConfig, InvalidDeviceAutomationConfig,
) )
from homeassistant.components.samsungtv import DOMAIN, device_trigger from homeassistant.components.samsungtv import device_trigger
from homeassistant.components.samsungtv.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError

View File

@ -5,7 +5,7 @@ from unittest.mock import patch
import pytest import pytest
from homeassistant.components import automation from homeassistant.components import automation
from homeassistant.components.samsungtv import DOMAIN from homeassistant.components.samsungtv.const import DOMAIN
from homeassistant.const import SERVICE_RELOAD, SERVICE_TURN_ON from homeassistant.const import SERVICE_RELOAD, SERVICE_TURN_ON
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr