-
-
Notifications
You must be signed in to change notification settings - Fork 32k
/
Copy path__init__.py
292 lines (236 loc) · 10 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""The Samsung TV integration."""
from __future__ import annotations
from collections.abc import Coroutine, Mapping
from functools import partial
from typing import Any
from urllib.parse import urlparse
import getmac
from homeassistant.components import ssdp
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_HOST,
CONF_MAC,
CONF_METHOD,
CONF_MODEL,
CONF_PORT,
CONF_TOKEN,
EVENT_HOMEASSISTANT_STOP,
Platform,
)
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.debounce import Debouncer
from .bridge import (
SamsungTVBridge,
async_get_device_info,
mac_from_device_info,
model_requires_encryption,
)
from .const import (
CONF_SESSION_ID,
CONF_SSDP_MAIN_TV_AGENT_LOCATION,
CONF_SSDP_RENDERING_CONTROL_LOCATION,
ENTRY_RELOAD_COOLDOWN,
LEGACY_PORT,
LOGGER,
METHOD_ENCRYPTED_WEBSOCKET,
METHOD_LEGACY,
UPNP_SVC_MAIN_TV_AGENT,
UPNP_SVC_RENDERING_CONTROL,
)
from .coordinator import SamsungTVDataUpdateCoordinator
PLATFORMS = [Platform.MEDIA_PLAYER, Platform.REMOTE]
SamsungTVConfigEntry = ConfigEntry[SamsungTVDataUpdateCoordinator]
@callback
def _async_get_device_bridge(
hass: HomeAssistant, data: dict[str, Any]
) -> SamsungTVBridge:
"""Get device bridge."""
return SamsungTVBridge.get_bridge(
hass,
data[CONF_METHOD],
data[CONF_HOST],
data[CONF_PORT],
data,
)
class DebouncedEntryReloader:
"""Reload only after the timer expires."""
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Init the debounced entry reloader."""
self.hass = hass
self.entry = entry
self.token = self.entry.data.get(CONF_TOKEN)
self._debounced_reload: Debouncer[Coroutine[Any, Any, None]] = Debouncer(
hass,
LOGGER,
cooldown=ENTRY_RELOAD_COOLDOWN,
immediate=False,
function=self._async_reload_entry,
)
async def async_call(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Start the countdown for a reload."""
if (new_token := entry.data.get(CONF_TOKEN)) != self.token:
LOGGER.debug("Skipping reload as its a token update")
self.token = new_token
return # Token updates should not trigger a reload
LOGGER.debug("Calling debouncer to get a reload after cooldown")
await self._debounced_reload.async_call()
@callback
def async_shutdown(self) -> None:
"""Cancel any pending reload."""
self._debounced_reload.async_shutdown()
async def _async_reload_entry(self) -> None:
"""Reload entry."""
LOGGER.debug("Reloading entry %s", self.entry.title)
await self.hass.config_entries.async_reload(self.entry.entry_id)
async def _async_update_ssdp_locations(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update ssdp locations from discovery cache."""
updates = {}
for ssdp_st, key in (
(UPNP_SVC_RENDERING_CONTROL, CONF_SSDP_RENDERING_CONTROL_LOCATION),
(UPNP_SVC_MAIN_TV_AGENT, CONF_SSDP_MAIN_TV_AGENT_LOCATION),
):
for discovery_info in await ssdp.async_get_discovery_info_by_st(hass, ssdp_st):
location = discovery_info.ssdp_location
host = urlparse(location).hostname
if host == entry.data[CONF_HOST]:
updates[key] = location
break
if updates:
hass.config_entries.async_update_entry(entry, data={**entry.data, **updates})
async def async_setup_entry(hass: HomeAssistant, entry: SamsungTVConfigEntry) -> bool:
"""Set up the Samsung TV platform."""
# Initialize bridge
if entry.data.get(CONF_METHOD) == METHOD_ENCRYPTED_WEBSOCKET:
if not entry.data.get(CONF_TOKEN) or not entry.data.get(CONF_SESSION_ID):
raise ConfigEntryAuthFailed(
"Token and session id are required in encrypted mode"
)
bridge = await _async_create_bridge_with_updated_data(hass, entry)
@callback
def _access_denied() -> None:
"""Access denied callback."""
LOGGER.debug("Access denied in getting remote object")
entry.async_start_reauth(hass)
bridge.register_reauth_callback(_access_denied)
# Ensure updates get saved against the config_entry
@callback
def _update_config_entry(updates: Mapping[str, Any]) -> None:
"""Update config entry with the new token."""
hass.config_entries.async_update_entry(entry, data={**entry.data, **updates})
bridge.register_update_config_entry_callback(_update_config_entry)
async def stop_bridge(event: Event | None = None) -> None:
"""Stop SamsungTV bridge connection."""
LOGGER.debug("Stopping SamsungTVBridge %s", bridge.host)
await bridge.async_close_remote()
entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_bridge)
)
entry.async_on_unload(stop_bridge)
await _async_update_ssdp_locations(hass, entry)
# We must not await after we setup the reload or there
# will be a race where the config flow will see the entry
# as not loaded and may reload it
debounced_reloader = DebouncedEntryReloader(hass, entry)
entry.async_on_unload(debounced_reloader.async_shutdown)
entry.async_on_unload(entry.add_update_listener(debounced_reloader.async_call))
coordinator = SamsungTVDataUpdateCoordinator(hass, bridge)
await coordinator.async_config_entry_first_refresh()
entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def _async_create_bridge_with_updated_data(
hass: HomeAssistant, entry: ConfigEntry
) -> SamsungTVBridge:
"""Create a bridge object and update any missing data in the config entry."""
updated_data: dict[str, str | int] = {}
host: str = entry.data[CONF_HOST]
port: int | None = entry.data.get(CONF_PORT)
method: str | None = entry.data.get(CONF_METHOD)
load_info_attempted = False
info: dict[str, Any] | None = None
if not port or not method:
LOGGER.debug("Attempting to get port or method for %s", host)
if method == METHOD_LEGACY:
port = LEGACY_PORT
else:
# When we imported from yaml we didn't setup the method
# because we didn't know it
_result, port, method, info = await async_get_device_info(hass, host)
load_info_attempted = True
if not port or not method:
raise ConfigEntryNotReady(
"Failed to determine connection method, make sure the device is on."
)
LOGGER.debug("Updated port to %s and method to %s for %s", port, method, host)
updated_data[CONF_PORT] = port
updated_data[CONF_METHOD] = method
bridge = _async_get_device_bridge(hass, {**entry.data, **updated_data})
mac: str | None = entry.data.get(CONF_MAC)
model: str | None = entry.data.get(CONF_MODEL)
mac_is_incorrectly_formatted = mac and dr.format_mac(mac) != mac
if (
not mac or not model or mac_is_incorrectly_formatted
) and not load_info_attempted:
info = await bridge.async_device_info()
if not mac or mac_is_incorrectly_formatted:
LOGGER.debug("Attempting to get mac for %s", host)
if info:
mac = mac_from_device_info(info)
if not mac:
mac = await hass.async_add_executor_job(
partial(getmac.get_mac_address, ip=host)
)
if mac and mac != "none":
# Samsung sometimes returns a value of "none" for the mac address
# this should be ignored
LOGGER.debug("Updated mac to %s for %s", mac, host)
updated_data[CONF_MAC] = dr.format_mac(mac)
else:
LOGGER.warning("Failed to get mac for %s", host)
if not model:
LOGGER.debug("Attempting to get model for %s", host)
if info:
model = info.get("device", {}).get("modelName")
if model:
LOGGER.debug("Updated model to %s for %s", model, host)
updated_data[CONF_MODEL] = model
if model_requires_encryption(model) and method != METHOD_ENCRYPTED_WEBSOCKET:
LOGGER.debug(
(
"Detected model %s for %s. Some televisions from H and J series use "
"an encrypted protocol but you are using %s which may not be supported"
),
model,
host,
method,
)
if updated_data:
data = {**entry.data, **updated_data}
hass.config_entries.async_update_entry(entry, data=data)
return bridge
async def async_unload_entry(hass: HomeAssistant, entry: SamsungTVConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Migrate old entry."""
version = config_entry.version
minor_version = config_entry.minor_version
LOGGER.debug("Migrating from version %s.%s", version, minor_version)
# 1 -> 2: Unique ID format changed, so delete and re-import:
if version == 1:
dev_reg = dr.async_get(hass)
dev_reg.async_clear_config_entry(config_entry.entry_id)
en_reg = er.async_get(hass)
en_reg.async_clear_config_entry(config_entry.entry_id)
version = 2
hass.config_entries.async_update_entry(config_entry, version=2)
if version == 2:
if minor_version < 2:
# Cleanup invalid MAC addresses - see #103512
# Reverted due to device registry collisions - see #119082 / #119249
minor_version = 2
hass.config_entries.async_update_entry(config_entry, minor_version=2)
LOGGER.debug("Migration to version %s.%s successful", version, minor_version)
return True