2020-03-19 16:29:51 +00:00
|
|
|
"""The Logitech Harmony Hub integration."""
|
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
2020-04-03 13:39:22 +00:00
|
|
|
from homeassistant.components.remote import (
|
|
|
|
ATTR_ACTIVITY,
|
|
|
|
ATTR_DELAY_SECS,
|
|
|
|
DEFAULT_DELAY_SECS,
|
|
|
|
)
|
2020-03-19 16:29:51 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
2020-03-20 01:43:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2020-03-19 16:29:51 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2020-03-20 01:43:44 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
2020-03-19 16:29:51 +00:00
|
|
|
|
2020-07-07 14:02:22 +00:00
|
|
|
from .const import DOMAIN, HARMONY_OPTIONS_UPDATE, PLATFORMS
|
2020-03-20 01:43:44 +00:00
|
|
|
from .remote import HarmonyRemote
|
2020-03-19 16:29:51 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
|
|
"""Set up the Logitech Harmony Hub component."""
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Set up Logitech Harmony Hub from a config entry."""
|
2020-03-20 01:43:44 +00:00
|
|
|
# As there currently is no way to import options from yaml
|
|
|
|
# when setting up a config entry, we fallback to adding
|
|
|
|
# the options to the config entry and pull them out here if
|
|
|
|
# they are missing from the options
|
|
|
|
_async_import_options_from_data_if_missing(hass, entry)
|
2020-03-19 16:29:51 +00:00
|
|
|
|
2020-03-20 01:43:44 +00:00
|
|
|
address = entry.data[CONF_HOST]
|
|
|
|
name = entry.data[CONF_NAME]
|
|
|
|
activity = entry.options.get(ATTR_ACTIVITY)
|
2020-04-03 13:39:22 +00:00
|
|
|
delay_secs = entry.options.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS)
|
2020-03-19 16:29:51 +00:00
|
|
|
|
|
|
|
harmony_conf_file = hass.config.path(f"harmony_{entry.unique_id}.conf")
|
|
|
|
try:
|
2020-03-23 04:24:49 +00:00
|
|
|
device = HarmonyRemote(
|
2020-07-07 14:02:22 +00:00
|
|
|
name, entry.unique_id, address, activity, harmony_conf_file, delay_secs
|
2020-03-23 04:24:49 +00:00
|
|
|
)
|
|
|
|
connected_ok = await device.connect()
|
2020-03-19 16:29:51 +00:00
|
|
|
except (asyncio.TimeoutError, ValueError, AttributeError):
|
|
|
|
raise ConfigEntryNotReady
|
|
|
|
|
2020-03-23 04:24:49 +00:00
|
|
|
if not connected_ok:
|
|
|
|
raise ConfigEntryNotReady
|
|
|
|
|
2020-03-19 16:29:51 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = device
|
|
|
|
|
|
|
|
entry.add_update_listener(_update_listener)
|
|
|
|
|
|
|
|
for component in PLATFORMS:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, component)
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-03-20 01:43:44 +00:00
|
|
|
@callback
|
|
|
|
def _async_import_options_from_data_if_missing(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
options = dict(entry.options)
|
|
|
|
modified = 0
|
|
|
|
for importable_option in [ATTR_ACTIVITY, ATTR_DELAY_SECS]:
|
|
|
|
if importable_option not in entry.options and importable_option in entry.data:
|
|
|
|
options[importable_option] = entry.data[importable_option]
|
|
|
|
modified = 1
|
2020-03-19 16:29:51 +00:00
|
|
|
|
2020-03-20 01:43:44 +00:00
|
|
|
if modified:
|
|
|
|
hass.config_entries.async_update_entry(entry, options=options)
|
2020-03-19 16:29:51 +00:00
|
|
|
|
|
|
|
|
2020-03-20 01:43:44 +00:00
|
|
|
async def _update_listener(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Handle options update."""
|
|
|
|
async_dispatcher_send(
|
|
|
|
hass, f"{HARMONY_OPTIONS_UPDATE}-{entry.unique_id}", entry.options
|
|
|
|
)
|
2020-03-19 16:29:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
unload_ok = all(
|
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
|
|
|
hass.config_entries.async_forward_entry_unload(entry, component)
|
|
|
|
for component in PLATFORMS
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Shutdown a harmony remote for removal
|
|
|
|
device = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
await device.shutdown()
|
|
|
|
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|