Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """The Logitech Harmony Hub integration utils."""
2 
3 import aioharmony.exceptions as harmony_exceptions
4 from aioharmony.harmonyapi import HarmonyAPI
5 
6 from homeassistant.const import CONF_NAME
7 
8 
9 def find_unique_id_for_remote(harmony: HarmonyAPI):
10  """Find the unique id for both websocket and xmpp clients."""
11  if harmony.hub_id is not None:
12  return str(harmony.hub_id)
13 
14  # fallback timeStampHash if Hub ID is not available
15  return harmony.config["global"]["timeStampHash"].split(";")[-1]
16 
17 
18 def find_best_name_for_remote(data: dict, harmony: HarmonyAPI):
19  """Find the best name from config or fallback to the remote."""
20  # As a last resort we get the name from the harmony client
21  # in the event a name was not provided. harmony.name is
22  # usually the ip address but it can be an empty string.
23  if CONF_NAME not in data or data[CONF_NAME] is None or data[CONF_NAME] == "":
24  return harmony.name
25 
26  return data[CONF_NAME]
27 
28 
29 async def get_harmony_client_if_available(ip_address: str) -> HarmonyAPI | None:
30  """Connect to a harmony hub and fetch info."""
31  harmony = HarmonyAPI(ip_address=ip_address)
32 
33  try:
34  if not await harmony.connect():
35  await harmony.close()
36  return None
37  except harmony_exceptions.TimeOut:
38  return None
39 
40  await harmony.close()
41 
42  return harmony
def find_unique_id_for_remote(HarmonyAPI harmony)
Definition: util.py:9
def find_best_name_for_remote(dict data, HarmonyAPI harmony)
Definition: util.py:18
HarmonyAPI|None get_harmony_client_if_available(str ip_address)
Definition: util.py:29