Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config flow to transmission #26434

Merged
merged 13 commits into from Sep 26, 2019
18 changes: 8 additions & 10 deletions homeassistant/components/transmission/__init__.py
Expand Up @@ -15,6 +15,7 @@
CONF_SCAN_INTERVAL,
CONF_USERNAME,
)
from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.dispatcher import dispatcher_send
Expand Down Expand Up @@ -48,7 +49,7 @@
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
): cv.positive_int,
): cv.time_period,
}
)
},
Expand Down Expand Up @@ -99,7 +100,7 @@ async def async_unload_entry(hass, entry):
return True
engrbm87 marked this conversation as resolved.
Show resolved Hide resolved


def get_api(host, port, username=None, password=None):
async def get_api(host, port, username=None, password=None):
"""Get Transmission client."""
try:
api = transmissionrpc.Client(host, port=port, user=username, password=password)
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -142,15 +143,15 @@ async def async_setup(self):
CONF_USERNAME: self.config_entry.data.get(CONF_USERNAME),
CONF_PASSWORD: self.config_entry.data.get(CONF_PASSWORD),
}
api = get_api(**config)
api = await get_api(**config)
if not api:
return False

self.tm_data = self.hass.data[DOMAIN][DATA_TRANSMISSION] = TransmissionData(
self.hass, self.config_entry, api
)

self.tm_data.init_torrent_list()
self.hass.async_add_executor_job(self.tm_data.init_torrent_list)
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
self.hass.async_add_executor_job(self.tm_data.update)
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
self.set_scan_interval(self.scan_interval)

Expand All @@ -161,13 +162,13 @@ async def async_setup(self):
)
)

def add_torrent(self, service):
def add_torrent(service):
"""Add new torrent to download."""
torrent = service.data[ATTR_TORRENT]
if torrent.startswith(
("http", "ftp:", "magnet:")
) or self.hass.config.is_allowed_path(torrent):
self.api.add_torrent(torrent)
api.add_torrent(torrent)
else:
_LOGGER.warning(
"Could not add torrent: unsupported type or no permission"
Expand All @@ -184,6 +185,7 @@ def add_torrent(self, service):
def set_scan_interval(self, scan_interval):
"""Update scan interval."""

@callback
def refresh(event_time):
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
"""Get the latest data from Transmission."""
self.hass.async_add_executor_job(self.tm_data.update)
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -216,10 +218,6 @@ def __init__(self, hass, config, api):
self.completed_torrents = []
self.started_torrents = []

def request_update(self):
"""Request immeidate update."""
self.hass.async_add_executor_job(self.update)

def update(self):
"""Get the latest data from Transmission instance."""
try:
Expand Down
6 changes: 2 additions & 4 deletions homeassistant/components/transmission/config_flow.py
Expand Up @@ -43,7 +43,7 @@ async def async_step_user(self, user_input=None):

self.config[CONF_NAME] = user_input.pop(CONF_NAME)
try:
get_api(**user_input)
await get_api(**user_input)
self.config.update(user_input)
if "options" not in self.config:
self.config["options"] = {CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL}
Expand Down Expand Up @@ -73,9 +73,7 @@ async def async_step_user(self, user_input=None):
async def async_step_import(self, import_config):
"""Import from Transmission client config."""
self.config["options"] = {
CONF_SCAN_INTERVAL: import_config.pop(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
)
CONF_SCAN_INTERVAL: import_config.pop(CONF_SCAN_INTERVAL).seconds
}

return await self.async_step_user(user_input=import_config)
Expand Down
12 changes: 4 additions & 8 deletions homeassistant/components/transmission/switch.py
Expand Up @@ -23,12 +23,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
name = config_entry.data[CONF_NAME]

dev = []
for switch_type in SWITCH_TYPES:
dev.append(
TransmissionSwitch(
switch_type, SWITCH_TYPES[switch_type], transmission_api, name
)
)
for switch_type, switch_name in SWITCH_TYPES.items():
dev.append(TransmissionSwitch(switch_type, switch_name, transmission_api, name))

async_add_entities(dev, True)

Expand Down Expand Up @@ -73,7 +69,7 @@ def turn_on(self, **kwargs):
elif self.type == "turtle_mode":
_LOGGING.debug("Turning Turtle Mode of Transmission on")
self._transmission_api.set_alt_speed_enabled(True)
self._transmission_api.request_update()
self._transmission_api.update()

def turn_off(self, **kwargs):
"""Turn the device off."""
Expand All @@ -83,7 +79,7 @@ def turn_off(self, **kwargs):
if self.type == "turtle_mode":
_LOGGING.debug("Turning Turtle Mode of Transmission off")
self._transmission_api.set_alt_speed_enabled(False)
self._transmission_api.request_update()
self._transmission_api.update()

async def async_added_to_hass(self):
"""Handle entity which will be added."""
Expand Down