TPLink Router Device Tracker

The code for using a tplink router as a device tracker has been removed from Home Assistant. However if you are still interested in using that functionality, I have moved the code into a custom component here.

I’m interested. Will it work on an access point?

I’ve quickly done some tests on mine, but couldn’t get the json loaded from data/map_access_wireless_client_grid.json. That could be from a missing referer though, cause it’s throwing me back to my login page…

For the record my AP is this one: TL-WA901ND

I’m not sure if it will work with that specific access point. I would suggest installing it and giving it a try. If it doesn’t work you could look into using ping or nmap.

Currently trying it out now, I can see the AP also uses a page named userRpm/WlanStationRpm.htm so fingers crossed, I’ restarting now

Too bad:

Couldn't fetch auth tokens! Response was: <body><script language="javaScript">window.parent.document.cookie="Authorization=;path=/"; window.parent.location.href = "http://192.168.x.x"; </script></body></html>
7:57 PM custom_components/tplink_router/device_tracker.py (ERROR)
AP didn't respond with JSON. Check if credentials are correct
7:57 PM custom_components/tplink_router/device_tracker.py (ERROR)

Was worth a try > ping and nmap just don’t cut it… I’ve even tried to do snmp on this AP, and it works but it does not contain the arp table / client list :roll_eyes:

Thanks for sharing anyway!

I’m looking at the WlanStationRpm.htm page

<!DOCTYPE html>
<SCRIPT language="javascript" type="text/javascript">
var wlanHostPara = new Array(
1, 1, 8, 5000, 5, 1, 0, 1, 1, 
0,0 );
</SCRIPT>
<!DOCTYPE html>
<SCRIPT language="javascript" type="text/javascript">
var hostList = new Array(
"94-EE-9F-XX-XX-XX", 5, 58456, 76165, 2, 
0,0 );
</SCRIPT>

I can see my mac in the new array being created, safe to say it does not get loaded from json I think?

I’m not too familiar with the code but at first glance, it seems like it tries several different types of scanners. And from what you said it seems your AP might use the Tplink1DeviceScanner which is the last one it tries (see the get_scanner function in the device_tracker.py file). You could try commenting out the other types of scanners and see if that helps.

Yeah seems to break on _get_auth_tokens line 382.

I’m not that great with python myself, I must warn… I’m still trying to get my head around {self.token} on line 426. Cause if I can figure that one out, I can dowload WlanStationRpm.htm.
Cause self.token is this ‘DHBVGBJAUNLSKMVB’ down here, but it changes every session
http://192.168.x.x/DHBVGBJAUNLSKMVB/userRpm/StatusRpm.htm

I think I can just parse that string from this url http://192.168.x.x/userRpm/LoginRpm.htm > it’s in there too. Should work but I’d rather figure out how it works first

couldn’t figure out how to pass my credentials to login, but I did see how they are ‘compiled’ into the cookie. If I re-use this I can get a list of my mac addresses like this (edited IP and cookie for privacy)

import re

from aiohttp.hdrs import (
    HOST,
    USER_AGENT,
    ACCEPT,
    ACCEPT_LANGUAGE,
    ACCEPT_ENCODING,
    CONNECTION,
    REFERER,
    COOKIE,
)
import requests

login_url = "http://192.168.0.1/userRpm/LoginRpm.htm?Save=Save"

header = {
    HOST: "192.168.0.1",
    USER_AGENT: "Mozilla/5.0 (X11; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0",
    ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    ACCEPT_LANGUAGE: "en-US,en;q=0.5",
    ACCEPT_ENCODING: "gzip, deflate",
    CONNECTION: "keep-alive",
    REFERER: "http://192.168.0.1/",
    COOKIE: "Authorization=Basic%20YWRtxxxxx0MDE5YxxxxxOGRhZjZlxxxxxxDFiY2Y%3D",
}

#load the login page
session = requests.session()
response = session.get(login_url, headers=header)
#print(response.content)
login_content = str(response.content)

#get Token
m = re.search('192.168.0.1/(.+?)/userRpm', login_content)
if m:
    token = m.group(1)
#print(token)

wlan_url = "http://192.168.0.1/" + token + "/userRpm/WlanStationRpm.htm?Page=1&vapIdx="
#print(wlan_url)

wlan_content = session.get(wlan_url, headers=header)
wlan_content = str(wlan_content.content)

mac_addresses = re.search('(?:[\da-fA-F]{2}[:\-]){5}[\da-fA-F]{2}' ,wlan_content)
print(mac_addresses)

session.close

the username and pasword are encoded like this:

var username = $("userName").value;				
var password = $("pcPassword").value;	
if(httpAutErrorArray[1] == 1)
{
	password = hex_md5($("pcPassword").value);	
}			
var auth = "Basic "+ Base64Encoding(username + ":" + password);
document.cookie = "Authorization="+escape(auth)+";path=/";
//location.href ="/userRpm/LoginRpm.htm?Save=Save";
$("loginForm").submit();

the hex_md5 and Base64Encoding are two lengthy functions in an ‘encrypt.js’ file which I won’t paste out here, too long :grin:

Hi, I would love to use the tracker over my TP Link router Archer C6, unfortunately I get an error when the script is called:
File “/config/custom_components/tplink_router/device_tracker.py”, line 283, in _get_auth_tokens self.stok = response.json().get(“data”).get(“stok”) AttributeError: ‘str’ object has no attribute ‘get’

I am not very familiar with python, so I would appreciate any help. Thank you!

@cby016 - is this integration still working? I’m getting weird errors such as:

Platform error: device_tracker - Requirements for tplink_router not found: [‘pyHS100==0.3.5’].

2:55:48 AM – config.py (ERROR)

Unable to install package pyHS100==0.3.5: ERROR: Could not find a version that satisfies the requirement typing==1000000000.0.0 (from -c /usr/src/homeassistant/homeassistant/package_constraints.txt (line 55)) (from versions: 3.5.0b1, 3.5.0, 3.5.0.1, 3.5.1.0, 3.5.2.2, 3.5.3.0, 3.6.1, 3.6.2, 3.6.4, 3.6.6, 3.7.4, 3.7.4.1, 3.7.4.3) ERROR: No matching distribution found for typing==1000000000.0.0 (from -c /usr/src/homeassistant/homeassistant/package_constraints.txt (line 55)) WARNING: You are using pip version 20.2.4; however, version 20.3.1 is available. You should consider upgrading via the ‘/usr/local/bin/python3 -m pip install --upgrade pip’ command.

2:55:48 AM – util/package.py (ERROR)

You are using a custom integration for tplink_router which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant.

2:55:42 AM – loader.py (WARNING)

EDIT: OK I have managed to pip install pyHS100, but now get this:

2020-12-13 03:03:44 ERROR (SyncWorker_4) [custom_components.tplink_router.device_tracker] AP didn’t respond with JSON. Check if credentials are correct

2020-12-13 03:03:44 ERROR (SyncWorker_4) [custom_components.tplink_router.device_tracker] Couldn’t fetch auth tokens! Response was: 500 Internal Server Error

500 Internal Server Error

2020-12-13 03:03:44 ERROR (SyncWorker_4) [custom_components.tplink_router.device_tracker] Router didn’t respond with JSON. Check if credentials are correct

2020-12-13 03:03:44 ERROR (SyncWorker_4) [custom_components.tplink_router.device_tracker] Router didn’t respond with JSON. Check if credentials are correct.

2020-12-13 03:03:44 ERROR (MainThread) [homeassistant.components.device_tracker] Error setting up platform legacy

my username (email) and password are 100% correct.

Is the support for the TP-Link router officially being banned? I got the error when I set everything according to the instruction.

Platform error: device_tracker - Integration 'tplink_router' not found.

How come it cannot find the integration if it sits within the custom_component folder?

The manifest.json file is missing the version key…this is a requirement for integrations now (new since the last time the tp-link code was updated). If you update the file and add the version key as below it will be found (don’t forget the comma on the line above). However it will fail at that point due to missing dependencies…

{
“domain”: “tplink_router”,
“name”: “TP Link Router Device Tracker”,
“documentation”: “”,
“dependencies”: [],
“codeowners”: [],
“requirements”: [“pyHS100==0.3.5”, “tplink==0.2.1”],
“version”: “1.01”
}

Thank you for the info!
That’s a sad story… Are there any alternatives for TP-Link routers now?

It depends on what model you have. I also tried GitHub - menahishayan/Home-Assistant-TP-Link-Router: Home Assistant component for complete router administration of the TP Link Archer C50 and the TP Link N600 but it didn’t work for my model (the EAP225 outdoor). Good luck!

That would mean a generic solution for TP-Link routers doesn’t exist so far. Much appreciated!

Once the config.yaml is edited, how do you actually use this tracker? where are the devices scanned being stored?

I am not sure if my credentials are working: I am using the encrypted version. Where can. Ifind any errors about this tracker?

thanks

I’ve created a HACs integration device tracker for TP-Link Deco X60 at GitHub - amosyuen/ha-tplink-deco: Home Assistant TP-Link Deco Custom Component. Untested with other decos, but if the API implementation is similar should work with them.