Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 62 additions & 8 deletions homeassistant/components/device_tracker/tplink.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
CONF_HOST, CONF_PASSWORD, CONF_USERNAME, HTTP_HEADER_X_REQUESTED_WITH)
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['tplink==0.2.1']

_LOGGER = logging.getLogger(__name__)

HTTP_HEADER_NO_CACHE = 'no-cache'
Expand All @@ -34,10 +36,22 @@


def get_scanner(hass, config):
"""Validate the configuration and return a TP-Link scanner."""
for cls in [Tplink5DeviceScanner, Tplink4DeviceScanner,
Tplink3DeviceScanner, Tplink2DeviceScanner,
TplinkDeviceScanner]:
"""
Validate the configuration and return a TP-Link scanner.

The default way of integrating devices is to use a pypi

package, The TplinkDeviceScanner has been refactored

to depend on a pypi package, the other implementations

should be gradually migrated in the pypi package

"""
for cls in [
TplinkDeviceScanner, Tplink5DeviceScanner, Tplink4DeviceScanner,
Tplink3DeviceScanner, Tplink2DeviceScanner, Tplink1DeviceScanner
]:
scanner = cls(config[DOMAIN])
if scanner.success_init:
return scanner
Expand All @@ -46,6 +60,46 @@ def get_scanner(hass, config):


class TplinkDeviceScanner(DeviceScanner):
"""Queries the router for connected devices."""

def __init__(self, config):
"""Initialize the scanner."""
from tplink.tplink import TpLinkClient
host = config[CONF_HOST]
password = config[CONF_PASSWORD]
username = config[CONF_USERNAME]

self.tplink_client = TpLinkClient(
password, host=host, username=username)

self.last_results = {}
self.success_init = self._update_info()

def scan_devices(self):
"""Scan for new devices and return a list with found device IDs."""
self._update_info()
return self.last_results.keys()

def get_device_name(self, device):
"""Get the name of the device."""
return self.last_results.get(device)

def _update_info(self):
"""Ensure the information from the TP-Link router is up to date.

Return boolean if scanning successful.
"""
_LOGGER.info("Loading wireless clients...")
result = self.tplink_client.get_connected_devices()

if result:
self.last_results = result
return True

return False


class Tplink1DeviceScanner(DeviceScanner):
"""This class queries a wireless router running TP-Link firmware."""

def __init__(self, config):
Expand Down Expand Up @@ -94,7 +148,7 @@ def _update_info(self):
return False


class Tplink2DeviceScanner(TplinkDeviceScanner):
class Tplink2DeviceScanner(Tplink1DeviceScanner):
"""This class queries a router with newer version of TP-Link firmware."""

def scan_devices(self):
Expand Down Expand Up @@ -147,7 +201,7 @@ def _update_info(self):
return False


class Tplink3DeviceScanner(TplinkDeviceScanner):
class Tplink3DeviceScanner(Tplink1DeviceScanner):
"""This class queries the Archer C9 router with version 150811 or high."""

def __init__(self, config):
Expand Down Expand Up @@ -256,7 +310,7 @@ def _log_out(self):
self.sysauth = ''


class Tplink4DeviceScanner(TplinkDeviceScanner):
class Tplink4DeviceScanner(Tplink1DeviceScanner):
"""This class queries an Archer C7 router with TP-Link firmware 150427."""

def __init__(self, config):
Expand Down Expand Up @@ -337,7 +391,7 @@ def _update_info(self):
return True


class Tplink5DeviceScanner(TplinkDeviceScanner):
class Tplink5DeviceScanner(Tplink1DeviceScanner):
"""This class queries a TP-Link EAP-225 AP with newer TP-Link FW."""

def scan_devices(self):
Expand Down
3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,9 @@ toonlib==1.0.2
# homeassistant.components.alarm_control_panel.totalconnect
total_connect_client==0.18

# homeassistant.components.device_tracker.tplink
tplink==0.2.1

# homeassistant.components.sensor.transmission
# homeassistant.components.switch.transmission
transmissionrpc==0.11
Expand Down