tuya / tuya-iot-python-sdk

Tuya IoT Python SDK for Tuya Open API.
MIT License
130 stars 49 forks source link

Error when I try to retrive data #61

Open arnauddomes opened 2 years ago

arnauddomes commented 2 years ago

Hello,

I use that guide to retrieve data from my Fingerbot: https://www.home-assistant.io/integrations/tuya/

I also use that repo to extract the data: https://github.com/redphx/tuya-local-key-extractor

When I use the extract.py file I get that error:

Exception in thread Thread-1: Traceback (most recent call last): openapi <tuya_iot.openapi.TuyaOpenAPI object at 0x7f7c4ccb24c0> openmq <TuyaOpenMQ(Thread-1, started 140171782297344)> File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/openmq.py", line 158, in run self.__run_mqtt() File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/openmq.py", line 164, in __run_mqtt mq_config = self._get_mqtt_config() File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/openmq.py", line 67, in _get_mqtt_config "uid": self.api.token_info.uid, AttributeError: 'NoneType' object has no attribute 'uid' Traceback (most recent call last): File "./extract.py", line 31, in <module> device_manager.update_device_list_in_smart_home() File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/device.py", line 239, in update_device_list_in_smart_home response = self.api.get(f"/v1.0/users/{self.api.token_info.uid}/devices") AttributeError: 'NoneType' object has no attribute 'uid'

Here is the code I use:

import json
import logging
import os

from config import (
    ENDPOINT,
    APP,
    EMAIL,
    PASSWORD,
    ACCESS_ID,
    ACCESS_KEY,
)

from tuya_iot import (
    TuyaOpenAPI,
    AuthType,
    TuyaOpenMQ,
    TuyaDeviceManager,
)

openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, AuthType.SMART_HOME)
openapi.connect(EMAIL, PASSWORD, country_code=84, schema=APP.value)

openmq = TuyaOpenMQ(openapi)
openmq.start()

print('openapi {}'.format(openapi))
print('openmq {}'.format(openmq))
device_manager = TuyaDeviceManager(openapi, openmq)
device_manager.update_device_list_in_smart_home()

devices = []
for tuya_device in device_manager.device_map.values():
    device = {
        'device_id': tuya_device.id,
        'device_name': tuya_device.name,
        'product_id': tuya_device.product_id,
        'product_name': tuya_device.product_name,
        'category': tuya_device.category,
        'uuid': tuya_device.uuid,
        'local_key': tuya_device.local_key,
    }

    try:
        resp = openapi.get('/v1.0/iot-03/devices/factory-infos?device_ids={}'.format(tuya_device.id))
        factory_info = resp['result'][0]
        if 'mac' in factory_info:
            mac = ':'.join(factory_info['mac'][i:i + 2] for i in range(0, 12, 2))
            device['mac_address'] = mac
    except Exception as e:
        print(e)

    devices.append(device)

print(json.dumps(devices, indent=2))
os._exit(0)

I am on Ubuntu 20.04 and I install the python sdk with tha t command: pip3 install tuya-iot-py-sdk