2019-03-17 03:44:05 +00:00
|
|
|
"""The kodi component."""
|
2019-08-10 21:31:04 +00:00
|
|
|
|
2020-08-21 04:16:58 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from pykodi import CannotConnectError, InvalidAuthError, Kodi, get_kodi_connection
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_SSL,
|
|
|
|
CONF_USERNAME,
|
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
2021-12-06 03:10:07 +00:00
|
|
|
Platform,
|
2020-08-21 04:16:58 +00:00
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
CONF_WS_PORT,
|
|
|
|
DATA_CONNECTION,
|
|
|
|
DATA_KODI,
|
|
|
|
DATA_REMOVE_LISTENER,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2019-12-08 22:42:04 +00:00
|
|
|
|
2020-08-21 04:16:58 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2021-12-06 03:10:07 +00:00
|
|
|
PLATFORMS = [Platform.MEDIA_PLAYER]
|
2019-08-10 21:31:04 +00:00
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-08-21 04:16:58 +00:00
|
|
|
"""Set up Kodi from a config entry."""
|
|
|
|
conn = get_kodi_connection(
|
|
|
|
entry.data[CONF_HOST],
|
|
|
|
entry.data[CONF_PORT],
|
|
|
|
entry.data[CONF_WS_PORT],
|
|
|
|
entry.data[CONF_USERNAME],
|
|
|
|
entry.data[CONF_PASSWORD],
|
|
|
|
entry.data[CONF_SSL],
|
|
|
|
session=async_get_clientsession(hass),
|
|
|
|
)
|
2020-11-17 17:27:58 +00:00
|
|
|
|
|
|
|
kodi = Kodi(conn)
|
|
|
|
|
2020-08-21 04:16:58 +00:00
|
|
|
try:
|
|
|
|
await conn.connect()
|
2020-11-17 17:27:58 +00:00
|
|
|
except CannotConnectError:
|
|
|
|
pass
|
2020-08-21 04:16:58 +00:00
|
|
|
except InvalidAuthError as error:
|
|
|
|
_LOGGER.error(
|
2020-08-27 11:56:20 +00:00
|
|
|
"Login to %s failed: [%s]",
|
|
|
|
entry.data[CONF_HOST],
|
|
|
|
error,
|
2020-08-21 04:16:58 +00:00
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
async def _close(event):
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
remove_stop_listener = hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close)
|
|
|
|
|
2021-04-16 15:46:49 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2020-08-21 04:16:58 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
|
|
DATA_CONNECTION: conn,
|
|
|
|
DATA_KODI: kodi,
|
|
|
|
DATA_REMOVE_LISTENER: remove_stop_listener,
|
2019-08-10 21:31:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2019-08-10 21:31:04 +00:00
|
|
|
|
|
|
|
return True
|
2020-08-21 04:16:58 +00:00
|
|
|
|
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-08-21 04:16:58 +00:00
|
|
|
"""Unload a config entry."""
|
2021-04-27 16:49:13 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-08-21 04:16:58 +00:00
|
|
|
if unload_ok:
|
|
|
|
data = hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
await data[DATA_CONNECTION].close()
|
|
|
|
data[DATA_REMOVE_LISTENER]()
|
|
|
|
|
|
|
|
return unload_ok
|