1 """Utility methods for initializing a Jellyfin client."""
3 from __future__
import annotations
8 from jellyfin_apiclient_python
import Jellyfin, JellyfinClient
9 from jellyfin_apiclient_python.api
import API
10 from jellyfin_apiclient_python.connection_manager
import (
15 from homeassistant
import exceptions
19 from .const
import CLIENT_VERSION, ITEM_KEY_IMAGE_TAGS, USER_AGENT, USER_APP_NAME
23 hass: HomeAssistant, user_input: dict[str, Any], client: JellyfinClient
24 ) -> tuple[str, dict[str, Any]]:
25 """Validate that the provided url and credentials can be used to connect."""
26 url = user_input[CONF_URL]
27 username = user_input[CONF_USERNAME]
28 password = user_input[CONF_PASSWORD]
30 user_id, connect_result = await hass.async_add_executor_job(
31 _connect, client, url, username, password
34 return (user_id, connect_result)
37 def create_client(device_id: str, device_name: str |
None =
None) -> JellyfinClient:
38 """Create a new Jellyfin client."""
39 if device_name
is None:
40 device_name = socket.gethostname()
44 client = jellyfin.get_client()
45 client.config.app(USER_APP_NAME, CLIENT_VERSION, device_name, device_id)
46 client.config.http(USER_AGENT)
52 client: JellyfinClient, url: str, username: str, password: str
53 ) -> tuple[str, dict[str, Any]]:
54 """Connect to the Jellyfin server and assert that the user can login."""
55 client.config.data[
"auth.ssl"] = url.startswith(
"https")
59 _login(client.auth, url, username, password)
65 connection_manager: ConnectionManager, url: str
67 """Connect to the Jellyfin server."""
68 result: dict[str, Any] = connection_manager.connect_to_address(url)
70 if result[
"State"] != CONNECTION_STATE[
"ServerSignIn"]:
77 connection_manager: ConnectionManager,
82 """Assert that the user can log in to the Jellyfin server."""
83 response = connection_manager.login(url, username, password)
85 if "AccessToken" not in response:
90 """Set the unique userid from a Jellyfin server."""
91 settings: dict[str, Any] = api.get_user_settings()
92 userid: str = settings[
"Id"]
97 client: JellyfinClient, item: dict[str, Any], max_width: int = 600
99 """Find a suitable thumbnail for an item."""
100 artwork_id: str = item[
"Id"]
101 artwork_type =
"Primary"
102 parent_backdrop_id: str |
None = item.get(
"ParentBackdropItemId")
104 if "Backdrop" in item[ITEM_KEY_IMAGE_TAGS]:
105 artwork_type =
"Backdrop"
106 elif parent_backdrop_id:
107 artwork_type =
"Backdrop"
108 artwork_id = parent_backdrop_id
109 elif "Primary" not in item[ITEM_KEY_IMAGE_TAGS]:
112 return str(client.jellyfin.artwork(artwork_id, artwork_type, max_width))
116 """Error to indicate the server is unreachable."""
120 """Error to indicate the credentials are invalid."""
JellyfinClient create_client(str device_id, str|None device_name=None)
tuple[str, dict[str, Any]] validate_input(HomeAssistant hass, dict[str, Any] user_input, JellyfinClient client)
str|None get_artwork_url(JellyfinClient client, dict[str, Any] item, int max_width=600)
dict[str, Any] _connect_to_address(ConnectionManager connection_manager, str url)
tuple[str, dict[str, Any]] _connect(JellyfinClient client, str url, str username, str password)
str _get_user_id(API api)
None _login(ConnectionManager connection_manager, str url, str username, str password)