Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get info without scanning the network #21

Open
ReneDyhr opened this issue Jul 31, 2021 · 1 comment
Open

Get info without scanning the network #21

ReneDyhr opened this issue Jul 31, 2021 · 1 comment

Comments

@ReneDyhr
Copy link

Is there a way where I can scan the device for new data without having to scan the whole network all the time for devices? I do know the device id, ip etc.

@jasonacox
Copy link
Owner

Hi @ReneDyhr there are several ways to do this. If you just want a continuous loop monitor listening to updates, you can use this (from TinyTuya examples):

# TinyTuya Example
# -*- coding: utf-8 -*-
"""
 TinyTuya - Example script to monitor state changes with Tuya devices.

 Author: Jason A. Cox
 For more information see https://github.com/jasonacox/tinytuya

"""
import tinytuya

# tinytuya.set_debug(True)

d = tinytuya.OutletDevice('DEVICEID', 'DEVICEIP', 'DEVICEKEY')
d.set_version(3.3)
d.set_socketPersistent(True)

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

print(" > Begin Monitor Loop <")
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)

    # Uncomment if you want the monitor to constantly request status - otherwise you
    # will only get updates when state changes
    print(" > Send Request for Status < ")
    payload = d.generate_payload(tinytuya.DP_QUERY)
    d.send(payload)

    # Uncomment if your device provides power monitoring data but it is not updating
    # Some devices require a UPDATEDPS command to force measurements of power.
    # print(" > Send DPS Update Request < ")
    # Most devices send power data on DPS indexes 18, 19 and 20
    # payload = d.generate_payload(tinytuya.UPDATEDPS,['18','19','20'])
    # Some Tuya devices will not accept the DPS index values for UPDATEDPS - try:
    # payload = d.generate_payload(tinytuya.UPDATEDPS)
    # d.send(payload)

If you just want to grab the status (power data, etc) you can use something as simple as this:

import tinytuya

# Connect to the device - replace with real values
d=tinytuya.OutletDevice(DEVICEID, DEVICEIP, DEVICEKEY)
d.set_version(3.3)

# Get the status of the device 
# e.g. {'devId': '0071299988f9376255b', 'dps': {'1': True, '3': 208, '101': False}}
data = d.get_status()
print(data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants