FlagX / ha-ledvance-tuya-resync-localkey

pyscript for homeassistant to resync local keys from private tuya api
MIT License
108 stars 17 forks source link

Trying to figure out how to use this :-) #1

Closed fribse closed 1 year ago

fribse commented 1 year ago

Hi @FlagX

I know this is just a POC, but I'm hoping I can get it working here :-)

Ok, so I got the pyscripting addon installed, I checked the settings as required. The two folders with contents are copied into it. I've modified the const.py, but here I think I'm hitting a snag. What is TUYA_COUNTRY_CODE and where do I find it? Also the TUYA_DEVICE_ID is that for a specific device?

The TUYA_CLIENT_ID is copied from Tuya IOT cloud -> Overview -> Authorization Key The same goes for TUYA_SECRET_KEY

I can call the script from HA->services, but I don't see any kind of output, and if I then go to add one of the ledvance lights in localtuya, the local key is still not shown.

Looking in the logs I see two messages:

Logger: tuya.api
Source: pyscript_modules/tuya/api.py:217
First occurred: 22.09.43 (2 occurrences)
Last logged: 22.19.02

Error! Code: SING_VALIDATE_FALED, message: Permission Verification Failed, result: {'t': 1659557383931, 'success': False, 'errorCode': 'SING_VALIDATE_FALED', 'status': 'error', 'errorMsg': 'Permission Verification Failed'}
Error! Code: SING_VALIDATE_FALED, message: Permission Verification Failed, result: {'t': 1659557942572, 'success': False, 'errorCode': 'SING_VALIDATE_FALED', 'status': 'error', 'errorMsg': 'Permission Verification Failed'}

Denne fejl stammer fra en brugerdefineret integration.

Logger: custom_components.pyscript.file.sync_tuya_keys.syncTuyaKeys
Source: custom_components/pyscript/eval.py:480
Integration: Pyscript Python scripting (documentation, issues)
First occurred: 22.09.43 (2 occurrences)
Last logged: 22.19.02

Exception in <file.sync_tuya_keys.syncTuyaKeys> line 12: await hass.async_add_executor_job(api.login) ^ ValueError: Invalid result, check logs

Any ideas?

FlagX commented 1 year ago

Hi,

TUYA_COUNTRY_CODE seem to match the phone country codes (e.g. 43 = Austria) I think the TUYA_DEVICE_ID is a random string which is generated on your phone on app install.

Secrets etc do not come from Tuya IOT cloud. If Ledvance would have enabled this service, the whole deal would be much easier ;)

Pyscript does not have the log level set to INFO per default. (https://hacs-pyscript.readthedocs.io/en/latest/tutorial.html#writing-your-first-script)

I made a mistake in the setup guide: You need change the placeholders in pyscript/sync_tuya_keys.py not in pyscript_modules/tuya/const.py

If you are using Ledvance than you should need to change anything in const.py (only if you are using a different vendor)

I will update the setup guide.

Thanks for the feedback!

fribse commented 1 year ago

Hi @FlagX I think what you are attempting here is fantastic, so I really hope I can prove your concept :-D

Ok, so the const is only to be changed for the TUYA_COUNTRY_CODE (for me, Denmark, 45). Can I read that value somewhere to be sure? Where do I read the TUYA_DEVICE_ID from, so is that a specific device, or is it an ID for all devices? I can see some device ID's when opening localtuya, so do I pick one of those?

Latest log:

Logger: custom_components.pyscript.file.sync_tuya_keys.syncTuyaKeys
Source: custom_components/pyscript/eval.py:480
Integration: Pyscript Python scripting (documentation, issues)
First occurred: 12.30.00 (1 occurrences)
Last logged: 12.30.00

Exception in <file.sync_tuya_keys.syncTuyaKeys> line 27: if (localKeys[devId] != localKey): ^ KeyError: 'bf32ab980658b2457aahd7'
FlagX commented 1 year ago

Looks like you have a device in your local tuya config that is not synced with your Ledvance account?

I added a check for this case, which should skip such devices.

Regarding the IDs: I got mine via via mitm proxy in the Android Studio emulator. But they do not seem to matter. It looks to me like you already got them right and you are able to authenticate to the API.

fribse commented 1 year ago

I have a 'non brand' fiber light also, that is in tuya cloud, but I can't figure out which fields does what, and that could be the one messing about.

Now I see this in the log:

2022-08-04 19:46:22.289 INFO (MainThread) [custom_components.pyscript.global_ctx] Reloaded /config/pyscript/sync_tuya_keys.py

So I guess that's cool now, but then what? If I go to localtuya and try to configure a light, it doesn't show the localkey. Can I run the print-local-keys.py from the HA shell?

FlagX commented 1 year ago

you can also run print-local-keys.py on your local machine (without home assistant) if you just need the keys

fribse commented 1 year ago

Hi @FlagX I REALLY appreciate your paticence with me, thankyou. Ok, so I got the python installed through the M$ Store, that seemed to be the easiest way. Now it complains that there is no module named Crypto. So I tried using pip install crypto, pip install cryptography and pip install pycrypto, but it still says it can't find the Crypto module???

fribse commented 1 year ago

Ahh, ok, so I uninstalled all those modules again, and then I installed pycryptodome, and BINGO, localkeys :-)

fribse commented 1 year ago

WOW, it WORKS, geeez this is bloody amazing. So now I need some explanation. the print-local-keys.py shows me the keys. This is needed to add the lights etc. But what about the sync script, what is that then used for?

FlagX commented 1 year ago

I have 12 lights and sometimes I reset them by accident (or idk why they do that) and I have to re-add them and they have a new local key. The sync script saves me a lot of time there.

fribse commented 1 year ago

Ok, I see, good to know :-) Could you explain the steps there? Do the old entries need to be removed first, and then readd the light, or what is the procedure?

FlagX commented 1 year ago

You just need to re-add them in the Ledvance app and call the sync service. That's it.

JAudi23 commented 9 months ago

This works great, amazing, thanks!!