Skip to content

Commit

Permalink
fixes #11848 (#11915)
Browse files Browse the repository at this point in the history
Adding tests to check the component after latest patch
  • Loading branch information
kennedyshead authored and balloob committed Jan 27, 2018
1 parent df24ecf commit 280c160
Show file tree
Hide file tree
Showing 3 changed files with 296 additions and 48 deletions.
1 change: 0 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ omit =
homeassistant/components/cover/scsgate.py
homeassistant/components/device_tracker/actiontec.py
homeassistant/components/device_tracker/aruba.py
homeassistant/components/device_tracker/asuswrt.py
homeassistant/components/device_tracker/automatic.py
homeassistant/components/device_tracker/bbox.py
homeassistant/components/device_tracker/bluetooth_le_tracker.py
Expand Down
61 changes: 21 additions & 40 deletions homeassistant/components/device_tracker/asuswrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@

CONF_PUB_KEY = 'pub_key'
CONF_SSH_KEY = 'ssh_key'

DEFAULT_SSH_PORT = 22

SECRET_GROUP = 'Password or SSH Key'

PLATFORM_SCHEMA = vol.All(
Expand Down Expand Up @@ -118,20 +116,10 @@ def __init__(self, config):
self.port = config[CONF_PORT]

if self.protocol == 'ssh':
if not (self.ssh_key or self.password):
_LOGGER.error("No password or private key specified")
self.success_init = False
return

self.connection = SshConnection(
self.host, self.port, self.username, self.password,
self.ssh_key, self.mode == 'ap')
else:
if not self.password:
_LOGGER.error("No password specified")
self.success_init = False
return

self.connection = TelnetConnection(
self.host, self.port, self.username, self.password,
self.mode == 'ap')
Expand Down Expand Up @@ -177,11 +165,16 @@ def get_asuswrt_data(self):
"""
devices = {}
devices.update(self._get_wl())
devices = self._get_arp(devices)
devices = self._get_neigh(devices)
devices.update(self._get_arp())
devices.update(self._get_neigh(devices))
if not self.mode == 'ap':
devices.update(self._get_leases(devices))
return devices

ret_devices = {}
for key in devices:
if devices[key].ip is not None:
ret_devices[key] = devices[key]
return ret_devices

def _get_wl(self):
lines = self.connection.run_command(_WL_CMD)
Expand Down Expand Up @@ -219,18 +212,13 @@ def _get_neigh(self, cur_devices):
result = _parse_lines(lines, _IP_NEIGH_REGEX)
devices = {}
for device in result:
if device['mac']:
if device['mac'] is not None:
mac = device['mac'].upper()
devices[mac] = Device(mac, None, None)
else:
cur_devices = {
k: v for k, v in
cur_devices.items() if v.ip != device['ip']
}
cur_devices.update(devices)
return cur_devices

def _get_arp(self, cur_devices):
old_ip = cur_devices.get(mac, {}).ip or None
devices[mac] = Device(mac, device.get('ip', old_ip), None)
return devices

def _get_arp(self):
lines = self.connection.run_command(_ARP_CMD)
if not lines:
return {}
Expand All @@ -240,13 +228,7 @@ def _get_arp(self, cur_devices):
if device['mac']:
mac = device['mac'].upper()
devices[mac] = Device(mac, device['ip'], None)
else:
cur_devices = {
k: v for k, v in
cur_devices.items() if v.ip != device['ip']
}
cur_devices.update(devices)
return cur_devices
return devices


class _Connection:
Expand All @@ -272,7 +254,7 @@ class SshConnection(_Connection):

def __init__(self, host, port, username, password, ssh_key, ap):
"""Initialize the SSH connection properties."""
super(SshConnection, self).__init__()
super().__init__()

self._ssh = None
self._host = host
Expand Down Expand Up @@ -322,7 +304,7 @@ def connect(self):
self._ssh.login(self._host, self._username,
password=self._password, port=self._port)

super(SshConnection, self).connect()
super().connect()

def disconnect(self): \
# pylint: disable=broad-except
Expand All @@ -334,15 +316,15 @@ def disconnect(self): \
finally:
self._ssh = None

super(SshConnection, self).disconnect()
super().disconnect()


class TelnetConnection(_Connection):
"""Maintains a Telnet connection to an ASUS-WRT router."""

def __init__(self, host, port, username, password, ap):
"""Initialize the Telnet connection properties."""
super(TelnetConnection, self).__init__()
super().__init__()

self._telnet = None
self._host = host
Expand All @@ -361,7 +343,6 @@ def run_command(self, command):
try:
if not self.connected:
self.connect()

self._telnet.write('{}\n'.format(command).encode('ascii'))
data = (self._telnet.read_until(self._prompt_string).
split(b'\n')[1:-1])
Expand Down Expand Up @@ -392,7 +373,7 @@ def connect(self):
self._telnet.write((self._password + '\n').encode('ascii'))
self._prompt_string = self._telnet.read_until(b'#').split(b'\n')[-1]

super(TelnetConnection, self).connect()
super().connect()

def disconnect(self): \
# pylint: disable=broad-except
Expand All @@ -402,4 +383,4 @@ def disconnect(self): \
except Exception:
pass

super(TelnetConnection, self).disconnect()
super().disconnect()
Loading

0 comments on commit 280c160

Please sign in to comment.