Add start torrent and stop torrent service for transmission integration (#43920)

This commit is contained in:
Julien Roy 2021-01-13 17:44:57 +01:00 committed by GitHub
parent e05bb7e858
commit 94417e3e14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 0 deletions

View File

@ -40,6 +40,8 @@ from .const import (
EVENT_STARTED_TORRENT, EVENT_STARTED_TORRENT,
SERVICE_ADD_TORRENT, SERVICE_ADD_TORRENT,
SERVICE_REMOVE_TORRENT, SERVICE_REMOVE_TORRENT,
SERVICE_START_TORRENT,
SERVICE_STOP_TORRENT,
) )
from .errors import AuthenticationError, CannotConnect, UnknownError from .errors import AuthenticationError, CannotConnect, UnknownError
@ -58,6 +60,20 @@ SERVICE_REMOVE_TORRENT_SCHEMA = vol.Schema(
} }
) )
SERVICE_START_TORRENT_SCHEMA = vol.Schema(
{
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_ID): cv.positive_int,
}
)
SERVICE_STOP_TORRENT_SCHEMA = vol.Schema(
{
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_ID): cv.positive_int,
}
)
TRANS_SCHEMA = vol.All( TRANS_SCHEMA = vol.All(
vol.Schema( vol.Schema(
{ {
@ -116,6 +132,8 @@ async def async_unload_entry(hass, config_entry):
if not hass.data[DOMAIN]: if not hass.data[DOMAIN]:
hass.services.async_remove(DOMAIN, SERVICE_ADD_TORRENT) hass.services.async_remove(DOMAIN, SERVICE_ADD_TORRENT)
hass.services.async_remove(DOMAIN, SERVICE_REMOVE_TORRENT) hass.services.async_remove(DOMAIN, SERVICE_REMOVE_TORRENT)
hass.services.async_remove(DOMAIN, SERVICE_START_TORRENT)
hass.services.async_remove(DOMAIN, SERVICE_STOP_TORRENT)
return True return True
@ -207,6 +225,34 @@ class TransmissionClient:
"Could not add torrent: unsupported type or no permission" "Could not add torrent: unsupported type or no permission"
) )
def start_torrent(service):
"""Start torrent."""
tm_client = None
for entry in self.hass.config_entries.async_entries(DOMAIN):
if entry.data[CONF_NAME] == service.data[CONF_NAME]:
tm_client = self.hass.data[DOMAIN][entry.entry_id]
break
if tm_client is None:
_LOGGER.error("Transmission instance is not found")
return
torrent_id = service.data[CONF_ID]
tm_client.tm_api.start_torrent(torrent_id)
tm_client.api.update()
def stop_torrent(service):
"""Stop torrent."""
tm_client = None
for entry in self.hass.config_entries.async_entries(DOMAIN):
if entry.data[CONF_NAME] == service.data[CONF_NAME]:
tm_client = self.hass.data[DOMAIN][entry.entry_id]
break
if tm_client is None:
_LOGGER.error("Transmission instance is not found")
return
torrent_id = service.data[CONF_ID]
tm_client.tm_api.stop_torrent(torrent_id)
tm_client.api.update()
def remove_torrent(service): def remove_torrent(service):
"""Remove torrent.""" """Remove torrent."""
tm_client = None tm_client = None
@ -233,6 +279,20 @@ class TransmissionClient:
schema=SERVICE_REMOVE_TORRENT_SCHEMA, schema=SERVICE_REMOVE_TORRENT_SCHEMA,
) )
self.hass.services.async_register(
DOMAIN,
SERVICE_START_TORRENT,
start_torrent,
schema=SERVICE_START_TORRENT_SCHEMA,
)
self.hass.services.async_register(
DOMAIN,
SERVICE_STOP_TORRENT,
stop_torrent,
schema=SERVICE_STOP_TORRENT_SCHEMA,
)
self.config_entry.add_update_listener(self.async_options_updated) self.config_entry.add_update_listener(self.async_options_updated)
return True return True

View File

@ -36,6 +36,8 @@ ATTR_TORRENT = "torrent"
SERVICE_ADD_TORRENT = "add_torrent" SERVICE_ADD_TORRENT = "add_torrent"
SERVICE_REMOVE_TORRENT = "remove_torrent" SERVICE_REMOVE_TORRENT = "remove_torrent"
SERVICE_START_TORRENT = "start_torrent"
SERVICE_STOP_TORRENT = "stop_torrent"
DATA_UPDATED = "transmission_data_updated" DATA_UPDATED = "transmission_data_updated"

View File

@ -20,3 +20,23 @@ remove_torrent:
delete_data: delete_data:
description: Delete torrent data description: Delete torrent data
example: false example: false
start_torrent:
description: Start a torrent
fields:
name:
description: Instance name as entered during entry config
example: Transmission
id:
description: ID of a torrent
example: 123
stop_torrent:
description: Stop a torrent
fields:
name:
description: Instance name as entered during entry config
example: Transmission
id:
description: ID of a torrent
example: 123