JurajNyiri / HomeAssistant-Tapo-Control

Control for Tapo cameras as a Home Assistant component
Apache License 2.0
1.03k stars 85 forks source link

Feature Request: Support new authorization on the newest firmwares #436

Closed mafr95 closed 9 months ago

mafr95 commented 9 months ago

Description

Yesterday I updated my Tapo C210 camera to version 1.3.9. After that, the integration could no longer connect to the camera. I decided to delete the camera from the integration and add it again. But every time I enter my cloud password, the error message "Invalid cloud password" appears. I have now checked the password 10 times. It is definitely correct. I suspect it has something to do with the camera update. Previously it worked without any problems.

I have two other cameras. (C200 and C220) I was able to add them with the same cloud password without any problems.

In the meantime, I removed the camera from the Tapo app, reset it and added it again. But without success.

Reproduction Steps

  1. Update C210 to version 1.3.9 Build 231019 Rel. 13805n(4555)
  2. Add camera in tapo integration
  3. Enter correct ip adress
  4. Enter correct username and passwort for camera account (created via tapo app)
  5. Enter cloud password ==> failed

Expected behavior

Add camera to integration

If applicable, add error logs.

No response

Device Firmware

1.3.9 Build 231019 Rel. 13805n(4555)

Using stream component

Yes

Does camera work via official integrations?

Yes

Camera has all attributes filled out in developer tools

Yes

HASS Environment

RPI 4

Search for similar issues

Yes

Additional information

No response

TwKenYang commented 9 months ago

Can I still use TC70? Because it still shows that the cloud password is wrong. I have updated to 5.4.0 Screenshot_2023-11-05-05-54-37-381_com tplink iot

JurajNyiri commented 9 months ago

If you are still experiencing issues:

I have pushed a new code: https://github.com/JurajNyiri/pytapo/tree/debug_request

Download it manually, and next to README.md create file test.py with content:

from pytapo import Tapo

# Camera
user = ""  # 3rd party account name
password_cloud = ""  # cloud password
host = ""  # ip of the camera, example: 192.168.1.52

tapo = Tapo(host, "admin", password_cloud)
request = tapo.getBasicInfo()
print(request)

Post the whole output of the script, it should already have everything important redacted.

Swampen commented 9 months ago

The new release didn't fix it for me as I still get "Invalid cloud password" (C210 with 1.3.9 firmware). I tried the new code from https://github.com/JurajNyiri/pytapo/tree/debug_request (you posted the earlier debug branch).

Local account output ``` {'data': {'method': 'login', 'params': {'encrypt_type': '3', 'username': 'homeassistant-rtsp'}}, 'headers': {'Host': 'REDACTED', 'Referer': 'REDACTED', 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Tapo CameraClient Android', 'Connection': 'close', 'requestByApp': 'true', 'Content-Type': 'application/json; charset=UTF-8'}, 'verify': False} 200 {'error_code': -40401, 'result': {'data': {'code': -40411, 'encrypt_type': ['1', '2'], 'key': 'REDACTED', 'nonce': 'REDACTED'}}} {'data': {'method': 'login', 'params': {'hashed': True, 'password': 'REDACTED', 'username': 'homeassistant-rtsp'}}, 'headers': {'Host': 'REDACTED', 'Referer': 'REDACTED', 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Tapo CameraClient Android', 'Connection': 'close', 'requestByApp': 'true', 'Content-Type': 'application/json; charset=UTF-8'}, 'verify': False} 200 {'error_code': -40401, 'result': {'data': {'code': -40411, 'encrypt_type': ['1', '2'], 'key': 'REDACTED', 'nonce': 'REDACTED'}}} Traceback (most recent call last): File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\ape.py", line 9, in tapo = Tapo(host, user, password) File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 68, in __init__ self.basicInfo = self.getBasicInfo() File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 776, in getBasicInfo return self.executeFunction( File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 332, in executeFunction data = self.performRequest( File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 371, in performRequest self.ensureAuthenticated() File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 88, in ensureAuthenticated return self.refreshStok() File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 310, in refreshStok raise Exception("Invalid authentication data") Exception: Invalid authentication data ```
Cloud account output ``` {'data': {'method': 'login', 'params': {'encrypt_type': '3', 'username': 'admin'}}, 'headers': {'Host': 'REDACTED', 'Referer': 'REDACTED', 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Tapo CameraClient Android', 'Connection': 'close', 'requestByApp': 'true', 'Content-Type': 'application/json; charset=UTF-8'}, 'verify': False} 200 {'error_code': -40413, 'result': {'data': {'code': -40401, 'encrypt_type': ['3'], 'key': 'REDACTED', 'nonce': 'REDACTED', 'device_confirm': 'REDACTED'}}} {'data': {'method': 'login', 'params': {'cnonce': 'REDACTED', 'encrypt_type': '3', 'username': 'admin'}}, 'headers': {'Host': 'REDACTED', 'Referer': 'REDACTED', 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Tapo CameraClient Android', 'Connection': 'close', 'requestByApp': 'true', 'Content-Type': 'application/json; charset=UTF-8'}, 'verify': False} 200 {'error_code': -40413, 'result': {'data': {'code': -40401, 'encrypt_type': ['3'], 'key': 'REDACTED', 'nonce': 'REDACTED', 'device_confirm': 'REDACTED'}}} Traceback (most recent call last): File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\ape.py", line 12, in tapo = Tapo(host, "admin", password_cloud) File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 68, in __init__ self.basicInfo = self.getBasicInfo() File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 776, in getBasicInfo return self.executeFunction( File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 332, in executeFunction data = self.performRequest( File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 371, in performRequest self.ensureAuthenticated() File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 88, in ensureAuthenticated return self.refreshStok() File "C:\Users\ole-m\Downloads\tapo_debug\pytapo\pytapo\__init__.py", line 306, in refreshStok raise Exception("Invalid authentication data") Exception: Invalid authentication data ```
JurajNyiri commented 9 months ago

@Swampen Nice catch. I pushed new version with more debug logs and ability to turn off redact as we might need the actual values during debugging (send it to me via email).

You can turn off redacting like this:

tapo = Tapo(host, "admin", password_cloud, "", "", None, False, False)

Do not post logs without redacted information here.

At this point, just run it again with the new code. If you see "Determined actual wrong cloud password!" you need to check if you are entering actual proper cloud password (and the password camera knows about) - if you changed password recently camera might not be aware of the change especially if you have it blocked in firewall. Unblock it, restart couple of times or even factory reset might be needed (past experience) to pick up the new password immediately.

GSzabados commented 9 months ago

@JurajNyiri, what if the device_confirm is empty?

JurajNyiri commented 9 months ago

The authentication would fail in that case. Can it be empty though? @Swampen is it empty? (Run without redact see above my comment)

viprapp commented 9 months ago

Well done @JurajNyiri! I can confirm that my C210 on 1.3.9 now works as expected again. I have to add that I removed and added the device entry. @Swampen Maybe that is a necessary step after the update to 1.3.9.

Swampen commented 9 months ago

@JurajNyiri A factory reset solved it. Removing the device and adding it again might solve it as well as @viprapp mentioned. Nice work!

fcachado commented 9 months ago

Hi @JurajNyiri !

I already removed and reinstalled the integration on HA (v5.4.0). I also removed the camera (C210 firmware 1.3.9) and added again to the Tapo app, but the cloud password error is still appearing once I try to add to HA. Is there something that I'm missing?

JurajNyiri commented 9 months ago

Follow all the steps I listed above (or see https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/issues/437#issuecomment-1793721991 where I wrote it again). Factory reset, unblock in firewall and run the script as described.

fcachado commented 9 months ago

I reboot once more the camera and it worked, I had to reboot a total of 3 times to make it work. Thanks for your help!

JurajNyiri commented 9 months ago

@fcachado glad you got it working! You probably changed the cloud password and it had to pick it up. I do not know when it decides to get new password but factory reset / readding always does it.

mitzu2250 commented 9 months ago

Hello,

Following your reply to #436 , please note that I haven't changed my Cloud password recently. I know for a fact that the password is right because I logged in to the app using the password. My firewall (router ports) are not blocked, another older firmware works on the same system.

Also, I cannot reset to factory my camera because it's in a space that is not that easily accessible. I tried rebooting it over and over again, didn't work.

I'll try to run the script later today and come back with the output.

{'data': {'method': 'login', 'params': {'encrypt_type': '3', 'username': 'admin'}}, 'headers': {'Host': 'REDACTED', 'Referer': 'REDACTED', 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Tapo CameraClient Android', 'Connection': 'close', 'requestByApp': 'true', 'Content-Type': 'application/json; charset=UTF-8'}, 'verify': False}
200
{'error_code': -40413, 'result': {'data': {'code': -40401, 'encrypt_type': ['3'], 'key': 'REDACTED', 'nonce': 'REDACTED', 'device_confirm': 'REDACTED'}}}

{'data': {'method': 'login', 'params': {'cnonce': 'REDACTED', 'encrypt_type': '3', 'username': 'admin'}}, 'headers': {'Host': 'REDACTED', 'Referer': 'REDACTED', 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Tapo CameraClient Android', 'Connection': 'close', 'requestByApp': 'true', 'Content-Type': 'application/json; charset=UTF-8'}, 'verify': False}
200
{'error_code': -40413, 'result': {'data': {'code': -40401, 'encrypt_type': ['3'], 'key': 'REDACTED', 'nonce': 'REDACTED', 'device_confirm': 'REDACTED'}}}
Determined actual wrong cloud password!
Traceback (most recent call last):
  File "C:\Users\Alex\Desktop\pytapo-debug_request\test.py", line 8, in <module>
    tapo = Tapo(host, "admin", password_cloud)
  File "C:\Users\Alex\Desktop\pytapo-debug_request\pytapo\__init__.py", line 70, in __init__
    self.basicInfo = self.getBasicInfo()
  File "C:\Users\Alex\Desktop\pytapo-debug_request\pytapo\__init__.py", line 785, in getBasicInfo
    return self.executeFunction(
  File "C:\Users\Alex\Desktop\pytapo-debug_request\pytapo\__init__.py", line 341, in executeFunction
    data = self.performRequest(
  File "C:\Users\Alex\Desktop\pytapo-debug_request\pytapo\__init__.py", line 380, in performRequest
    self.ensureAuthenticated()
  File "C:\Users\Alex\Desktop\pytapo-debug_request\pytapo\__init__.py", line 90, in ensureAuthenticated
    return self.refreshStok()
  File "C:\Users\Alex\Desktop\pytapo-debug_request\pytapo\__init__.py", line 315, in refreshStok
    raise Exception("Invalid authentication data")
Exception: Invalid authentication data
JurajNyiri commented 9 months ago

Camera doesn't agree with the password you are sending it. It is 99percent camera or user issue.

One last thing we can check - run it without redact - see https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/issues/436#issuecomment-1793694748 and check what is inside the device_confirm field. If it is not empty it is camera/user issue and there is nothing to fix on integration.

mitzu2250 commented 9 months ago

Ok, not sure what to do more besides resetting it factory.

For the other camera, also a C210 but on 1.3.7, same settings (different ip of course) it works. For this one on 1.3.9 I get the error and the device_confirm is not empty.

I will keep restarting it and trying again until it will work.

JurajNyiri commented 9 months ago

You can also try resetting the cloud password and trying with a new one.

mitzu2250 commented 9 months ago

You can also try resetting the cloud password and trying with a new one.

This did not work. 1.3.7 picked up the new password right away, 1.3.9 didn't.

JurajNyiri commented 9 months ago

Try sending me the clear version of logs to mail juraj.nyiri@gmail.com I can take a look if values look correct. Logs contain sha256 hash of your cloud password.

mitzu2250 commented 9 months ago

Since we agreed that is something wrong on camera side with my setup (and might be the case for others who do not read github discussions), and even if I input the correct cloud password I cannot get the camera added again, it would be nice to be able to check a box when I get the "Invalid cloud password" message while trying to add a new device, box like "I confirm that the password is correct" and if I check that and hit submit, it will skip the initial check, add the new device (which of course will fail) but at least it will automatically try the password and I don't need to repeat the workflow over and over again.

GSzabados commented 9 months ago

add the new device

And how the integration would know what to add for your device?

JurajNyiri commented 9 months ago

That's not possible, good practice and would introduce great levels of confusion. It will do what you said if you have added the device in the past. There is no reason to readd device once it was once added.

If you really want to do it you can modify the storage files or the integration code fairly simply. In case someone can do that they probably will not come here asking for support related to them not understanding why it doesn't still work. I am not going to provide a guide on how to do it though.

mitzu2250 commented 9 months ago

Just updated my second camera which was on 1.3.7 to 1.3.9. It started having the same password issue, ran the debug tool before and after.

So, from my point of view, something is messing up the password storage during the firmware upgrade.

JurajNyiri commented 9 months ago

@mitzu2250 does the factory reset / readding to tapo app help?

mitzu2250 commented 9 months ago

Yes, this one was in an area which allowed me to reset it. After adding it again to the app, the debug script works, I get expected output.

After restarting the Home Assistant service everything got back to normal.

So, yes, apparently a Reset would do the trick. I'll have to do this for my other camera too...

rark-ha commented 9 months ago

Moving from my closed item (https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/issues/442

I updated to 5.4.2 but now have created issues with two further cameras (so now all 4 of my 210 models).

Trying to reload the failed setup entries doesn't do anything. When my first two entities played up, they were 'failed setup' before becoming 'migration errors' after another restart.

Screenshot 2023-11-08 at 5 39 24 am Screenshot 2023-11-08 at 5 48 24 am
JurajNyiri commented 9 months ago

Read above and tell us what you tried so far. Basic steps, first update when you see an issue (and read the release notes), then if you didn't and you were told to and linked to this issue, read about the issue you were linked to. Stop wasting others time asking for help if you have instructions right above you.

JurajNyiri commented 9 months ago

I just discovered what was happening to most of the users here. More details soon (spoilers).

TL&DR

If you have updated to latest and see Invalid password: You can factory reset to get it work NOW or remove camera, restart HA, wait an hour and readd. I am working on a better solution.

mitzu2250 commented 9 months ago

I tried running the debug_request even after 24h and still didn't worked so I'm not convinced that waiting is a 100% working solution. Also tried adding the camera after 24h and still got the error message. During this time, rebooted the camera and HA multiple times.

For now, the only 100% working solution is the Factory Reset (aka Reset button).

JurajNyiri commented 9 months ago

@mitzu2250 Waiting AFTER removing the camera from HA and not running any scripts, important detail.

mitzu2250 commented 9 months ago

Just tried with the camera I removed yesterday, restarted HA, waited around 65 minutes, tried to add it, didn't work.

JurajNyiri commented 9 months ago

@mitzu2250 There could be multiple issues, some I can fix and some I can't that are on cameras. They added temporary suspension of account on too many failed sign ins. On update to 1.3.9 if people are still using old integration it will keep retrying to sign in using the old method, resulting in refreshing temporary ban all the time. I am working on fixing that.

Wait for new version which will handle this better and show if user is banned.

JurajNyiri commented 9 months ago

@mitzu2250 Try with https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/releases/tag/5.4.3 . It is possible most of users who updated firmware got into temporary ban loop (which takes 1800 seconds). This will no longer happen, instead integration will stop retrying and ask for re-authorization. This should help user experiencing the invalid authentication data error.

Edit: although I do not know if it works the same on new Auth. I have still not received an update 😞

mitzu2250 commented 9 months ago

Nope, doesn't work for me.

silfax commented 9 months ago

Ok, I know that it's marked as 'fixed', but I'm probably dumb enough and I cannot make it work. C310, 1.3.9 firmware, integration 5.4.8. I keep getting "Invalid authentication data. Make sure you have created your 3rd party account via Tapo app. You can also test if these credentials work via rtsp stream, for example VLC using link rtsp://username:password@IP Address:554/stream1". VLC doesn't work as well, although I remember that at some point, after the issue started, it was able to show stream2 (but not stream1). Strangely, my software NVR (Luxriot) doesn't have any issues accessing both streams of the camera with the same credentials. Can anyone point me to the right steps? Is a factory reset of the camera necessary? Thanks!

GSzabados commented 9 months ago

Strangely, my software NVR (Luxriot) doesn't have any issues accessing both streams of the camera with the same credentials. Can anyone point me to the right steps? Is a factory reset of the camera necessary? Thanks!

Do you have an SD card installed or Tapo Care as well?

These devices support limited amount of stream, if you use those options and stream to another software and use the app, then you will be unable to access the streams anymore.

JurajNyiri commented 9 months ago

@silfax This issue was fixing a different bug. You are encountering a limit of streams issue.

https://www.tp-link.com/cz/support/faq/2742/

Q3: Can multiple accounts/devices view the Tapo camera at the same time?

A: Currently, each camera can be controlled or managed by only one account on the Tapo App. You can share it with 5 different accounts at most, and these two accounts can only access live view and playback features of the camera.

Each camera also supports up to 2 simultaneous video streams. You could use up to 2 devices to view the live feed of the camera simultaneously using the Tapo App or via RTSP. You may also only view the playback of a camera using one Tapo app at a time.

Q4: Why can’t I use Tapo Care, SD card, and NVR at the same time?

A: Due to the limited hardware performance of the camera itself, Tapo Care works best with one of the NVR or SD card recordings.

If you are using an SD card and Tapo Care at the same time, the NVR(RTSP/ONVIF) will be disabled.

To restart the recording on the NVR, please remove the SD card from the camera.

I added it to FAQ in readme as well now.

silfax commented 9 months ago

Strangely, my software NVR (Luxriot) doesn't have any issues accessing both streams of the camera with the same credentials. Can anyone point me to the right steps? Is a factory reset of the camera necessary? Thanks!

Do you have an SD card installed or Tapo Care as well?

These devices support limited amount of stream, if you use those options and stream to another software and use the app, then you will be unable to access the streams anymore.

SD card installed, no Tapo Care. Whatever the reason, it started after the 1.3.9 firmware upgrade. No issues before, nothing is changed. NVR used before, camera working before under HA. 3 x C200 and 1 x C500 still working.

silfax commented 9 months ago

@silfax This issue was fixing a different bug. You are encountering a limit of streams issue.

https://www.tp-link.com/cz/support/faq/2742/

Q3: Can multiple accounts/devices view the Tapo camera at the same time?

A: Currently, each camera can be controlled or managed by only one account on the Tapo App. You can share it with 5 different accounts at most, and these two accounts can only access live view and playback features of the camera.

Each camera also supports up to 2 simultaneous video streams. You could use up to 2 devices to view the live feed of the camera simultaneously using the Tapo App or via RTSP. You may also only view the playback of a camera using one Tapo app at a time.

Q4: Why can’t I use Tapo Care, SD card, and NVR at the same time?

A: Due to the limited hardware performance of the camera itself, Tapo Care works best with one of the NVR or SD card recordings.

If you are using an SD card and Tapo Care at the same time, the NVR(RTSP/ONVIF) will be disabled.

To restart the recording on the NVR, please remove the SD card from the camera.

I added it to FAQ in readme as well now.

Thanks for answering. Yeah, it seems I totally misunderstood what issue was in fact fixed.

On Q3 and Q4, I'm too unexperienced to disagree, but from my own experience, the same camera used to work in the same config, simultaneously with: NVR, HA, phone/tablet app (occasionaly). No error message before the firmware upgrade. Now I can't even add it again in HA.

JurajNyiri commented 9 months ago

It is official TPLink documentation, if you disagree you are most probably wrong.

Nobody but you can help you solve this issue now, it is clear what is the issue, you have all the official TPLink information why it is happening, now you need to solve it, its your environment, we do not know it.

It is not an integration issue.

silfax commented 9 months ago

ok, thanks anyway. i never said it was an integration issue, just that the 1.3.9 firmware probably changed some things. my environment may be the issue, there may be just a very small number of users affected, but i hoped that maybe someone else encountered the same problem and could share some solution. i still have 3 x c200 and 1 x c500 working in the same environment without any error messages.

HoLy1337 commented 8 months ago

I have the same problem with my C200, Hardware version 2.0 and the 1.3.9 version... frigate can connect without problems, but the tapo control can not connect... I tried to deactivate frigate for a while and restart my complete environment multiple times, but it looks for me like the version is the problem... i even tried downgrading home assistant.

Here is the error log, 500 indicates an internal error... Maybe they want to start to lockdown there services to there app...

Logger: custom_components.tapo_control Source: custom_components/tapo_control/init.py:479 Integration: Tapo: Kamerasteuerung (documentation, issues)

Unable to connect to Tapo: Cameras Control controller: Error communicating with Tapo Camera. Status code: 500

if you need more informations feel free to tell me :)

The only things i changed recently in my environment is:

HoLy1337 commented 8 months ago

I found a solution. for my case the problem was that the automatic update from HACS is broken. I searched for the integration and redownloaded it, after a HA restart I was getting a migration error, then i removed the konfiguration and added the camera again, now it works again...

But I have noticed that HACS now shows 2 versions of the integration:

maybe this helps others too...

GSzabados commented 8 months ago

Tapo Controller is a different integration:

https://github.com/petretiandrea/home-assistant-tapo-p100

It is for plugs, bulbs, etc.

nikitiuk0 commented 3 months ago

I have 1.3.11 firmware (so two versions newer than 1.3.9) and I get the same -40401 error when trying to authenticate via pytapo:

{'error_code': -40401, 'result': {'data': {'code': -40411, 'encrypt_type': ['1', '2'], 'key': 'XXX, 'nonce': 'XXX'}}}

I also used HTTP toolkit to intercept traffic from the Android app in emulator but it seems like it doesn't even try to connect and auth into camera directly via local IP, but instead sends requests remotely to https://use1-app-server.iot.i.tplinknbu.com/

image

Was that how you folks intercepted this traffic previously? Or am I doing something differently?

shurakr commented 2 months ago

Everything worked stably until today. Today HA asks to reconfigure the cameras and ends up with Invalid cloud password

mgmorpheus77 commented 2 months ago

Hi Does anyone of you have old firmware for the TPLink C220 camera?