Skip to content
Closed
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
79 changes: 76 additions & 3 deletions homeassistant/components/device_tracker/tplink.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hashlib
import logging
import re
import html

from aiohttp.hdrs import (
ACCEPT, COOKIE, PRAGMA, REFERER, CONNECTION, KEEP_ALIVE, USER_AGENT,
Expand All @@ -35,9 +36,9 @@

def get_scanner(hass, config):
"""Validate the configuration and return a TP-Link scanner."""
for cls in [Tplink5DeviceScanner, Tplink4DeviceScanner,
Tplink3DeviceScanner, Tplink2DeviceScanner,
TplinkDeviceScanner]:
for cls in [TplinkEAPControllerDeviceScanner, Tplink5DeviceScanner,
Tplink4DeviceScanner, Tplink3DeviceScanner,
Tplink2DeviceScanner, TplinkDeviceScanner]:
scanner = cls(config[DOMAIN])
if scanner.success_init:
return scanner
Expand Down Expand Up @@ -412,3 +413,75 @@ def _update_info(self):
return True

return False


class TplinkEAPControllerDeviceScanner(TplinkDeviceScanner):
"""This class queries a TP-Link EAP Controller Server"""
_LOGGER.info("Using EAP Controller Scanner")

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

def get_device_name(self, device):
"""Get firmware doesn't save the name of the wireless device."""
return self.last_results.get(device)['name']

def get_extra_attributes(self, device):
return self.last_results.get(device)

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

Return boolean if scanning successful.
"""

base_url = 'https://{}:8043'.format(self.host)

# Create a session to handle cookie easier
session = requests.session()
session.verify = False

login = session.post('{}/login'.format(base_url),
data=(('name', self.username),
('password', self.password)))
if(login.status_code != 200):
_LOGGER.error('HTTP Request failed with status {}'
.format(login.status_code))
else:
json = login.json()
if not json['success']:
_LOGGER.error('Login failed, response was: '+json['message'])
else:
_LOGGER.info("Loading wireless clients...")
client_list_url = '{}/monitor/allActiveClients'
.format(base_url)
post_data = (('currentPage', 1), ('currentPageSize', 1000))

clients = session.post(client_list_url, data=post_data)

try:
list_of_devices = clients.json()
except ValueError:
_LOGGER.error("AP didn't respond with JSON. "
"Check if credentials are correct")
return False

session.close()

if list_of_devices:
self.last_results = {
device['mac'].replace('-', ':'): {
'mac': device['mac'].replace('-', ':'),
'name': device['name'],
'ap': html.unescape(device['apName']),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

undefined name 'html'

'ssid': device['ssid'],
'snr': device['snr'],
'ip': device['ip']
}
for device in list_of_devices['data']
}
return True

return False