almottier / TapoP100

A module for controlling the TP-Link Tapo P100 Plugs
MIT License
75 stars 23 forks source link

Authentication issue #2

Closed DrakarBarna closed 11 months ago

DrakarBarna commented 11 months ago

Hi, I'm using Domoticz to manage many automation in my house. I'm using many kind of different device and some of them are TP-Link brand (SmartPlug P100/P110, LightBulb L510/L610). To control TP-Link device, I have few Domoticz plugin base on the PyP100 library. Like many people, few weeks ago some device stop working properly when they are drive by Domoticz due to firmware upgrade (authentication vulnerability). Specially the P100 SmartPlug.

I've remove the initial PyP100 library of Fishbigger to install the PyP100 fork of Almottier. This made my P100 plug working again but now I have some strange issue with P110/L510 and L610 device. They all work fine for a certain time but after a random time, they stop working and I have to reboot the Domoticz server to make it run again. The log file show error related to authentication.

Trying to identify where the problem is, I made a small python script. import time from PyP100 import PyP100, PyP110 p100 = PyP100.P100("192.168.1.25", "myemail@gmail.com", "mypassword") print("Turn on") p100.turnOn() time.sleep(3) print("Turn off") p100.turnOff() time.sleep(3) print("get device info") pluginfo = p100.getDeviceInfo() print(pluginfo)

This script should turn on the plug, wait 3 second, turn off the plug, wait another 3 second and then display the device information.

When I run it, I have something strange. Run : python3 test-p110.py Turn on Failed to initialize protocol AuthProtocol Traceback (most recent call last): File "/usr/local/lib/python3.9/dist-packages/PyP100/PyP100.py", line 23, in _initialize protocol.Initialize() File "/usr/local/lib/python3.9/dist-packages/PyP100/auth_protocol.py", line 109, in Initialize raise Exception("Failed to authenticate") Exception: Failed to authenticate Turn off get device info {'device_id': '802810AB1BF7E1B4A384DBE43E7345EB30B42D13', 'fw_ver': '1.2.3 Build 230425 Rel.142542', 'hw_ver': '1.0', 'type': 'SMART.TAPOPLUG', 'model': 'P110', 'mac': '30-DE-4B-B7-3D-45', 'hw_id': '2FB30EF5BF220C44026401D373C6B55B', 'fw_id': '00000000000000000000000000000000', 'oem_id': '18BDC6C774AF8407B3EF899EACFCECF5', 'ip': '192.168.1.25', 'time_diff': 60, 'ssid': 'REJfER3t', 'rssi': -38, 'signal_level': 3, 'auto_off_status': 'off', 'auto_off_remain_time': 0, 'latitude': 493857, 'longitude': 30042, 'lang': 'fr_FR', 'avatar': 'plug', 'region': 'Europe/Paris', 'specs': '', 'nickname': 'R3VpcmxhbmRlIFNhbG9u', 'has_set_location_info': True, 'device_on': False, 'on_time': 0, 'default_states': {'type': 'last_states', 'state': {}}, 'overheated': False, 'power_protection_status': 'normal'}

The strange thing in this is the plug really turn on and off when I execute this script, even with the authentication failure message. In Domoticz (I'm not sure but I guess), the authentication failure message stop the plugin script before the p100.turnOn() or p100.turnOff() command is execute. So the plug is not turn on or off.

Regards.

almottier commented 11 months ago

Hello, this fork has a fallback to the old authentification mechanism for older hardware (for hw_ver < 2). So the result you have running your script is expected. I don't know what Domoticz is doing but it should just log an exception without failing too.

Schiki commented 11 months ago

Unfortunately this exception can't be catched with try: Is there another way to suppress these messages? In a Windows-Environment the old authentication-method doesn't work atl all, because no /tmp directory exists there.

almottier commented 11 months ago

Hello, in this PR https://github.com/almottier/TapoP100/pull/5 , you can choose between the protocols using the preferred_protocol parameter.

If you want to use only the old protocol for one of your devices, you can initialize it like this:

p100 = PyP100.P100(
    "192.168.1.10",
    "user@inboxen.org",
    "password",
    preferred_protocol="old"
)

You should not have any exceptions logged. I also added a fix for the /tmp @Schiki.

You can install it by running:

pip install --force-reinstall git+https://github.com/almottier/TapoP100.git@preferred_protocol

Please let me know if it works for you.

Schiki commented 11 months ago

Thanks for the quick fix, also in Windows it would work without the leading slash in the tempfile-string: FileNotFoundError: [Errno 2] No such file or directory: '/C:\Users\rschi\AppData\Local\Temp/tapo.key'

almottier commented 11 months ago

It should be fixed now @Schiki

Schiki commented 11 months ago

Now it works with both Windows and Linux and with the new parameter 'preferred_protocol="old"' the exception log message is avoided. Thanks a lot.

almottier commented 11 months ago

Resolved with https://github.com/almottier/TapoP100/pull/5

DrakarBarna commented 11 months ago

Hi, I did not answer yet because my problem originally appear random. Now, after week, the issue did not appear and I consider it resolve. Thanks a lot for you quick reaction.

Regards.