Skip to content

Commit

Permalink
Add add_torrent service to Transmission (home-assistant#25144)
Browse files Browse the repository at this point in the history
* Add add_torrent service to Transmission

* Fix services.yaml format

* Verify that torrent is whitelisted

* Add logging if adding failed

* Change warn to warning
  • Loading branch information
postlund authored and pvizeli committed Jul 21, 2019
1 parent 0be2dad commit 797196d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions homeassistant/components/transmission/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@

DEFAULT_SCAN_INTERVAL = timedelta(seconds=120)

ATTR_TORRENT = 'torrent'

SERVICE_ADD_TORRENT = 'add_torrent'

SERVICE_ADD_TORRENT_SCHEMA = vol.Schema({
vol.Required(ATTR_TORRENT): cv.string,
})

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_HOST): cv.string,
Expand Down Expand Up @@ -82,6 +90,19 @@ def refresh(event_time):

track_time_interval(hass, refresh, scan_interval)

def add_torrent(service):
"""Add new torrent to download."""
torrent = service.data[ATTR_TORRENT]
if torrent.startswith(('http', 'ftp:', 'magnet:')) or \
hass.config.is_allowed_path(torrent):
api.add_torrent(torrent)
else:
_LOGGER.warning('Could not add torrent: '
'unsupported type or no permission')

hass.services.register(DOMAIN, SERVICE_ADD_TORRENT, add_torrent,
schema=SERVICE_ADD_TORRENT_SCHEMA)

sensorconfig = {
'sensors': config[DOMAIN][CONF_MONITORED_CONDITIONS],
'client_name': config[DOMAIN][CONF_NAME]}
Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/transmission/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_torrent:
description: Add a new torrent to download (URL, magnet link or Base64 encoded).
fields:
torrent:
description: URL, magnet link or Base64 encoded file.
example: http://releases.ubuntu.com/19.04/ubuntu-19.04-desktop-amd64.iso.torrent}

0 comments on commit 797196d

Please sign in to comment.