Initial commit for Emby Media Player HAOS HACS integration
All checks were successful
Validate / Hassfest (push) Successful in 9s
All checks were successful
Validate / Hassfest (push) Successful in 9s
This commit is contained in:
135
custom_components/emby_player/__init__.py
Normal file
135
custom_components/emby_player/__init__.py
Normal file
@@ -0,0 +1,135 @@
|
||||
"""Emby Media Player integration for Home Assistant."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
|
||||
from .api import EmbyApiClient, EmbyConnectionError
|
||||
from .const import (
|
||||
CONF_API_KEY,
|
||||
CONF_HOST,
|
||||
CONF_PORT,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_SSL,
|
||||
CONF_USER_ID,
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
DEFAULT_SSL,
|
||||
DOMAIN,
|
||||
)
|
||||
from .coordinator import EmbyCoordinator
|
||||
from .websocket import EmbyWebSocket
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORMS = [Platform.MEDIA_PLAYER]
|
||||
|
||||
|
||||
@dataclass
|
||||
class EmbyRuntimeData:
|
||||
"""Runtime data for Emby integration."""
|
||||
|
||||
coordinator: EmbyCoordinator
|
||||
api: EmbyApiClient
|
||||
websocket: EmbyWebSocket
|
||||
user_id: str
|
||||
|
||||
|
||||
type EmbyConfigEntry = ConfigEntry[EmbyRuntimeData]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: EmbyConfigEntry) -> bool:
|
||||
"""Set up Emby Media Player from a config entry."""
|
||||
host = entry.data[CONF_HOST]
|
||||
port = int(entry.data[CONF_PORT])
|
||||
api_key = entry.data[CONF_API_KEY]
|
||||
ssl = entry.data.get(CONF_SSL, DEFAULT_SSL)
|
||||
user_id = entry.data[CONF_USER_ID]
|
||||
scan_interval = int(entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL))
|
||||
|
||||
# Create shared aiohttp session
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
session = async_get_clientsession(hass)
|
||||
|
||||
# Create API client
|
||||
api = EmbyApiClient(
|
||||
host=host,
|
||||
port=port,
|
||||
api_key=api_key,
|
||||
ssl=ssl,
|
||||
session=session,
|
||||
)
|
||||
|
||||
# Test connection
|
||||
try:
|
||||
await api.test_connection()
|
||||
except EmbyConnectionError as err:
|
||||
raise ConfigEntryNotReady(f"Cannot connect to Emby server: {err}") from err
|
||||
|
||||
# Create WebSocket client
|
||||
websocket = EmbyWebSocket(
|
||||
host=host,
|
||||
port=port,
|
||||
api_key=api_key,
|
||||
ssl=ssl,
|
||||
session=session,
|
||||
)
|
||||
|
||||
# Create coordinator
|
||||
coordinator = EmbyCoordinator(
|
||||
hass=hass,
|
||||
api=api,
|
||||
websocket=websocket,
|
||||
scan_interval=scan_interval,
|
||||
)
|
||||
|
||||
# Set up WebSocket connection
|
||||
await coordinator.async_setup()
|
||||
|
||||
# Fetch initial data
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
# Store runtime data
|
||||
entry.runtime_data = EmbyRuntimeData(
|
||||
coordinator=coordinator,
|
||||
api=api,
|
||||
websocket=websocket,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
# Set up platforms
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
# Register update listener for options
|
||||
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def _async_update_listener(hass: HomeAssistant, entry: EmbyConfigEntry) -> None:
|
||||
"""Handle options update."""
|
||||
scan_interval = entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
||||
entry.runtime_data.coordinator.update_scan_interval(scan_interval)
|
||||
_LOGGER.debug("Updated Emby scan interval to %d seconds", scan_interval)
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: EmbyConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
# Unload platforms
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
if unload_ok:
|
||||
# Shut down coordinator (closes WebSocket)
|
||||
await entry.runtime_data.coordinator.async_shutdown()
|
||||
|
||||
return unload_ok
|
||||
Reference in New Issue
Block a user