Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout errors while toggling device state #11

Closed
Aurillium opened this issue Nov 30, 2020 · 7 comments
Closed

Timeout errors while toggling device state #11

Aurillium opened this issue Nov 30, 2020 · 7 comments

Comments

@Aurillium
Copy link

I'm sure there's something I'm doing wrong here, but every time I try to set the state of my OutletDevice I get timeout errors. I checked this with the example test.py too. I followed all the instructions for getting the device key and checked through them multiple times, and watched the linked video as well. I can get the device state fine, I just can't set it, which is what leads me to believe it's something to do with the device key. Any help or explanation as to what is happening here would be much appreciated.

Thank you

@arrmo
Copy link

arrmo commented Dec 1, 2020

Hi,

Just tried it here, using turn_on() and turn_off() ... works fine, no issues at all. What state are you trying to set?

@ear9mrn
Copy link

ear9mrn commented Dec 17, 2020

Having the same issue here. Status is return ok but fails to turn switch on or off.

import tinytuya

d = tinytuya.OutletDevice('xxxx', 'xxxx', 'xxxx')
d.set_version(3.1)
data = d.status()
d.turn_off()

print('Dictionary %r' % data)
print('State (bool, true is ON) %r' % data['dps']['1'])'

and response but switch does not change state. Works fine with app.

`
Dictionary {'devId': 'xxxx', 'dps': {'1': True, '11': 0}}
State (bool, true is ON) True

and with debug turned on....DEBUG:tinytuya:Exception with low level TinyTuya socket!!! retry 1/5`
Any suggestions?

Thanks.

Pete.

@jasonacox
Copy link
Owner

Hi Pete, to control any Tuya device you need to make sure you have the correct device key. A 3.1 device will provide status without the correct key so it can be misleading. Did you run the Wizard (python -m tinytuya wizard) to get the keys (see here) or did you find them another way?

@ear9mrn
Copy link

ear9mrn commented Dec 20, 2020

Hi Jason,

Many thanks for getting back to me. Yes, I did follow that process and as far as I can tell everything worked fine. When I ran the wizard it came back with a json response for my smart plug with a name, id and key which i then used to run the test.py script and it just timeout. I changed random letters in the key and get the same results so I assume the key the wizard is returning is incorrect for some reason. I will dig through the code to see if I can figure out what the problem is but my python skills are a little lacking...!

Let me know if I can provide any more information to help diagnose the problem.

Pete.

@jasonacox
Copy link
Owner

Thanks, Pete! I think I found a bug. For 3.1 devices, the hexdigest was set to trim [8:][:24] based on other tuya libraries. I adjusted it to hexdigest[8:][:16] for CONTROL commands.

I have pushed a new patched version (tinytuya 1.1.2), so try to upgrade see if this fixes the issue for you.

python3 -m pip install --upgrade tinytuya

Here is the updated section FYI:

if self.version == 3.3:
# expect to connect and then disconnect to set new
self.cipher = AESCipher(self.local_key)
json_payload = self.cipher.encrypt(json_payload, False)
self.cipher = None
if command_hb != '0a':
# add the 3.3 header
json_payload = PROTOCOL_VERSION_BYTES_33 + \
b"\0\0\0\0\0\0\0\0\0\0\0\0" + json_payload
elif command == CONTROL:
# need to encrypt
# expect to connect and then disconnect to set new
self.cipher = AESCipher(self.local_key)
json_payload = self.cipher.encrypt(json_payload)
preMd5String = b'data=' + json_payload + b'||lpv=' + \
PROTOCOL_VERSION_BYTES_31 + b'||' + self.local_key
m = md5()
m.update(preMd5String)
hexdigest = m.hexdigest()
# some tuya libraries strip 8: to :24
json_payload = PROTOCOL_VERSION_BYTES_31 + \
hexdigest[8:][:16].encode('latin1') + json_payload
self.cipher = None # expect to connect and then disconnect to set new

@ear9mrn
Copy link

ear9mrn commented Dec 20, 2020

yep. working now...... :-)

Thanks,

Pete.

@jasonacox
Copy link
Owner

Thanks, Pete! I'll close this issue since it is linked to the new release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants