1 """The qbittorrent component."""
6 from qbittorrentapi
import APIConnectionError, Forbidden403Error, LoginFailed
24 SERVICE_GET_ALL_TORRENTS,
26 STATE_ATTR_ALL_TORRENTS,
30 from .coordinator
import QBittorrentDataCoordinator
31 from .helpers
import format_torrents, setup_client
33 _LOGGER = logging.getLogger(__name__)
35 CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
37 PLATFORMS = [Platform.SENSOR, Platform.SWITCH]
42 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
43 """Set up qBittorrent services."""
45 async
def handle_get_torrents(service_call: ServiceCall) -> dict[str, Any] |
None:
46 device_registry = dr.async_get(hass)
47 device_entry = device_registry.async_get(service_call.data[ATTR_DEVICE_ID])
49 if device_entry
is None:
51 translation_domain=DOMAIN,
52 translation_key=
"invalid_device",
53 translation_placeholders={
54 "device_id": service_call.data[ATTR_DEVICE_ID]
60 for key, value
in device_entry.identifiers:
66 translation_domain=DOMAIN,
67 translation_key=
"invalid_entry_id",
68 translation_placeholders={
"device_id": entry_id
or ""},
71 coordinator: QBittorrentDataCoordinator = hass.data[DOMAIN][entry_id]
72 items = await coordinator.get_torrents(service_call.data[TORRENT_FILTER])
75 STATE_ATTR_TORRENTS: info,
78 hass.services.async_register(
82 supports_response=SupportsResponse.ONLY,
85 async
def handle_get_all_torrents(
86 service_call: ServiceCall,
87 ) -> dict[str, Any] |
None:
90 for key, value
in hass.data[DOMAIN].items():
91 coordinator: QBittorrentDataCoordinator = value
92 items = await coordinator.get_torrents(service_call.data[TORRENT_FILTER])
96 STATE_ATTR_ALL_TORRENTS: torrents,
99 hass.services.async_register(
101 SERVICE_GET_ALL_TORRENTS,
102 handle_get_all_torrents,
103 supports_response=SupportsResponse.ONLY,
110 """Set up qBittorrent from a config entry."""
113 client = await hass.async_add_executor_job(
115 config_entry.data[CONF_URL],
116 config_entry.data[CONF_USERNAME],
117 config_entry.data[CONF_PASSWORD],
118 config_entry.data[CONF_VERIFY_SSL],
120 except LoginFailed
as err:
122 except Forbidden403Error
as err:
124 except APIConnectionError
as exc:
129 await coordinator.async_config_entry_first_refresh()
130 hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = coordinator
132 await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
138 """Unload qBittorrent config entry."""
139 if unload_ok := await hass.config_entries.async_unload_platforms(
140 config_entry, PLATFORMS
142 del hass.data[DOMAIN][config_entry.entry_id]
143 if not hass.data[DOMAIN]:
144 del hass.data[DOMAIN]
dict[str, dict[str, Any]] format_torrents(TorrentInfoList torrents)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)