import voluptuous as vol
import logging
import threading
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['transmissionrpc']
ATTR_URL = 'url'
CONF_HOST = 'host'
CONF_PORT = 'port'
CONF_USERNAME = 'username'
CONF_PASSWORD = 'password'
DOMAIN = 'transmissionctl'
SERVICE_DOWNLOAD = 'Download'
SERVICE_DOWNLOAD_SCHEMA = vol.Schema({
vol.Required(ATTR_URL): cv.string,
})
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_PORT): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
})
}, extra=vol.ALLOW_EXTRA)
def setup(hass, config):
host = config[DOMAIN][CONF_HOST]
port = config[DOMAIN][CONF_PORT]
username = config[DOMAIN][CONF_USERNAME]
password = config[DOMAIN][CONF_PASSWORD]
if host == None:
_LOGGER.error('TransmissionCtl:Pls enter host!')
return False
if port == None:
_LOGGER.error('TransmissionCtl:Pls enter host!')
port = 9091
if username == None:
_LOGGER.error('TransmissionCtl:Pls enter host!')
if username == None:
_LOGGER.error('TransmissionCtl:Pls enter host!')
import transmissionrpc
def transmission_download(service):
def add_download():
download_url = service.data[ATTR_URL]
tc = transmissionrpc.Client(host, port=port, user=username, password=password)
tc.add_torrent(download_url)
threading.Thread(target=add_download).start()
hass.services.register(DOMAIN, SERVICE_DOWNLOAD, transmission_download,schema=SERVICE_DOWNLOAD_SCHEMA)
return True
加了账号密码 |