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

Added support for alternate SSH ports in AsusWRT (#4832) #6109

Merged
merged 5 commits into from Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions homeassistant/components/device_tracker/asuswrt.py
Expand Up @@ -16,7 +16,8 @@

from homeassistant.components.device_tracker import (
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_USERNAME, CONF_PORT)
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv

Expand All @@ -25,6 +26,7 @@

CONF_PROTOCOL = 'protocol'
CONF_MODE = 'mode'
DEFAULT_SSH_PORT = 22
CONF_SSH_KEY = 'ssh_key'
CONF_PUB_KEY = 'pub_key'
SECRET_GROUP = 'Password or SSH Key'
Expand All @@ -38,6 +40,7 @@
vol.In(['ssh', 'telnet']),
vol.Optional(CONF_MODE, default='router'):
vol.In(['router', 'ap']),
vol.Optional(CONF_PORT, default=DEFAULT_SSH_PORT): cv.port,
vol.Exclusive(CONF_PASSWORD, SECRET_GROUP): cv.string,
vol.Exclusive(CONF_SSH_KEY, SECRET_GROUP): cv.isfile,
vol.Exclusive(CONF_PUB_KEY, SECRET_GROUP): cv.isfile
Expand Down Expand Up @@ -112,12 +115,16 @@ def __init__(self, config):
self.ssh_key = config.get('ssh_key', config.get('pub_key', ''))
self.protocol = config[CONF_PROTOCOL]
self.mode = config[CONF_MODE]
self.port = config[CONF_PORT]
self.ssh_args = {}

if self.protocol == 'ssh':

self.ssh_args['port'] = self.port
if self.ssh_key:
self.ssh_secret = {'ssh_key': self.ssh_key}
self.ssh_args['ssh_key'] = self.ssh_key
elif self.password:
self.ssh_secret = {'password': self.password}
self.ssh_args['password'] = self.password
else:
_LOGGER.error('No password or private key specified')
self.success_init = False
Expand Down Expand Up @@ -179,7 +186,7 @@ def ssh_connection(self):

ssh = pxssh.pxssh()
try:
ssh.login(self.host, self.username, **self.ssh_secret)
ssh.login(self.host, self.username, **self.ssh_args)
except exceptions.EOF as err:
_LOGGER.error('Connection refused. Is SSH enabled?')
return None
Expand Down
8 changes: 5 additions & 3 deletions tests/components/device_tracker/test_asuswrt.py
Expand Up @@ -12,7 +12,7 @@
CONF_CONSIDER_HOME, CONF_TRACK_NEW)
from homeassistant.components.device_tracker.asuswrt import (
CONF_PROTOCOL, CONF_MODE, CONF_PUB_KEY, DOMAIN,
PLATFORM_SCHEMA)
CONF_PORT, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_PLATFORM, CONF_PASSWORD, CONF_USERNAME,
CONF_HOST)

Expand Down Expand Up @@ -86,6 +86,7 @@ def test_get_scanner_with_password_no_pubkey(self, asuswrt_mock): \

conf_dict[DOMAIN][CONF_MODE] = 'router'
conf_dict[DOMAIN][CONF_PROTOCOL] = 'ssh'
conf_dict[DOMAIN][CONF_PORT] = 22
self.assertEqual(asuswrt_mock.call_count, 1)
self.assertEqual(asuswrt_mock.call_args, mock.call(conf_dict[DOMAIN]))

Expand All @@ -111,6 +112,7 @@ def test_get_scanner_with_pubkey_no_password(self, asuswrt_mock): \

conf_dict[DOMAIN][CONF_MODE] = 'router'
conf_dict[DOMAIN][CONF_PROTOCOL] = 'ssh'
conf_dict[DOMAIN][CONF_PORT] = 22
self.assertEqual(asuswrt_mock.call_count, 1)
self.assertEqual(asuswrt_mock.call_args, mock.call(conf_dict[DOMAIN]))

Expand All @@ -136,7 +138,7 @@ def test_ssh_login_with_pub_key(self):
self.assertEqual(ssh.login.call_count, 1)
self.assertEqual(
ssh.login.call_args,
mock.call('fake_host', 'fake_user', ssh_key=FAKEFILE)
mock.call('fake_host', 'fake_user', port=22, ssh_key=FAKEFILE)
)

def test_ssh_login_with_password(self):
Expand All @@ -161,7 +163,7 @@ def test_ssh_login_with_password(self):
self.assertEqual(ssh.login.call_count, 1)
self.assertEqual(
ssh.login.call_args,
mock.call('fake_host', 'fake_user', password='fake_pass')
mock.call('fake_host', 'fake_user', password='fake_pass', port=22)
)

def test_ssh_login_without_password_or_pubkey(self): \
Expand Down