jasonacox / tuyapower

Python module to read status and energy monitoring data from Tuya based WiFi smart devices. This includes state (on/off), current (mA), voltage (V), and power (wattage).
MIT License
136 stars 20 forks source link

smart energy meter not returning data #29

Closed judester1 closed 1 year ago

judester1 commented 1 year ago

Hi, could you please help as I am trying to access/record data from a smart energy meter I recently installed.

I have setup my Tuya IOT project to get client id and secret so I can run the wizard and and scan successfully to generate tuya-raw.json (includes a valid device status report) and devices.json to provide valid local key. API calls in IOT project for my device all work successfully.

However, when trying the following code to access device status locally (I use device id, IP address, local key) appears to get no response:

import tinytuya
tinytuya.set_debug(True)

a = tinytuya.OutletDevice('bf3e7252097f07dbd5XXXX', '192.168.xx.xx', 'XXXXX', 'device22')
a.set_version(3.3)
a.set_dpsUsed({"1": None})  # This needs to be a datapoint available on the device
data =  a.status()
print(data)

I'm getting the following error but can't tell if no data returned or data returned is not readable.

[31;1mDEBUG:TinyTuya [1.12.7]

DEBUG:Python 3.11.0 (v3.11.0:deaf509e8f, Oct 24 2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
DEBUG:Using PyCrypto (3, 17)
DEBUG:status() entry (dev_type is device22)
DEBUG:final payload_dict for 'bf3e7252097f07dbd5iejq' ('v3.3'/'device22'): {1: {'command': {'gwId': '', 'devId': '', 'uid': '', 't': ''}}, 7: {'command': {'devId': '', 'uid': '', 't': ''}}, 8: {'command': {'gwId': '', 'devId': ''}}, 9: {'command': {'gwId': '', 'devId': ''}}, 10: {'command': {'devId': '', 'uid': '', 't': ''}, 'command_override': 13}, 13: {'command': {'devId': '', 'uid': '', 't': ''}}, 16: {'command': {'devId': '', 'uid': '', 't': ''}}, 18: {'command': {'dpId': [18, 19, 20]}}, 64: {'command': {'reqType': '', 'data': {}}}}
DEBUG:building command 10 payload=b'{"devId":"bf3e7252097f07dbd5iejq","uid":"bf3e7252097f07dbd5iejq","t":"1684324838","dps":{"1":null}}'
DEBUG:sending payload
DEBUG:payload encrypted=b'000055aa000000010000000d00000087332e33000000000000000000000000ec61a4ee075ac904dbef0abfb86b456cb988ff2a9edc781dd58a3cc8703f3206d46ce06a95515979d48199991abbd94f8e58484431fe861cdc0ead3761a2057b89d8c712f1199313a522f2a1fa0012000d650534c7ce6aeab4702dac17b2faf445c1f6ef4ed1e894279b8f92e2462750da1953af0000aa55'
DEBUG:_recv_all(): no data? b''
DEBUG:_recv_all(): no data? b''
DEBUG:Error decoding received data - read retry 0/5
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tinytuya/core.py", line 1008, in _send_receive
    rmsg = self._receive()
           ^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tinytuya/core.py", line 898, in _receive
    data = self._recv_all( min_len )
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tinytuya/core.py", line 879, in _recv_all
    raise DecodeError('No data received - connection closed?')
tinytuya.core.DecodeError: No data received - connection closed?
judester1 commented 1 year ago

I got it working/returning data by changing to version 3.4 and using code below:

import tinytuya

d = tinytuya.OutletDevice('bf3e7252097f07dbd5iejq', '192.168.0.60', 'XXXXXXX')

d.set_version(3.4)
d.set_socketPersistent(True)

print(" > Send Request for Status < ")
payload = d.generate_payload(tinytuya.DP_QUERY)
d.send(payload)

while(True):
    # See if any data is available
    data = d.receive()
    print('Received Payload: %r' % data)

    # Send keyalive heartbeat
    print(" > Send Heartbeat Ping < ")
    payload = d.generate_payload(tinytuya.HEART_BEAT)
    d.send(payload)
jasonacox commented 1 year ago

Hi @judester1

Did it report as being a 3.3 device when you ran python3 -m tinytuya scan? Also, does your original code work when you change set_version() to 3.4?

judester1 commented 1 year ago

Hi, thanks for your response.

Scan reported as 3.4 in devices.json file. Original code works if I drop the extra device22 parameter ... also works without the set_dpsused line.

import tinytuya
tinytuya.set_debug(True)

a = tinytuya.OutletDevice('bf3e7252097f07dbd5XXXX', '192.168.xx.xx', 'XXXXX')
a.set_version(3.4)
a.set_dpsUsed({"1": None})  # this line is optional/does not change data result
data =  a.status()
print(data)
jasonacox commented 1 year ago

Thanks @judester1 - does tuyapower still not work or should we close this issue?

As a note on tinytuya, you can also specify the version in the class initialization:

import tinytuya
tinytuya.set_debug(True)

a = tinytuya.OutletDevice('bf3e7252097f07dbd5XXXX', '192.168.xx.xx', 'XXXXX', version=3.4)
data =  a.status()
print(data)