piekstra / tplink-cloud-api

A Python library to remotely control TP-Link smart home devices using their cloud service - no need to be on the same network as your devices
GNU General Public License v3.0
41 stars 11 forks source link

Not working with Tapo L530 bulb #29

Closed ndg63276 closed 3 years ago

ndg63276 commented 3 years ago

Hello, trying to get this to work with my Tapo L530 bulb, I can log in and get the device list, but don't seem to get the relay_state:

>>> from tplinkcloud import TPLinkDeviceManager
>>> device_manager = TPLinkDeviceManager(username, password)
>>> devices = device_manager.get_devices()
>>> if devices:
...   print(f'Found {len(devices)} devices')
...   for device in devices:
...     print(f'{device.model_type.name} device called {device.get_alias()}')
... 
Found 1 devices
UNKNOWN device called U21hcnQgQnVsYg==
>>> device_name = "U21hcnQgQnVsYg=="
>>> device = device_manager.find_device(device_name)
>>> if device:
...   print(f'Found {device.model_type.name} device: {device.get_alias()}')
...   device.toggle()
... 
Found UNKNOWN device: U21hcnQgQnVsYg==
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/home/pi/.local/lib/python3.7/site-packages/tplinkcloud/device.py", line 68, in toggle
    if self.is_on():
  File "/home/pi/.local/lib/python3.7/site-packages/tplinkcloud/device.py", line 91, in is_on
    return sys_info['relay_state'] == 1
TypeError: 'NoneType' object is not subscriptable

Trying to look through the code, I can log in and get device list using requests, but then I get this error:

>>> termId = str(uuid.uuid4())
>>> payload = {
...     'method': 'login',
...     'params': {
...             'appType': 'Kasa_Android',
...             'cloudUserName': username,
...             'cloudPassword': password,
...             'terminalUUID': termId
...     }
... }
>>> url = 'https://wap.tplinkcloud.com'
>>> r = requests.post(url, json=payload)
>>> params = {
...     'token': r.json()['result']['token'],
...     'appName': 'Kasa_Android',
...     'termID': termId,
...     'appVer': '1.4.4.607',
...     'ospf': 'Android+6.0.1',
...     'netType': 'wifi',
...     'locale': 'en_GB'
... }
>>> payload2 = {"method": "getDeviceList"}
>>> r2 = requests.post(url, params=params, json=payload2)
>>> controlUrl = r2.json()["result"]["deviceList"][0]["appServerUrl"]
>>> deviceId = r2.json()["result"]["deviceList"][0]["deviceId"]
>>> payload3 = {
...     "method": "passthrough",
...     "params": {
...             "deviceId": deviceId,
...             "requestData": "{\"system\":{\"set_relay_state\":{\"state\":1}}}"
...     }
... }
>>> r3 = requests.post(controlUrl, params=params, json=payload3)
>>> r3.json()
{'error_code': -20571, 'msg': 'Device is offline'}

The bulb is definitely online, and controllable through the Tapo app. It doesn't matter what I send in the requestData, it always returns the same error.

So I guess my questions are:

  1. Does this library work with Tapo devices?
  2. Has anyone got it to work on a L530 bulb?
  3. Has anyone seen this -20571 error?

I feel like I'm close but can't get the last step.

piekstra commented 3 years ago

Hi @ndg63276 thanks for your post!

In answer to your questions:

  1. This library does not support Tapo devices at this time and I am unsure if it will - it is currently oriented towards devices that integrate with TP-Link's Kasa app which I do not believe that particular Tapo device does.
  2. Based on the above, I would expect not. That said, I found some other open source code out there that may deal with Tapo devices. I plan to do some digging into those to see if I can't add support to this project. Depending on how unique the API contracts are compared to Kasa-supported devices, it may be outside the scope of this project. a. https://github.com/fishbigger/TapoP100 b. https://github.com/fishbigger/HomeAssistant-Tapo-P100-Control c. https://github.com/python-kasa/python-kasa
  3. I have not seen that error code before - that is an interesting response that I will have to account for so that a friendlier message can be returned than the exception you were running into.
ndg63276 commented 3 years ago

Hi @piekstra, thanks for getting back to me. I've tried the bulb with the Kasa app, and it is not found, so not a big surprise it doesn't work with your library. I've had a look at those repo's, and tested the first one, it does work with my bulb. However, all these Tapo resources only work on the local network, whereas your Kasa one works via the cloud, which is what I would like. I guess the only way forward would be packet sniffing what the app sends, so I will look into that. If anyone has any advice, I'm listening!

Thanks anyway.

piekstra commented 3 years ago

Hi @piekstra, thanks for getting back to me. I've tried the bulb with the Kasa app, and it is not found, so not a big surprise it doesn't work with your library. I've had a look at those repo's, and tested the first one, it does work with my bulb. However, all these Tapo resources only work on the local network, whereas your Kasa one works via the cloud, which is what I would like. I guess the only way forward would be packet sniffing what the app sends, so I will look into that. If anyone has any advice, I'm listening!

Thanks anyway.

Thanks for following up again. I haven't had much time to invest in looking at the Tapo resources but if you end up being able to do some packet sniffing to figure out the API contracts then it would be far easier for me to try and make an integration within this library, or even setup a separate library for the Tapo devices (dependent on how much overlap there ends up being between Kasa and Tapo). As I do not have any Tapo devices currently I am not readily able to do that investigation myself.

For now I will be closing the issue but please feel free to open it up again, or start a new issue if you end up being able to gather that information about their API. I am sorry that I could not be of greater help to your efforts.

Until then, best of luck!