Make new_token_callback more generic in SamsungTV (#67990)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2022-03-17 20:24:00 +01:00 committed by GitHub
parent 9ea73e0d90
commit 0adc7042dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 20 deletions

View File

@ -1,6 +1,7 @@
"""The Samsung TV integration.""" """The Samsung TV integration."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Mapping
from functools import partial from functools import partial
import socket import socket
from typing import Any from typing import Any
@ -16,7 +17,6 @@ from homeassistant.const import (
CONF_METHOD, CONF_METHOD,
CONF_NAME, CONF_NAME,
CONF_PORT, CONF_PORT,
CONF_TOKEN,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_STOP,
Platform, Platform,
) )
@ -102,7 +102,7 @@ def _async_get_device_bridge(
data[CONF_METHOD], data[CONF_METHOD],
data[CONF_HOST], data[CONF_HOST],
data[CONF_PORT], data[CONF_PORT],
data.get(CONF_TOKEN), data,
) )
@ -112,15 +112,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Initialize bridge # Initialize bridge
bridge = await _async_create_bridge_with_updated_data(hass, entry) bridge = await _async_create_bridge_with_updated_data(hass, entry)
# Ensure new token gets saved against the config_entry # Ensure updates get saved against the config_entry
@callback @callback
def _update_token() -> None: def _update_config_entry(updates: Mapping[str, Any]) -> None:
"""Update config entry with the new token.""" """Update config entry with the new token."""
hass.config_entries.async_update_entry( hass.config_entries.async_update_entry(entry, data={**entry.data, **updates})
entry, data={**entry.data, CONF_TOKEN: bridge.token}
)
bridge.register_new_token_callback(_update_token) bridge.register_update_config_entry_callback(_update_config_entry)
async def stop_bridge(event: Event) -> None: async def stop_bridge(event: Event) -> None:
"""Stop SamsungTV bridge connection.""" """Stop SamsungTV bridge connection."""

View File

@ -4,6 +4,7 @@ from __future__ import annotations
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
import asyncio import asyncio
from asyncio.exceptions import TimeoutError as AsyncioTimeoutError from asyncio.exceptions import TimeoutError as AsyncioTimeoutError
from collections.abc import Callable, Mapping
import contextlib import contextlib
from typing import Any, cast from typing import Any, cast
@ -24,6 +25,7 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
CONF_PORT, CONF_PORT,
CONF_TIMEOUT, CONF_TIMEOUT,
CONF_TOKEN,
) )
from homeassistant.core import CALLBACK_TYPE, HomeAssistant from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -87,12 +89,12 @@ class SamsungTVBridge(ABC):
method: str, method: str,
host: str, host: str,
port: int | None = None, port: int | None = None,
token: str | None = None, entry_data: Mapping[str, Any] | None = None,
) -> SamsungTVBridge: ) -> SamsungTVBridge:
"""Get Bridge instance.""" """Get Bridge instance."""
if method == METHOD_LEGACY or port == LEGACY_PORT: if method == METHOD_LEGACY or port == LEGACY_PORT:
return SamsungTVLegacyBridge(hass, method, host, port) return SamsungTVLegacyBridge(hass, method, host, port)
return SamsungTVWSBridge(hass, method, host, port, token) return SamsungTVWSBridge(hass, method, host, port, entry_data)
def __init__( def __init__(
self, hass: HomeAssistant, method: str, host: str, port: int | None = None self, hass: HomeAssistant, method: str, host: str, port: int | None = None
@ -104,15 +106,17 @@ class SamsungTVBridge(ABC):
self.host = host self.host = host
self.token: str | None = None self.token: str | None = None
self._reauth_callback: CALLBACK_TYPE | None = None self._reauth_callback: CALLBACK_TYPE | None = None
self._new_token_callback: CALLBACK_TYPE | None = None self._update_config_entry: Callable[[Mapping[str, Any]], None] | None = None
def register_reauth_callback(self, func: CALLBACK_TYPE) -> None: def register_reauth_callback(self, func: CALLBACK_TYPE) -> None:
"""Register a callback function.""" """Register a callback function."""
self._reauth_callback = func self._reauth_callback = func
def register_new_token_callback(self, func: CALLBACK_TYPE) -> None: def register_update_config_entry_callback(
self, func: Callable[[Mapping[str, Any]], None]
) -> None:
"""Register a callback function.""" """Register a callback function."""
self._new_token_callback = func self._update_config_entry = func
@abstractmethod @abstractmethod
async def async_try_connect(self) -> str: async def async_try_connect(self) -> str:
@ -147,10 +151,10 @@ class SamsungTVBridge(ABC):
if self._reauth_callback is not None: if self._reauth_callback is not None:
self._reauth_callback() self._reauth_callback()
def _notify_new_token_callback(self) -> None: def _notify_update_config_entry(self, updates: Mapping[str, Any]) -> None:
"""Notify new token callback.""" """Notify update config callback."""
if self._new_token_callback is not None: if self._update_config_entry is not None:
self._new_token_callback() self._update_config_entry(updates)
class SamsungTVLegacyBridge(SamsungTVBridge): class SamsungTVLegacyBridge(SamsungTVBridge):
@ -304,11 +308,12 @@ class SamsungTVWSBridge(SamsungTVBridge):
method: str, method: str,
host: str, host: str,
port: int | None = None, port: int | None = None,
token: str | None = None, entry_data: Mapping[str, Any] | None = None,
) -> None: ) -> None:
"""Initialize Bridge.""" """Initialize Bridge."""
super().__init__(hass, method, host, port) super().__init__(hass, method, host, port)
self.token = token if entry_data:
self.token = entry_data.get(CONF_TOKEN)
self._rest_api: SamsungTVAsyncRest | None = None self._rest_api: SamsungTVAsyncRest | None = None
self._app_list: dict[str, str] | None = None self._app_list: dict[str, str] | None = None
self._device_info: dict[str, Any] | None = None self._device_info: dict[str, Any] | None = None
@ -505,7 +510,7 @@ class SamsungTVWSBridge(SamsungTVBridge):
self._remote.token, self._remote.token,
) )
self.token = self._remote.token self.token = self._remote.token
self._notify_new_token_callback() self._notify_update_config_entry({CONF_TOKEN: self.token})
return self._remote return self._remote
@staticmethod @staticmethod