diff --git a/homeassistant/components/transmission/__init__.py b/homeassistant/components/transmission/__init__.py index b1837708919..d020bfe9745 100644 --- a/homeassistant/components/transmission/__init__.py +++ b/homeassistant/components/transmission/__init__.py @@ -40,6 +40,8 @@ from .const import ( EVENT_STARTED_TORRENT, SERVICE_ADD_TORRENT, SERVICE_REMOVE_TORRENT, + SERVICE_START_TORRENT, + SERVICE_STOP_TORRENT, ) 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( vol.Schema( { @@ -116,6 +132,8 @@ async def async_unload_entry(hass, config_entry): if not hass.data[DOMAIN]: hass.services.async_remove(DOMAIN, SERVICE_ADD_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 @@ -207,6 +225,34 @@ class TransmissionClient: "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): """Remove torrent.""" tm_client = None @@ -233,6 +279,20 @@ class TransmissionClient: 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) return True diff --git a/homeassistant/components/transmission/const.py b/homeassistant/components/transmission/const.py index 960fd7a65b4..185148f3bd9 100644 --- a/homeassistant/components/transmission/const.py +++ b/homeassistant/components/transmission/const.py @@ -36,6 +36,8 @@ ATTR_TORRENT = "torrent" SERVICE_ADD_TORRENT = "add_torrent" SERVICE_REMOVE_TORRENT = "remove_torrent" +SERVICE_START_TORRENT = "start_torrent" +SERVICE_STOP_TORRENT = "stop_torrent" DATA_UPDATED = "transmission_data_updated" diff --git a/homeassistant/components/transmission/services.yaml b/homeassistant/components/transmission/services.yaml index e8114b680ab..04ac5472d4c 100644 --- a/homeassistant/components/transmission/services.yaml +++ b/homeassistant/components/transmission/services.yaml @@ -20,3 +20,23 @@ remove_torrent: delete_data: description: Delete torrent data 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