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 3 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.get(CONF_PORT, 22)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use only config[CONF_PORT] you have set default on vol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pvizeli Thanks for the feedback. Commited this change.

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
4 changes: 3 additions & 1 deletion 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 Down