jasonacox / tinytuya

Python API for Tuya WiFi smart devices using a direct local area network (LAN) connection or the cloud (TuyaCloud API).
MIT License
868 stars 157 forks source link

Wrong protocol when using generate_payload #448

Closed Xenomes closed 5 months ago

Xenomes commented 5 months ago

Hi, I think I found a bug. I'm attempting to turn on an infrared heating panel using DPS codes, and I've noticed differences in the protocol to the turn_on() command.

import tinytuya
tinytuya.version
'1.13.1'
tuya = tinytuya.Device(dev_id='bf6bf5c39b8e48682fhksl', address='192.168.1.56', local_key='secret', version=3.4)
payload = tuya.generate_payload(tinytuya.CONTROL_NEW, {1: 'True'})
payload
MessagePayload(cmd=13, payload=b'{"protocol":5,"t":1706453979,"data":{"dps":{"1":"True"}}}')
tuya.send(payload)

The device doesn't react to "True"; if the device is on, it goes off.

tuya.turn_on()
{'protocol': 4, 't': 1706454015, 'data': {'dps': {'1': True}}, 'dps': {'1': True}}

This works as expected; the difference is the protocol version.

jasonacox commented 5 months ago

Hi @Xenomes ,

The turn_on() uses the CONTROL command that calls generate_payload() which translates that to the right protocol template for your device version. It also formats the dictionary elements into the right payload.

Also:

tuya.generate_payload(tinytuya.CONTROL_NEW, {1: 'True'})

That's incorrect. It should be:

tuya.generate_payload(tinytuya.CONTROL_NEW, {1: True})

The "True" part should be a pythonic boolean, not a string.

Xenomes commented 5 months ago

Thanks for the explaining and you are correct i just need to boolean the value i got back. Thanks I overlooked this. 😅