From f17c5bc33493baa00b2374f63105e1ec0d893ac8 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 19 Oct 2024 11:24:41 +0200 Subject: [PATCH] Use new reauth helpers in samsungtv (#128729) --- .../components/samsungtv/__init__.py | 14 ++--------- .../components/samsungtv/config_flow.py | 25 ++++++++----------- .../samsungtv/test_device_trigger.py | 3 ++- tests/components/samsungtv/test_trigger.py | 2 +- 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/samsungtv/__init__.py b/homeassistant/components/samsungtv/__init__.py index b43b8abea65..6d4e491b839 100644 --- a/homeassistant/components/samsungtv/__init__.py +++ b/homeassistant/components/samsungtv/__init__.py @@ -10,7 +10,7 @@ from urllib.parse import urlparse import getmac from homeassistant.components import ssdp -from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry +from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( CONF_HOST, CONF_MAC, @@ -36,7 +36,6 @@ from .const import ( CONF_SESSION_ID, CONF_SSDP_MAIN_TV_AGENT_LOCATION, CONF_SSDP_RENDERING_CONTROL_LOCATION, - DOMAIN, ENTRY_RELOAD_COOLDOWN, LEGACY_PORT, LOGGER, @@ -135,16 +134,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: SamsungTVConfigEntry) -> def _access_denied() -> None: """Access denied callback.""" LOGGER.debug("Access denied in getting remote object") - hass.create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) - ) + entry.async_start_reauth(hass) bridge.register_reauth_callback(_access_denied) diff --git a/homeassistant/components/samsungtv/config_flow.py b/homeassistant/components/samsungtv/config_flow.py index 9d2ecefd442..837651f9900 100644 --- a/homeassistant/components/samsungtv/config_flow.py +++ b/homeassistant/components/samsungtv/config_flow.py @@ -105,7 +105,6 @@ class SamsungTVConfigFlow(ConfigFlow, domain=DOMAIN): def __init__(self) -> None: """Initialize flow.""" - self._reauth_entry: ConfigEntry | None = None self._host: str = "" self._mac: str | None = None self._udn: str | None = None @@ -529,9 +528,6 @@ class SamsungTVConfigFlow(ConfigFlow, domain=DOMAIN): self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """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): self._title = f"{entry_data[CONF_NAME]} ({entry_data[CONF_MODEL]})" else: @@ -543,22 +539,23 @@ class SamsungTVConfigFlow(ConfigFlow, domain=DOMAIN): ) -> ConfigFlowResult: """Confirm reauth.""" 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 method == METHOD_ENCRYPTED_WEBSOCKET: return await self.async_step_reauth_confirm_encrypted() bridge = SamsungTVBridge.get_bridge( self.hass, method, - self._reauth_entry.data[CONF_HOST], + reauth_entry.data[CONF_HOST], ) result = await bridge.async_try_connect() if result == RESULT_SUCCESS: - new_data = dict(self._reauth_entry.data) + new_data = dict(reauth_entry.data) new_data[CONF_TOKEN] = bridge.token return self.async_update_reload_and_abort( - self._reauth_entry, + reauth_entry, data=new_data, ) if result not in (RESULT_AUTH_MISSING, RESULT_CANNOT_CONNECT): @@ -587,8 +584,9 @@ class SamsungTVConfigFlow(ConfigFlow, domain=DOMAIN): ) -> ConfigFlowResult: """Confirm reauth (encrypted method).""" 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 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()) ): return self.async_update_reload_and_abort( - self._reauth_entry, - data={ - **self._reauth_entry.data, + reauth_entry, + data_updates={ CONF_TOKEN: token, CONF_SESSION_ID: session_id, }, diff --git a/tests/components/samsungtv/test_device_trigger.py b/tests/components/samsungtv/test_device_trigger.py index acc7ecb904d..fa6efd08076 100644 --- a/tests/components/samsungtv/test_device_trigger.py +++ b/tests/components/samsungtv/test_device_trigger.py @@ -7,7 +7,8 @@ from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.device_automation.exceptions import ( 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.core import HomeAssistant, ServiceCall from homeassistant.exceptions import HomeAssistantError diff --git a/tests/components/samsungtv/test_trigger.py b/tests/components/samsungtv/test_trigger.py index 8076ceb2807..e1d26043bb0 100644 --- a/tests/components/samsungtv/test_trigger.py +++ b/tests/components/samsungtv/test_trigger.py @@ -5,7 +5,7 @@ from unittest.mock import patch import pytest 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.core import HomeAssistant, ServiceCall from homeassistant.helpers import device_registry as dr