Skip to content

Commit

Permalink
Also use psutil to get the force-scan IP list if netifaces is not ava…
Browse files Browse the repository at this point in the history
…ilable
  • Loading branch information
uzlonewolf committed Jul 7, 2024
1 parent 0b19990 commit c6dba30
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions tinytuya/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ def getmyIP():
return '%s.%s.%s.0/24' % tuple(r[:3])

def getmyIPs( term, verbose, ask ):
if NETIFLIBS:
return getmyIPs_via_netifaces( term, verbose, ask )
if PSULIBS:
return getmyIPs_via_psutil( term, verbose, ask )
return None

def getmyIPs_via_netifaces( term, verbose, ask ):
ips = {}
interfaces = netifaces.interfaces()
try:
Expand Down Expand Up @@ -138,6 +145,30 @@ def getmyIPs( term, verbose, ask ):
ips[k] = True
return ips.keys()

def getmyIPs_via_psutil( term, verbose, ask ):
ips = {}
interfaces = psutil.net_if_addrs()
for interface in interfaces:
addresses = interfaces[interface]
for addr in addresses:
if addr.family != socket.AF_INET:
continue
k = str(ipaddress.IPv4Interface(addr.address+'/'+addr.netmask).network)
if k[:4] == '127.':
# skip the loopback interface
continue
if ask:
if ask != 2:
answer = input( '%sScan network %s from interface %s?%s ([Y]es/[n]o/[a]ll yes): ' % (term.bold, k, str(interface), term.normal) )
if answer[0:1].lower() == 'a':
ask = 2
elif answer.lower().find('n') >= 0:
continue
if verbose:
print(term.dim + 'Adding Network', k, 'to the force-scan list')
ips[k] = True
return ips.keys()

def get_ip_to_broadcast():
ip_to_broadcast = {}

Expand Down Expand Up @@ -1252,10 +1283,10 @@ def tuyaLookup(deviceid):
add_connected = False

if add_connected:
if not NETIFLIBS:
if (not NETIFLIBS) and (not PSULIBS):
print(term.alert +
' NOTE: netifaces module not available, multi-interface machines will be limited.\n'
' (Requires: pip install netifaces)\n' + term.dim)
' NOTE: neither module netifaces nor module psutil are available, multi-interface machines will be limited.\n'
' (Requires: `pip install netifaces` or `pip install psutil`)\n' + term.dim)
try:
ip = getmyIP()
networks.append( ip )
Expand Down

0 comments on commit c6dba30

Please sign in to comment.