2022-04-26 23:37:13 +00:00
|
|
|
"""The Tautulli integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-06-29 09:21:01 +00:00
|
|
|
from pytautulli import PyTautulli, PyTautulliApiUser, PyTautulliHostConfiguration
|
2022-04-26 23:37:13 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL, Platform
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
|
|
|
from homeassistant.helpers.entity import EntityDescription
|
2022-04-26 23:37:13 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
|
|
|
|
from .const import DEFAULT_NAME, DOMAIN
|
|
|
|
from .coordinator import TautulliDataUpdateCoordinator
|
|
|
|
|
|
|
|
PLATFORMS = [Platform.SENSOR]
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up Tautulli from a config entry."""
|
|
|
|
host_configuration = PyTautulliHostConfiguration(
|
|
|
|
api_token=entry.data[CONF_API_KEY],
|
|
|
|
url=entry.data[CONF_URL],
|
|
|
|
verify_ssl=entry.data[CONF_VERIFY_SSL],
|
|
|
|
)
|
|
|
|
api_client = PyTautulli(
|
|
|
|
host_configuration=host_configuration,
|
|
|
|
session=async_get_clientsession(hass, entry.data[CONF_VERIFY_SSL]),
|
|
|
|
)
|
|
|
|
coordinator = TautulliDataUpdateCoordinator(hass, host_configuration, api_client)
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2022-09-26 12:54:17 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2022-04-26 23:37:13 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
2022-09-26 12:54:17 +00:00
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2022-04-26 23:37:13 +00:00
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
|
|
|
class TautulliEntity(CoordinatorEntity[TautulliDataUpdateCoordinator]):
|
|
|
|
"""Defines a base Tautulli entity."""
|
|
|
|
|
2022-07-18 21:22:38 +00:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2022-04-26 23:37:13 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: TautulliDataUpdateCoordinator,
|
|
|
|
description: EntityDescription,
|
2022-06-29 09:21:01 +00:00
|
|
|
user: PyTautulliApiUser | None = None,
|
2022-04-26 23:37:13 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize the Tautulli entity."""
|
|
|
|
super().__init__(coordinator)
|
2022-06-29 09:21:01 +00:00
|
|
|
entry_id = coordinator.config_entry.entry_id
|
|
|
|
self._attr_unique_id = f"{entry_id}_{description.key}"
|
2022-04-26 23:37:13 +00:00
|
|
|
self.entity_description = description
|
2022-06-29 09:21:01 +00:00
|
|
|
self.user = user
|
2022-04-26 23:37:13 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
configuration_url=coordinator.host_configuration.base_url,
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
2022-06-29 09:21:01 +00:00
|
|
|
identifiers={(DOMAIN, user.user_id if user else entry_id)},
|
2022-04-26 23:37:13 +00:00
|
|
|
manufacturer=DEFAULT_NAME,
|
2022-06-29 09:21:01 +00:00
|
|
|
name=user.username if user else DEFAULT_NAME,
|
2022-04-26 23:37:13 +00:00
|
|
|
)
|