2020-03-19 16:29:51 +00:00
|
|
|
"""The Logitech Harmony Hub integration."""
|
|
|
|
import asyncio
|
2021-02-11 07:50:27 +00:00
|
|
|
import logging
|
2020-03-19 16:29:51 +00:00
|
|
|
|
2021-01-04 23:21:14 +00:00
|
|
|
from homeassistant.components.remote import ATTR_ACTIVITY, ATTR_DELAY_SECS
|
2020-03-19 16:29:51 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-18 07:44:29 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME, EVENT_HOMEASSISTANT_STOP
|
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
|
2021-02-11 07:50:27 +00:00
|
|
|
from homeassistant.helpers import entity_registry
|
2020-03-20 01:43:44 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
2020-03-19 16:29:51 +00:00
|
|
|
|
2021-04-18 07:44:29 +00:00
|
|
|
from .const import (
|
|
|
|
CANCEL_LISTENER,
|
|
|
|
CANCEL_STOP,
|
|
|
|
DOMAIN,
|
|
|
|
HARMONY_DATA,
|
|
|
|
HARMONY_OPTIONS_UPDATE,
|
|
|
|
PLATFORMS,
|
|
|
|
)
|
2021-01-04 23:21:14 +00:00
|
|
|
from .data import HarmonyData
|
2020-03-19 16:29:51 +00:00
|
|
|
|
2021-02-11 07:50:27 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-03-19 16:29:51 +00:00
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-03-19 16:29:51 +00:00
|
|
|
"""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]
|
2021-01-04 23:21:14 +00:00
|
|
|
data = HarmonyData(hass, address, name, entry.unique_id)
|
2020-03-19 16:29:51 +00:00
|
|
|
try:
|
2021-01-04 23:21:14 +00:00
|
|
|
connected_ok = await data.connect()
|
2020-08-28 11:50:32 +00:00
|
|
|
except (asyncio.TimeoutError, ValueError, AttributeError) as err:
|
|
|
|
raise ConfigEntryNotReady from err
|
2020-03-19 16:29:51 +00:00
|
|
|
|
2020-03-23 04:24:49 +00:00
|
|
|
if not connected_ok:
|
|
|
|
raise ConfigEntryNotReady
|
|
|
|
|
2021-02-11 07:50:27 +00:00
|
|
|
await _migrate_old_unique_ids(hass, entry.entry_id, data)
|
|
|
|
|
2021-04-18 07:44:29 +00:00
|
|
|
cancel_listener = entry.add_update_listener(_update_listener)
|
|
|
|
|
|
|
|
async def _async_on_stop(event):
|
|
|
|
await data.shutdown()
|
|
|
|
|
|
|
|
cancel_stop = hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, _async_on_stop)
|
|
|
|
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
|
|
HARMONY_DATA: data,
|
|
|
|
CANCEL_LISTENER: cancel_listener,
|
|
|
|
CANCEL_STOP: cancel_stop,
|
|
|
|
}
|
2020-03-19 16:29:51 +00:00
|
|
|
|
2021-04-27 14:09:59 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2020-03-19 16:29:51 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-02-11 07:50:27 +00:00
|
|
|
async def _migrate_old_unique_ids(
|
|
|
|
hass: HomeAssistant, entry_id: str, data: HarmonyData
|
|
|
|
):
|
|
|
|
names_to_ids = {activity["label"]: activity["id"] for activity in data.activities}
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_migrator(entity_entry: entity_registry.RegistryEntry):
|
|
|
|
# Old format for switches was {remote_unique_id}-{activity_name}
|
|
|
|
# New format is activity_{activity_id}
|
|
|
|
parts = entity_entry.unique_id.split("-", 1)
|
|
|
|
if len(parts) > 1: # old format
|
|
|
|
activity_name = parts[1]
|
|
|
|
activity_id = names_to_ids.get(activity_name)
|
|
|
|
|
|
|
|
if activity_id is not None:
|
|
|
|
_LOGGER.info(
|
|
|
|
"Migrating unique_id from [%s] to [%s]",
|
|
|
|
entity_entry.unique_id,
|
|
|
|
activity_id,
|
|
|
|
)
|
|
|
|
return {"new_unique_id": f"activity_{activity_id}"}
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
await entity_registry.async_migrate_entries(hass, entry_id, _async_migrator)
|
|
|
|
|
|
|
|
|
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."""
|
2021-04-27 14:09:59 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-03-19 16:29:51 +00:00
|
|
|
|
|
|
|
# Shutdown a harmony remote for removal
|
2021-04-18 07:44:29 +00:00
|
|
|
entry_data = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
entry_data[CANCEL_LISTENER]()
|
|
|
|
entry_data[CANCEL_STOP]()
|
|
|
|
await entry_data[HARMONY_DATA].shutdown()
|
2020-03-19 16:29:51 +00:00
|
|
|
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|