Skip to content

Commit 9c4648d

Browse files
authored
Merge pull request #517 from jasonacox/v1.15.0
Add netifaces as an install requirement
2 parents 828d762 + a1e35a2 commit 9c4648d

File tree

7 files changed

+63
-11
lines changed

7 files changed

+63
-11
lines changed

RELEASE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# RELEASE NOTES
22

3-
## v1.14.1 - Scanner Fixes
3+
## v1.15.0 - Scanner Fixes
44

55
* Fix force-scanning bug in scanner introduced in last release and add broadcast request feature to help discover Tuya version 3.5 devices by @uzlonewolf in https://github.com/jasonacox/tinytuya/pull/511.
66
* Server p12 updates:

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
cryptography>=3.1 # Encryption - AES can also be provided via PyCryptodome or pyaes or pyca/cryptography
33
requests # Used for Setup Wizard - Tuya IoT Platform calls
44
colorama # Makes ANSI escape character sequences work under MS Windows.
5+
netifaces # Used to get the IP address of the local machine for scanning for devices.

server/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ WORKDIR /app
33
ENV PYTHONUNBUFFERED 1
44
ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
55
RUN pip3 install cryptography pycryptodome --no-build-isolation
6-
RUN pip3 install --no-cache-dir tinytuya
6+
RUN pip3 install --no-cache-dir netifaces tinytuya
77
COPY . .
88
CMD ["python3", "server.py"]
99
EXPOSE 8888
1010
EXPOSE 6666/udp
1111
EXPOSE 6667/udp
12+
EXPOSE 7000/udp

server/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ Starting threads...
5858
-p 8888:8888 \
5959
-p 6666:6666/udp \
6060
-p 6667:6667/udp \
61-
-e DEBUG='no' \
61+
-p 7000:7000/udp \
62+
--network host \
63+
-e DEBUGMODE='no' \
64+
-e HOST='192.168.0.100' \
6265
-v $PWD/devices.json:/app/devices.json \
6366
-v $PWD/tinytuya.json:/app/tinytuya.json \
6467
--name tinytuya \

server/server.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@
6262
from tinytuya import scanner
6363
import os
6464

65-
BUILD = "p12"
65+
BUILD = "p13"
6666

6767
# Defaults from Environment
6868
APIPORT = int(os.getenv("APIPORT", "8888"))
69-
DEBUGMODE = os.getenv("DEBUGMODE", "False").lower() == "true"
7069
DEVICEFILE = os.getenv("DEVICEFILE", tinytuya.DEVICEFILE)
7170
SNAPSHOTFILE = os.getenv("SNAPSHOTFILE", tinytuya.SNAPSHOTFILE)
7271
CONFIGFILE = os.getenv("CONFIGFILE", tinytuya.CONFIGFILE)
@@ -81,6 +80,14 @@
8180
RETRYCOUNT = int(os.getenv("RETRYCOUNT", "5"))
8281
SAVEDEVICEFILE = os.getenv("SAVEDEVICEFILE", "True").lower() == "true"
8382
DEBUGMODE = 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
8693
log = 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)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
INSTALL_REQUIRES = [
1010
'requests', # Used for Setup Wizard - Tuya IoT Platform calls
1111
'colorama', # Makes ANSI escape character sequences work under MS Windows.
12+
'netifaces', # Used for device discovery
1213
]
1314

1415
CHOOSE_CRYPTO_LIB = [

tinytuya/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
# Colorama terminal color capability for all platforms
124124
init()
125125

126-
version_tuple = (1, 14, 1)
126+
version_tuple = (1, 15, 0)
127127
version = __version__ = "%d.%d.%d" % version_tuple
128128
__author__ = "jasonacox"
129129

0 commit comments

Comments
 (0)