Home Assistant Unofficial Reference 2024.12.1
client_wrapper.py
Go to the documentation of this file.
1 """Utility methods for initializing a Jellyfin client."""
2 
3 from __future__ import annotations
4 
5 import socket
6 from typing import Any
7 
8 from jellyfin_apiclient_python import Jellyfin, JellyfinClient
9 from jellyfin_apiclient_python.api import API
10 from jellyfin_apiclient_python.connection_manager import (
11  CONNECTION_STATE,
12  ConnectionManager,
13 )
14 
15 from homeassistant import exceptions
16 from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME
17 from homeassistant.core import HomeAssistant
18 
19 from .const import CLIENT_VERSION, ITEM_KEY_IMAGE_TAGS, USER_AGENT, USER_APP_NAME
20 
21 
22 async def validate_input(
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]
29 
30  user_id, connect_result = await hass.async_add_executor_job(
31  _connect, client, url, username, password
32  )
33 
34  return (user_id, connect_result)
35 
36 
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()
41 
42  jellyfin = Jellyfin()
43 
44  client = jellyfin.get_client()
45  client.config.app(USER_APP_NAME, CLIENT_VERSION, device_name, device_id)
46  client.config.http(USER_AGENT)
47 
48  return client
49 
50 
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")
56 
57  connect_result = _connect_to_address(client.auth, url)
58 
59  _login(client.auth, url, username, password)
60 
61  return (_get_user_id(client.jellyfin), connect_result)
62 
63 
65  connection_manager: ConnectionManager, url: str
66 ) -> dict[str, Any]:
67  """Connect to the Jellyfin server."""
68  result: dict[str, Any] = connection_manager.connect_to_address(url)
69 
70  if result["State"] != CONNECTION_STATE["ServerSignIn"]:
71  raise CannotConnect
72 
73  return result
74 
75 
76 def _login(
77  connection_manager: ConnectionManager,
78  url: str,
79  username: str,
80  password: str,
81 ) -> None:
82  """Assert that the user can log in to the Jellyfin server."""
83  response = connection_manager.login(url, username, password)
84 
85  if "AccessToken" not in response:
86  raise InvalidAuth
87 
88 
89 def _get_user_id(api: API) -> str:
90  """Set the unique userid from a Jellyfin server."""
91  settings: dict[str, Any] = api.get_user_settings()
92  userid: str = settings["Id"]
93  return userid
94 
95 
97  client: JellyfinClient, item: dict[str, Any], max_width: int = 600
98 ) -> str | None:
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")
103 
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]:
110  return None
111 
112  return str(client.jellyfin.artwork(artwork_id, artwork_type, max_width))
113 
114 
116  """Error to indicate the server is unreachable."""
117 
118 
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)
None _login(ConnectionManager connection_manager, str url, str username, str password)