Home Assistant Unofficial Reference 2024.12.1
helpers.py
Go to the documentation of this file.
1 """Helper functions for LG Netcast TV."""
2 
3 from typing import TypedDict
4 import xml.etree.ElementTree as ET
5 
6 from pylgnetcast import LgNetCastClient
7 from requests import RequestException
8 
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers import device_registry as dr
11 from homeassistant.helpers.device_registry import DeviceEntry
12 
13 from .const import DOMAIN
14 
15 
17  """Unable to retrieve details from Netcast TV."""
18 
19 
20 class NetcastDetails(TypedDict):
21  """Netcast TV Details."""
22 
23  uuid: str
24  model_name: str
25  friendly_name: str
26 
27 
29  hass: HomeAssistant, client: LgNetCastClient
30 ) -> NetcastDetails:
31  """Discover UUID and Model Name from Netcast Tv."""
32  try:
33  resp = await hass.async_add_executor_job(client.query_device_info)
34  except RequestException as err:
36  f"Error in connecting to {client.url}"
37  ) from err
38  except ET.ParseError as err:
39  raise LGNetCastDetailDiscoveryError("Invalid XML") from err
40 
41  if resp is None:
42  raise LGNetCastDetailDiscoveryError("Empty response received")
43 
44  return resp
45 
46 
47 @callback
49  hass: HomeAssistant, device_id: str
50 ) -> DeviceEntry:
51  """Get Device Entry from Device Registry by device ID.
52 
53  Raises ValueError if device ID is invalid.
54  """
55  device_reg = dr.async_get(hass)
56  if (device := device_reg.async_get(device_id)) is None:
57  raise ValueError(f"Device {device_id} is not a valid {DOMAIN} device.")
58 
59  return device
NetcastDetails async_discover_netcast_details(HomeAssistant hass, LgNetCastClient client)
Definition: helpers.py:30
DeviceEntry async_get_device_entry_by_device_id(HomeAssistant hass, str device_id)
Definition: helpers.py:50