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

deviceScan / wizard works on cmd but not running the script #596

Open
JONTK123 opened this issue Feb 16, 2025 · 5 comments
Open

deviceScan / wizard works on cmd but not running the script #596

JONTK123 opened this issue Feb 16, 2025 · 5 comments

Comments

@JONTK123
Copy link

Hello

The following script does not return devices and their IPs

import tinytuya
import logging

logging.basicConfig(level=logging.DEBUG)

scan_result = tinytuya.deviceScan()
print(scan_result)

(Just a test)

DEBUG:tinytuya.scanner:loaded=devices.json [7 devices]
DEBUG:tinytuya.scanner:Listening for Tuya devices on UDP ports 6666, 6667 and 7000
DEBUG:tinytuya.scanner:Sending discovery broadcast from '192.168.15.10' to '255.255.255.255' on port 7000
DEBUG:tinytuya.scanner:Received valid UDP packet: {'from': 'app', 'ip': '192.168.15.10'}
DEBUG:tinytuya.scanner:Received valid UDP packet: {'from': 'app', 'ip': '192.168.15.10'}
DEBUG:tinytuya.scanner:Sending discovery broadcast from '192.168.15.10' to '255.255.255.255' on port 7000
DEBUG:tinytuya.scanner:Received valid UDP packet: {'from': 'app', 'ip': '192.168.15.10'}
DEBUG:tinytuya.scanner:Received valid UDP packet: {'from': 'app', 'ip': '192.168.15.10'}
DEBUG:tinytuya.scanner:Sending discovery broadcast from '192.168.15.10' to '255.255.255.255' on port 7000
DEBUG:tinytuya.scanner:Received valid UDP packet: {'from': 'app', 'ip': '192.168.15.10'}
DEBUG:tinytuya.scanner:Received valid UDP packet: {'from': 'app', 'ip': '192.168.15.10'}
{}
DEBUG:tinytuya.scanner:Scan complete with 0 devices found

Process finished with exit code 0

However, when I run the commands:

python -m tinytuya wizard
and/or

python -m tinytuya scan

It works and returns the devices with their Ip etc etc... ( Just for examplifing, I am working with Wi-fi smart switch )

I wonder why the script does not work but the cmd command works...

I have tried the following:

  • Uninstall tinytuya, install again
  • Different version of tinytuya
  • Different pc
  • Same steps on different pc
  • Disabling firewall on UDP ports 6666, 66667 and 7000
  • Installing netifaces with the C++ compiler dependecies but still does not work

However, I noticed that using forcescan=True it does work and return the ip for some devices but for some devices and for some attributes such as name key etc returns as ' '

scan_result = tinytuya.deviceScan(forcescan=True)
'mac': '', 'name': '', 'key': '', 'gwId': '', 'active': '' (Example of return)

Any advice?

Thanks!

@JONTK123
Copy link
Author

Another thing I forgot to mention. I tried the same script couple days ago and It worked just fine... Even tho I havent even touched this...

@JONTK123
Copy link
Author

JONTK123 commented Mar 6, 2025

Anyone can help me? ...

@uzlonewolf
Copy link
Collaborator

tinytuya.deviceScan() and python -m tinytuya scan both call the exact same function, so if one works then both should work. The only time I've seen tinytuya.deviceScan() not work is when it's being run inside a Docker container or otherwise not allowed direct access to the network - Tuya devices use broadcast packets for device discovery, so they do not transverse routers or similar network breaks.

forcescan is a crude hack that I'm surprised works as well as it does. Because it has no idea what version a device is or what its device ID is, it blindly tries every combination of secret key and device version until it gets a response. Some devices do not like this and stop responding for a bit. I'd avoid using it if at all possible.

Does running with tinytuya.deviceScan( verbose=True ) show any broadcasts being received?

@uzlonewolf
Copy link
Collaborator

Oh yeah, running Home Assistant on the same machine can also prevent the discovery packets form being received as the operating system usually only gives UDP broadcast packets to a single program. Some devices also stop sending broadcasts if a LAN connection is already active with them.

@JONTK123
Copy link
Author

JONTK123 commented Mar 9, 2025

Heyy @uzlonewolf thanks for your reply, after like 1 million tries, I finally fixed it. The problem was windows firewall... I had no Idea that this could happen...

But I wonder why, at first, everything was fine until this happened.

Also, my application is running in a container and scan works fine.

I have one last question, is there any way to know if a device works locally before testing? I want to buy a Temperature Sensor and I need to control it locally but the one I bought does not work apparently. I am thinking to use this [Zigbee2MQTT].

Thanks!

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