Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """The QBittorrent coordinator."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 
8 from qbittorrentapi import (
9  APIConnectionError,
10  Client,
11  Forbidden403Error,
12  LoginFailed,
13  SyncMainDataDictionary,
14  TorrentInfoList,
15 )
16 from qbittorrentapi.torrents import TorrentStatusesT
17 
18 from homeassistant.core import HomeAssistant
19 from homeassistant.exceptions import HomeAssistantError
20 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
21 
22 from .const import DOMAIN
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
27 class QBittorrentDataCoordinator(DataUpdateCoordinator[SyncMainDataDictionary]):
28  """Coordinator for updating QBittorrent data."""
29 
30  def __init__(self, hass: HomeAssistant, client: Client) -> None:
31  """Initialize coordinator."""
32  self.clientclient = client
33  self._is_alternative_mode_enabled_is_alternative_mode_enabled = False
34  # self.main_data: dict[str, int] = {}
35  self.total_torrents: dict[str, int] = {}
36  self.active_torrents: dict[str, int] = {}
37  self.inactive_torrents: dict[str, int] = {}
38  self.paused_torrents: dict[str, int] = {}
39  self.seeding_torrents: dict[str, int] = {}
40  self.started_torrents: dict[str, int] = {}
41 
42  super().__init__(
43  hass,
44  _LOGGER,
45  name=DOMAIN,
46  update_interval=timedelta(seconds=30),
47  )
48 
49  async def _async_update_data(self) -> SyncMainDataDictionary:
50  try:
51  data = await self.hasshass.async_add_executor_job(self.clientclient.sync_maindata)
52  self._is_alternative_mode_enabled_is_alternative_mode_enabled = (
53  await self.hasshass.async_add_executor_job(
54  self.clientclient.transfer_speed_limits_mode
55  )
56  == "1"
57  )
58  except (LoginFailed, Forbidden403Error) as exc:
59  raise HomeAssistantError(
60  translation_domain=DOMAIN, translation_key="login_error"
61  ) from exc
62  except APIConnectionError as exc:
63  raise HomeAssistantError(
64  translation_domain=DOMAIN, translation_key="cannot_connect"
65  ) from exc
66  return data
67 
68  def set_alt_speed_enabled(self, is_enabled: bool) -> None:
69  """Set the alternative speed mode."""
70  self.clientclient.transfer_toggle_speed_limits_mode(is_enabled)
71 
72  def toggle_alt_speed_enabled(self) -> None:
73  """Toggle the alternative speed mode."""
74  self.clientclient.transfer_toggle_speed_limits_mode()
75 
76  def get_alt_speed_enabled(self) -> bool:
77  """Get the alternative speed mode."""
78  return self._is_alternative_mode_enabled_is_alternative_mode_enabled
79 
80  async def get_torrents(self, torrent_filter: TorrentStatusesT) -> TorrentInfoList:
81  """Async method to get QBittorrent torrents."""
82  try:
83  torrents = await self.hasshass.async_add_executor_job(
84  lambda: self.clientclient.torrents_info(torrent_filter)
85  )
86  except (LoginFailed, Forbidden403Error) as exc:
87  raise HomeAssistantError(
88  translation_domain=DOMAIN, translation_key="login_error"
89  ) from exc
90  except APIConnectionError as exc:
91  raise HomeAssistantError(
92  translation_domain=DOMAIN, translation_key="cannot_connect"
93  ) from exc
94 
95  return torrents
TorrentInfoList get_torrents(self, TorrentStatusesT torrent_filter)
Definition: coordinator.py:80