Home Assistant Unofficial Reference 2024.12.1
helpers.py
Go to the documentation of this file.
1 """Helper functions for webOS Smart TV."""
2 
3 from __future__ import annotations
4 
5 from aiowebostv import WebOsClient
6 
7 from homeassistant.core import HomeAssistant, callback
8 from homeassistant.helpers import device_registry as dr, entity_registry as er
9 from homeassistant.helpers.device_registry import DeviceEntry
10 
11 from . import async_control_connect
12 from .const import DATA_CONFIG_ENTRY, DOMAIN, LIVE_TV_APP_ID, WEBOSTV_EXCEPTIONS
13 
14 
15 @callback
17  hass: HomeAssistant, device_id: str
18 ) -> DeviceEntry:
19  """Get Device Entry from Device Registry by device ID.
20 
21  Raises ValueError if device ID is invalid.
22  """
23  device_reg = dr.async_get(hass)
24  if (device := device_reg.async_get(device_id)) is None:
25  raise ValueError(f"Device {device_id} is not a valid {DOMAIN} device.")
26 
27  return device
28 
29 
30 @callback
31 def async_get_device_id_from_entity_id(hass: HomeAssistant, entity_id: str) -> str:
32  """Get device ID from an entity ID.
33 
34  Raises ValueError if entity or device ID is invalid.
35  """
36  ent_reg = er.async_get(hass)
37  entity_entry = ent_reg.async_get(entity_id)
38 
39  if (
40  entity_entry is None
41  or entity_entry.device_id is None
42  or entity_entry.platform != DOMAIN
43  ):
44  raise ValueError(f"Entity {entity_id} is not a valid {DOMAIN} entity.")
45 
46  return entity_entry.device_id
47 
48 
49 @callback
51  hass: HomeAssistant, device: DeviceEntry
52 ) -> WebOsClient:
53  """Get WebOsClient from Device Registry by device entry.
54 
55  Raises ValueError if client is not found.
56  """
57  for config_entry_id in device.config_entries:
58  if client := hass.data[DOMAIN][DATA_CONFIG_ENTRY].get(config_entry_id):
59  break
60 
61  if not client:
62  raise ValueError(
63  f"Device {device.id} is not from an existing {DOMAIN} config entry"
64  )
65 
66  return client
67 
68 
69 async def async_get_sources(host: str, key: str) -> list[str]:
70  """Construct sources list."""
71  try:
72  client = await async_control_connect(host, key)
73  except WEBOSTV_EXCEPTIONS:
74  return []
75 
76  sources = []
77  found_live_tv = False
78  for app in client.apps.values():
79  sources.append(app["title"])
80  if app["id"] == LIVE_TV_APP_ID:
81  found_live_tv = True
82 
83  for source in client.inputs.values():
84  sources.append(source["label"])
85  if source["appId"] == LIVE_TV_APP_ID:
86  found_live_tv = True
87 
88  if not found_live_tv:
89  sources.append("Live TV")
90 
91  # Preserve order when filtering duplicates
92  return list(dict.fromkeys(sources))
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
WebOsClient async_get_client_by_device_entry(HomeAssistant hass, DeviceEntry device)
Definition: helpers.py:52
list[str] async_get_sources(str host, str key)
Definition: helpers.py:69
str async_get_device_id_from_entity_id(HomeAssistant hass, str entity_id)
Definition: helpers.py:31
DeviceEntry async_get_device_entry_by_device_id(HomeAssistant hass, str device_id)
Definition: helpers.py:18
WebOsClient async_control_connect(str host, str|None key)
Definition: __init__.py:151