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

Update scanner.py #521

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions tinytuya/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def get_ip_to_broadcast():
ip_to_broadcast['255.255.255.255'] = getmyIP()
return ip_to_broadcast

def send_discovery_request( iface_list=None ):
def send_discovery_request(iface_list=None):
close_sockets = False

if not iface_list:
Expand All @@ -208,32 +208,38 @@ def send_discovery_request( iface_list=None ):
client_bcast_addrs = get_ip_to_broadcast()
for bcast in client_bcast_addrs:
addr = client_bcast_addrs[bcast]
iface_list[addr] = { 'broadcast': bcast }
iface_list[addr] = {'broadcast': bcast}

for address in iface_list:
iface = iface_list[address]
if 'socket' not in iface:
iface['socket'] = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
iface['socket'].setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
iface['socket'].bind( (address,0) )
try:
print(f"Attempting to bind to address: {address}")
iface['socket'] = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
iface['socket'].setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
iface['socket'].bind((address, 0))
except socket.gaierror as e:
print(f"Failed to bind to {address}: {e}")
continue
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR @blitzu ! I'm wondering, why use print() instead of log.debug() and log.error() here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what chatgpt gave me. i had an error on using scanner.py and this was the solution from chatgpt and it works


if 'payload' not in iface:
bcast = json.dumps( {"from":"app","ip":address} ).encode()
bcast_msg = tinytuya.TuyaMessage( 0, tinytuya.REQ_DEVINFO, None, bcast, 0, True, tinytuya.PREFIX_6699_VALUE, True )
iface['payload'] = tinytuya.pack_message( bcast_msg, hmac_key=tinytuya.udpkey )
bcast = json.dumps({"from": "app", "ip": address}).encode()
bcast_msg = tinytuya.TuyaMessage(0, tinytuya.REQ_DEVINFO, None, bcast, 0, True, tinytuya.PREFIX_6699_VALUE, True)
iface['payload'] = tinytuya.pack_message(bcast_msg, hmac_key=tinytuya.udpkey)

if 'port' not in iface:
iface['port'] = 7000

log.debug( 'Sending discovery broadcast from %r to %r on port %r', address, iface['broadcast'], iface['port'] )
log.debug('Sending discovery broadcast from %r to %r on port %r', address, iface['broadcast'], iface['port'])
# the official app always sends it twice, so do the same
iface['socket'].sendto( iface['payload'], (iface['broadcast'], iface['port']) )
iface['socket'].sendto( iface['payload'], (iface['broadcast'], iface['port']) )
iface['socket'].sendto(iface['payload'], (iface['broadcast'], iface['port']))
iface['socket'].sendto(iface['payload'], (iface['broadcast'], iface['port']))

if close_sockets:
iface['socket'].close()
del iface['socket']


class KeyObj(object):
def __init__( self, gwId, key ):
self.gwId = gwId
Expand Down
Loading