2020-03-19 16:29:51 +00:00
|
|
|
"""The Logitech Harmony Hub integration utils."""
|
2020-04-02 16:46:10 +00:00
|
|
|
import aioharmony.exceptions as harmony_exceptions
|
|
|
|
from aioharmony.harmonyapi import HarmonyAPI
|
2020-03-19 16:29:51 +00:00
|
|
|
|
2021-01-14 08:45:32 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2020-04-02 16:46:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def find_unique_id_for_remote(harmony: HarmonyAPI):
|
2020-03-19 16:29:51 +00:00
|
|
|
"""Find the unique id for both websocket and xmpp clients."""
|
2020-06-08 14:55:05 +00:00
|
|
|
if harmony.hub_id is not None:
|
|
|
|
return str(harmony.hub_id)
|
2020-03-19 16:29:51 +00:00
|
|
|
|
2020-06-08 14:55:05 +00:00
|
|
|
# fallback timeStampHash if Hub ID is not available
|
2020-03-20 01:43:44 +00:00
|
|
|
return harmony.config["global"]["timeStampHash"].split(";")[-1]
|
2020-04-02 16:46:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def find_best_name_for_remote(data: dict, harmony: HarmonyAPI):
|
|
|
|
"""Find the best name from config or fallback to the remote."""
|
|
|
|
# As a last resort we get the name from the harmony client
|
|
|
|
# in the event a name was not provided. harmony.name is
|
|
|
|
# usually the ip address but it can be an empty string.
|
|
|
|
if CONF_NAME not in data or data[CONF_NAME] is None or data[CONF_NAME] == "":
|
|
|
|
return harmony.name
|
|
|
|
|
|
|
|
return data[CONF_NAME]
|
|
|
|
|
|
|
|
|
|
|
|
async def get_harmony_client_if_available(ip_address: str):
|
|
|
|
"""Connect to a harmony hub and fetch info."""
|
|
|
|
harmony = HarmonyAPI(ip_address=ip_address)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if not await harmony.connect():
|
|
|
|
await harmony.close()
|
|
|
|
return None
|
|
|
|
except harmony_exceptions.TimeOut:
|
|
|
|
return None
|
|
|
|
|
|
|
|
await harmony.close()
|
|
|
|
|
|
|
|
return harmony
|