Add transmission download path to events + add_torrent service (#121371)
Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>pull/119120/head^2
parent
1de8d63a63
commit
da85c497bf
|
@ -42,6 +42,7 @@ from homeassistant.helpers.typing import ConfigType
|
|||
|
||||
from .const import (
|
||||
ATTR_DELETE_DATA,
|
||||
ATTR_DOWNLOAD_PATH,
|
||||
ATTR_TORRENT,
|
||||
CONF_ENTRY_ID,
|
||||
DEFAULT_DELETE_DATA,
|
||||
|
@ -82,7 +83,12 @@ SERVICE_BASE_SCHEMA = vol.Schema(
|
|||
)
|
||||
|
||||
SERVICE_ADD_TORRENT_SCHEMA = vol.All(
|
||||
SERVICE_BASE_SCHEMA.extend({vol.Required(ATTR_TORRENT): cv.string}),
|
||||
SERVICE_BASE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(ATTR_TORRENT): cv.string,
|
||||
vol.Optional(ATTR_DOWNLOAD_PATH, default=None): cv.string,
|
||||
}
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
@ -213,10 +219,18 @@ def setup_hass_services(hass: HomeAssistant) -> None:
|
|||
entry_id: str = service.data[CONF_ENTRY_ID]
|
||||
coordinator = _get_coordinator_from_service_data(hass, entry_id)
|
||||
torrent: str = service.data[ATTR_TORRENT]
|
||||
download_path: str | None = service.data.get(ATTR_DOWNLOAD_PATH)
|
||||
if torrent.startswith(
|
||||
("http", "ftp:", "magnet:")
|
||||
) or hass.config.is_allowed_path(torrent):
|
||||
await hass.async_add_executor_job(coordinator.api.add_torrent, torrent)
|
||||
if download_path:
|
||||
await hass.async_add_executor_job(
|
||||
partial(
|
||||
coordinator.api.add_torrent, torrent, download_dir=download_path
|
||||
)
|
||||
)
|
||||
else:
|
||||
await hass.async_add_executor_job(coordinator.api.add_torrent, torrent)
|
||||
await coordinator.async_request_refresh()
|
||||
else:
|
||||
_LOGGER.warning("Could not add torrent: unsupported type or no permission")
|
||||
|
|
|
@ -40,6 +40,7 @@ STATE_ATTR_TORRENT_INFO = "torrent_info"
|
|||
|
||||
ATTR_DELETE_DATA = "delete_data"
|
||||
ATTR_TORRENT = "torrent"
|
||||
ATTR_DOWNLOAD_PATH = "download_path"
|
||||
|
||||
SERVICE_ADD_TORRENT = "add_torrent"
|
||||
SERVICE_REMOVE_TORRENT = "remove_torrent"
|
||||
|
|
|
@ -102,7 +102,12 @@ class TransmissionDataUpdateCoordinator(DataUpdateCoordinator[SessionStats]):
|
|||
for torrent in current_completed_torrents:
|
||||
if torrent.id not in old_completed_torrents:
|
||||
self.hass.bus.fire(
|
||||
EVENT_DOWNLOADED_TORRENT, {"name": torrent.name, "id": torrent.id}
|
||||
EVENT_DOWNLOADED_TORRENT,
|
||||
{
|
||||
"name": torrent.name,
|
||||
"id": torrent.id,
|
||||
"download_path": torrent.download_dir,
|
||||
},
|
||||
)
|
||||
|
||||
self._completed_torrents = current_completed_torrents
|
||||
|
@ -118,7 +123,12 @@ class TransmissionDataUpdateCoordinator(DataUpdateCoordinator[SessionStats]):
|
|||
for torrent in current_started_torrents:
|
||||
if torrent.id not in old_started_torrents:
|
||||
self.hass.bus.fire(
|
||||
EVENT_STARTED_TORRENT, {"name": torrent.name, "id": torrent.id}
|
||||
EVENT_STARTED_TORRENT,
|
||||
{
|
||||
"name": torrent.name,
|
||||
"id": torrent.id,
|
||||
"download_path": torrent.download_dir,
|
||||
},
|
||||
)
|
||||
|
||||
self._started_torrents = current_started_torrents
|
||||
|
@ -130,7 +140,12 @@ class TransmissionDataUpdateCoordinator(DataUpdateCoordinator[SessionStats]):
|
|||
for torrent in self._all_torrents:
|
||||
if torrent.id not in current_torrents:
|
||||
self.hass.bus.fire(
|
||||
EVENT_REMOVED_TORRENT, {"name": torrent.name, "id": torrent.id}
|
||||
EVENT_REMOVED_TORRENT,
|
||||
{
|
||||
"name": torrent.name,
|
||||
"id": torrent.id,
|
||||
"download_path": torrent.download_dir,
|
||||
},
|
||||
)
|
||||
|
||||
self._all_torrents = self.torrents.copy()
|
||||
|
|
|
@ -9,6 +9,11 @@ add_torrent:
|
|||
example: http://releases.ubuntu.com/19.04/ubuntu-19.04-desktop-amd64.iso.torrent
|
||||
selector:
|
||||
text:
|
||||
download_path:
|
||||
required: false
|
||||
example: "/path/to/download/directory"
|
||||
selector:
|
||||
text:
|
||||
|
||||
remove_torrent:
|
||||
fields:
|
||||
|
|
|
@ -101,6 +101,10 @@
|
|||
"torrent": {
|
||||
"name": "Torrent",
|
||||
"description": "URL, magnet link or Base64 encoded file."
|
||||
},
|
||||
"download_path": {
|
||||
"name": "Download path",
|
||||
"description": "Optional path to specify where the torrent should be downloaded. If not specified, the default download directory is used."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue