6262from tinytuya import scanner
6363import os
6464
65- BUILD = "p12 "
65+ BUILD = "p13 "
6666
6767# Defaults from Environment
6868APIPORT = int (os .getenv ("APIPORT" , "8888" ))
69- DEBUGMODE = os .getenv ("DEBUGMODE" , "False" ).lower () == "true"
7069DEVICEFILE = os .getenv ("DEVICEFILE" , tinytuya .DEVICEFILE )
7170SNAPSHOTFILE = os .getenv ("SNAPSHOTFILE" , tinytuya .SNAPSHOTFILE )
7271CONFIGFILE = os .getenv ("CONFIGFILE" , tinytuya .CONFIGFILE )
8180RETRYCOUNT = int (os .getenv ("RETRYCOUNT" , "5" ))
8281SAVEDEVICEFILE = os .getenv ("SAVEDEVICEFILE" , "True" ).lower () == "true"
8382DEBUGMODE = os .getenv ("DEBUGMODE" , "no" ).lower () == "yes"
83+ HOST = os .getenv ("HOST" , None )
84+ BROADCAST = os .getenv ("BROADCAST" , None )
85+
86+ # Set up broadcast address
87+ if HOST and not BROADCAST :
88+ BROADCAST = HOST .split ('.' )
89+ BROADCAST [3 ] = '255'
90+ BROADCAST = '.' .join (BROADCAST )
8491
8592# Logging
8693log = logging .getLogger (__name__ )
@@ -275,6 +282,7 @@ def tuyalisten(port):
275282 """
276283 Thread to listen for Tuya devices UDP broadcast on port
277284 """
285+ global BROADCAST
278286 log .debug ("Started tuyalisten thread on %d" , port )
279287 print (" - tuyalisten %d Running" % port )
280288 last_broadcast = 0
@@ -290,11 +298,20 @@ def tuyalisten(port):
290298 client .bind (("" , port ))
291299 client .settimeout (5 )
292300
301+ iface_list = None
302+ if HOST :
303+ iface_list = {}
304+ iface_list [HOST ] = { 'broadcast' : BROADCAST }
305+ log .debug ("Using iface_list: %r" , iface_list )
306+
293307 while (running ):
294308 if port == UDPPORTAPP and time .time () - last_broadcast > scanner .BROADCASTTIME :
295- log .debug ("Sending discovery request to all 3.5 devices on the network" )
309+ log .debug ("Sending discovery request to all 3.5 devices on the network" )
310+ if HOST :
311+ scanner .send_discovery_request (iface_list )
312+ else :
296313 scanner .send_discovery_request ()
297- last_broadcast = time .time ()
314+ last_broadcast = time .time ()
298315 try :
299316 data , addr = client .recvfrom (4048 )
300317 except (KeyboardInterrupt , SystemExit ) as err :
@@ -653,6 +670,28 @@ def api(port):
653670 "\n %sTinyTuya %s(Server)%s [%s%s]\n "
654671 % (bold , normal , dim , tinytuya .__version__ , BUILD )
655672 )
673+
674+ # IP Address
675+ print ("%sConfiguration Settings:" % dim )
676+ if HOST :
677+ print (" Using Host IP: %s%s%s" % (cyan , HOST , dim ))
678+ if BROADCAST :
679+ print (" Using Broadcast IP: %s%s%s" % (cyan , BROADCAST , dim ))
680+ print (" UDP Ports: %s%d%s, %s%d%s, %s%d%s" % (cyan , UDPPORT , dim , cyan , UDPPORTS , dim , cyan , UDPPORTAPP , dim ))
681+ print (" TCP Port: %s%d%s" % (cyan , TCPPORT , dim ))
682+ print (" API Port: %s%d%s" % (cyan , APIPORT , dim ))
683+ print (" Device File: %s%s%s" % (cyan , DEVICEFILE , dim ))
684+ print (" Snapshot File: %s%s%s" % (cyan , SNAPSHOTFILE , dim ))
685+ print (" Config File: %s%s%s" % (cyan , CONFIGFILE , dim ))
686+ print (" TCP Timeout: %s%s%s" % (cyan , TCPTIMEOUT , dim ))
687+ print (" UDP Timeout: %s%s%s" % (cyan , TIMEOUT , dim ))
688+ print (" Max Devices: %s%s%s" % (cyan , MAXCOUNT , dim ))
689+ print (" Retry Time: %s%s%s" % (cyan , RETRYTIME , dim ))
690+ print (" Retry Count: %s%s%s" % (cyan , RETRYCOUNT , dim ))
691+ print (" Save Device File: %s%s%s" % (cyan , SAVEDEVICEFILE , dim ))
692+ print (" Debug Mode: %s%s%s" % (cyan , DEBUGMODE , dim ))
693+ print ("" )
694+
656695 if len (tuyadevices ) > 0 :
657696 print ("%s[Loaded devices.json - %d devices]%s\n " % (dim , len (tuyadevices ), normal ))
658697 else :
@@ -681,9 +720,10 @@ def api(port):
681720 # maxdevices=0)
682721 try :
683722 found = scanner .devices (forcescan = True , verbose = False , discover = False , assume_yes = True , tuyadevices = tuyadevices )
684- except :
685- log .error ("Error during scanner.devices()" )
723+ except Exception as err :
724+ log .error (f "Error during scanner.devices() { err } " )
686725 found = []
726+ forcescandone = True
687727 print (f" - ForceScan: Found { len (found )} devices" )
688728 for f in found :
689729 log .debug (f" - { found [f ]} " )
@@ -711,7 +751,6 @@ def api(port):
711751 # If fetching the key failed, save it to retry later
712752 retrydevices [result ["id" ]] = RETRYCOUNT
713753 newdevices .append (result ["id" ])
714- forcescandone = True
715754
716755 if cloudsync :
717756 cloudsync = False
@@ -765,3 +804,10 @@ def api(port):
765804 print ("Stopping threads..." )
766805 log .debug ("Stoppping threads" )
767806 requests .get ('http://localhost:%d/stop' % APIPORT , timeout = 5 )
807+ except Exception as err :
808+ log .error (f"Error in main loop: { err } " )
809+ running = False
810+ # Close down API thread
811+ print ("Stopping threads..." )
812+ log .debug ("Stoppping threads" )
813+ requests .get ('http://localhost:%d/stop' % APIPORT , timeout = 5 )
0 commit comments