* Added support for alternate SSH ports in AsusWRT (#4832) * Always set the SSH port in SSH arguments * Removed whitespace * Adding port=22 to the mock calls * Changed config.get(CONF_PORT) to config[CONF_PORT] per feedback from @pvizeli
This commit is contained in:
parent
25408941de
commit
37a8035c54
2 changed files with 16 additions and 7 deletions
|
@ -16,7 +16,8 @@ import voluptuous as vol
|
|||
|
||||
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
|
||||
|
||||
|
@ -25,6 +26,7 @@ MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5)
|
|||
|
||||
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'
|
||||
|
@ -38,6 +40,7 @@ PLATFORM_SCHEMA = vol.All(
|
|||
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
|
||||
|
@ -112,12 +115,16 @@ class AsusWrtDeviceScanner(DeviceScanner):
|
|||
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
|
||||
|
@ -179,7 +186,7 @@ class AsusWrtDeviceScanner(DeviceScanner):
|
|||
|
||||
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
|
||||
|
|
|
@ -12,7 +12,7 @@ from homeassistant.components.device_tracker import (
|
|||
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)
|
||||
|
||||
|
@ -86,6 +86,7 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
|
|||
|
||||
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]))
|
||||
|
||||
|
@ -111,6 +112,7 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
|
|||
|
||||
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]))
|
||||
|
||||
|
@ -136,7 +138,7 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
|
|||
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):
|
||||
|
@ -161,7 +163,7 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
|
|||
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): \
|
||||
|
|
Loading…
Add table
Reference in a new issue