tuya / tuya-iot-python-sdk

Tuya IoT Python SDK for Tuya Open API.
MIT License
117 stars 47 forks source link

MAC check fails on device rename #75

Closed InterStella0 closed 5 months ago

InterStella0 commented 1 year ago

Raising a value error in TuyaOpenMQ when I update a device's name through the tuya smart mobile app. After this error, I no longer receive any data in the message listener.

Error

Exception in thread Thread-2 (_thread_main):
Traceback (most recent call last):
  File "C:\Users\sarah\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\Users\sarah\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\paho\mqtt\client.py", line 3591, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\paho\mqtt\client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\paho\mqtt\client.py", line 1164, in _loop
    rc = self.loop_read()
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\paho\mqtt\client.py", line 1556, in loop_read
    rc = self._packet_read()
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\paho\mqtt\client.py", line 2439, in _packet_read
    rc = self._packet_handle()
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\paho\mqtt\client.py", line 3033, in _packet_handle
    return self._handle_publish()
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\paho\mqtt\client.py", line 3327, in _handle_publish
    self._handle_on_message(message)
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\paho\mqtt\client.py", line 3570, in _handle_on_message
    on_message(self, self._userdata, message)
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\tuya_iot\openmq.py", line 137, in _on_message
    decrypted_data = self._decode_mq_message(
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\tuya_iot\openmq.py", line 110, in _decode_mq_message
    plaintext = cipher.decrypt_and_verify(data_buffer, tag_buffer).decode(
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\Crypto\Cipher\_mode_gcm.py", line 567, in decrypt_and_verify
    self.verify(received_mac_tag)
  File "D:\Programming\PycharmProjects\test-tuya\venv\lib\site-packages\Crypto\Cipher\_mode_gcm.py", line 508, in verify
    raise ValueError("MAC check failed")
ValueError: MAC check failed

Minimal Reproducible Code

from tuya_iot import TuyaOpenAPI, AuthType, TuyaAssetManager, TuyaOpenMQ
BASE = "https://openapi.tuyaeu.com"
client_api = TuyaOpenAPI(
    BASE, access_id, secret, auth_type=AuthType.CUSTOM
)
asset_client = TuyaAssetManager(client_api)
iot_hub = TuyaOpenMQ(client_api)
client_api.connect(username, password)
def event(d):
    print(d) # No longer prints after the error was raised.

iot_hub.add_message_listener(event)
iot_hub.start()

After running you would just have to change the name of any device.

If you need more code, the full code is on my repository: https://github.com/InterStella0/discord-iot-bot

InterStella0 commented 5 months ago

Seems like this cannot be reproduced as of anymore.