1 """Coordinator for transmssion integration."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 import transmission_rpc
9 from transmission_rpc.session
import SessionStats
21 DEFAULT_SCAN_INTERVAL,
23 EVENT_DOWNLOADED_TORRENT,
24 EVENT_REMOVED_TORRENT,
25 EVENT_STARTED_TORRENT,
28 _LOGGER = logging.getLogger(__name__)
32 """Transmission dataupdate coordinator class."""
34 config_entry: ConfigEntry
37 self, hass: HomeAssistant, entry: ConfigEntry, api: transmission_rpc.Client
39 """Initialize the Transmission RPC API."""
42 self.
hosthost = entry.data[CONF_HOST]
43 self.
_session_session: transmission_rpc.Session |
None =
None
44 self.
_all_torrents_all_torrents: list[transmission_rpc.Torrent] = []
47 self.
torrentstorrents: list[transmission_rpc.Torrent] = []
50 name=f
"{DOMAIN} - {self.host}",
52 update_interval=
timedelta(seconds=DEFAULT_SCAN_INTERVAL),
66 """Update transmission data."""
67 return await self.
hasshass.async_add_executor_job(self.
updateupdate)
70 """Get the latest data from Transmission instance."""
72 data = self.
apiapi.session_stats()
75 except transmission_rpc.TransmissionError
as err:
76 raise UpdateFailed(
"Unable to connect to Transmission client")
from err
85 """Initialize torrent lists."""
88 torrent
for torrent
in self.
torrentstorrents
if torrent.status ==
"seeding"
91 torrent
for torrent
in self.
torrentstorrents
if torrent.status ==
"downloading"
95 """Get completed torrent functionality."""
96 old_completed_torrents = {torrent.id
for torrent
in self.
_completed_torrents_completed_torrents}
98 current_completed_torrents = [
99 torrent
for torrent
in self.
torrentstorrents
if torrent.status ==
"seeding"
102 for torrent
in current_completed_torrents:
103 if torrent.id
not in old_completed_torrents:
104 self.
hasshass.bus.fire(
105 EVENT_DOWNLOADED_TORRENT, {
"name": torrent.name,
"id": torrent.id}
111 """Get started torrent functionality."""
112 old_started_torrents = {torrent.id
for torrent
in self.
_started_torrents_started_torrents}
114 current_started_torrents = [
115 torrent
for torrent
in self.
torrentstorrents
if torrent.status ==
"downloading"
118 for torrent
in current_started_torrents:
119 if torrent.id
not in old_started_torrents:
120 self.
hasshass.bus.fire(
121 EVENT_STARTED_TORRENT, {
"name": torrent.name,
"id": torrent.id}
127 """Get removed torrent functionality."""
128 current_torrents = {torrent.id
for torrent
in self.
torrentstorrents}
131 if torrent.id
not in current_torrents:
132 self.
hasshass.bus.fire(
133 EVENT_REMOVED_TORRENT, {
"name": torrent.name,
"id": torrent.id}
139 """Start all torrents."""
142 self.
apiapi.start_all()
145 """Stop all active torrents."""
148 torrent_ids: list[int | str] = [torrent.id
for torrent
in self.
torrentstorrents]
149 self.
apiapi.stop_torrent(torrent_ids)
152 """Set the alternative speed flag."""
153 self.
apiapi.set_session(alt_speed_enabled=is_enabled)
156 """Get the alternative speed flag."""
160 return self.
_session_session.alt_speed_enabled
bool|None get_alt_speed_enabled(self)
None start_torrents(self)
None init_torrent_list(self)
SessionStats _async_update_data(self)
SessionStats update(self)
None check_removed_torrent(self)
None set_alt_speed_enabled(self, bool is_enabled)
None check_started_torrent(self)
None __init__(self, HomeAssistant hass, ConfigEntry entry, transmission_rpc.Client api)
None check_completed_torrent(self)