2021-11-12 13:57:40 +00:00
|
|
|
"""The Jellyfin integration."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
|
|
|
|
from .client_wrapper import CannotConnect, InvalidAuth, create_client, validate_input
|
2022-10-09 02:12:30 +00:00
|
|
|
from .const import CONF_CLIENT_DEVICE_ID, DATA_CLIENT, DOMAIN
|
2021-11-12 13:57:40 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up Jellyfin from a config entry."""
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
|
2022-10-09 02:12:30 +00:00
|
|
|
if CONF_CLIENT_DEVICE_ID not in entry.data:
|
|
|
|
entry_data = entry.data.copy()
|
|
|
|
entry_data[CONF_CLIENT_DEVICE_ID] = entry.entry_id
|
|
|
|
hass.config_entries.async_update_entry(entry, data=entry_data)
|
|
|
|
|
2022-10-11 01:24:00 +00:00
|
|
|
client = create_client(
|
|
|
|
device_id=entry.data[CONF_CLIENT_DEVICE_ID],
|
|
|
|
device_name=hass.config.location_name,
|
|
|
|
)
|
2022-10-09 02:12:30 +00:00
|
|
|
|
2021-11-12 13:57:40 +00:00
|
|
|
try:
|
|
|
|
await validate_input(hass, dict(entry.data), client)
|
|
|
|
except CannotConnect as ex:
|
|
|
|
raise ConfigEntryNotReady("Cannot connect to Jellyfin server") from ex
|
|
|
|
except InvalidAuth:
|
|
|
|
_LOGGER.error("Failed to login to Jellyfin server")
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {DATA_CLIENT: client}
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return True
|