jasonacox / tinytuya

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

Zigbee Gateway Integration #245

Closed marconvtr closed 1 year ago

marconvtr commented 1 year ago

Greetings, testing this python module I could not get the example scripts to work, my main goal is to work with the device locally, gw.receive() always return None

bf2b9acc766177d359zlbp - GW Unique ID (virtual id) bf6765b23ab9d70d6dprom - Device Unique ID (virtual id)

gateway is SM310 Zigbee Hub, device is a generic tuya on-off 220v power relay.

import tinytuya
import time
# Zigbee Gateway support uses a parent/child model where a parent gateway device is
#  connected and then one or more children are added.

# Configure the parent device
gw = tinytuya.Device( 'bf2b9acc766177d359zlbp', address="192.168.1.75", local_key='d78bb4910f6dd3a4', persist=True, version=3.3 )

print( 'GW IP found:', gw.address )

# Configure one or more children.  Every dev_id must be unique!
zigbee1 = tinytuya.OutletDevice('bf6765b23ab9d70d6dprom', parent=gw)

print(zigbee1.status())

print(" > Begin Monitor Loop <")
pingtime = time.time() + 9

while(True):
    if( pingtime <= time.time() ):
        payload = gw.generate_payload(tinytuya.HEART_BEAT)
        gw.send(payload)
        pingtime = time.time() + 9

    # receive from the gateway object to get updates for all sub-devices
    print('recv:')
    data = gw.receive()
    print( data )

    # data['device'] contains a reference to the device object
    if data and 'device' in data and data['device'] == zigbee1:
        print('toggling device state')
        time.sleep(1)
        if data['dps']['1']:
            data['device'].turn_off(nowait=True)
        else:
            data['device'].turn_on(nowait=True)
uzlonewolf commented 1 year ago

What's it doing when that example runs?

Please add tinytuya.set_debug() before the gw = ... line and post the output.

marconvtr commented 1 year ago

my device was not respoding, I had to reset and re-make my network and now the api works.