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

Add CLI Support for pipx #502

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/contrib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
python-version: ["3.6", "3.7", "3.8"]
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: "actions/checkout@v2"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
python-version: ["3.5", "3.6", "3.7", "3.8", "3.9", "3.10"]
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: "actions/checkout@v3"
Expand Down
5 changes: 5 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# RELEASE NOTES

## v1.13.3 - PIPX Support

* Add support for `pipx install tinytuya` as raised by @felipecrs in https://github.com/jasonacox/tinytuya/issues/500 allowing for easier CLI use.
* Possible breaking change: Running `tinytuya` by itself will now produce a "Usage" page instead of running a scan. Use `tinytuya scan` or `python -m tinytuya scan`.

## v1.13.2 - Contrib Updates

* Add example for XmCosy+ RGBW patio string lights by @bikerglen in https://github.com/jasonacox/tinytuya/pull/445
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build-system]
requires = ["setuptools", "colorama", "requests", "cryptography"]
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
url='https://github.com/jasonacox/tinytuya',
packages=setuptools.find_packages(exclude=("sandbox",)),
install_requires=INSTALL_REQUIRES,
entry_points={"console_scripts": ["tinytuya=tinytuya.__main__:main"]},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down
2 changes: 2 additions & 0 deletions tinytuya/BulbDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def _hexvalue_to_rgb(hexvalue, bulb="A"):
Args:
hexvalue(string): The hex representation generated by BulbDevice._rgb_to_hexvalue()
"""
r, g, b = 0, 0, 0
if bulb == "A":
r = int(hexvalue[0:2], 16)
g = int(hexvalue[2:4], 16)
Expand All @@ -203,6 +204,7 @@ def _hexvalue_to_hsv(hexvalue, bulb="A"):
Args:
hexvalue(string): The hex representation generated by BulbDevice._rgb_to_hexvalue()
"""
h, s, v = 0, 0, 0
if bulb == "A":
h = int(hexvalue[7:10], 16) / 360.0
s = int(hexvalue[10:12], 16) / 255.0
Expand Down
160 changes: 81 additions & 79 deletions tinytuya/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,92 +19,94 @@
from . import wizard
from . import scanner

retries = None
state = 0
color = True
force = False
force_list = []
last_force = False
broadcast_listen = True
assume_yes = False
def main():
retries = None
state = 10
color = True
force = False
force_list = []
last_force = False
broadcast_listen = True
assume_yes = False

for i in sys.argv:
if i==sys.argv[0]:
continue
this_force = False
if i.lower() == "wizard":
state = 1
elif i.lower() == "scan":
state = 0
elif i.lower() == "-nocolor":
color = False
elif i.lower() == "-force" or i.lower() == "-f":
force = True
this_force = True
elif i.lower() == "-no-broadcasts":
broadcast_listen = False
elif i.lower() == "snapshot":
state = 2
elif i.lower() == "devices":
state = 3
elif i.lower() == "json":
state = 4
elif i.lower() == "-yes" or i.lower() == "-y":
assume_yes = True
elif i.lower() == "-debug" or i.lower() == "-d":
tinytuya.set_debug(True)
elif last_force and len(i) > 6:
this_force = True
force_list.append( i )
else:
try:
retries = int(i)
except:
state = 10
for i in sys.argv:
if i==sys.argv[0]:
continue
this_force = False
if i.lower() == "wizard":
state = 1
elif i.lower() == "scan":
state = 0
elif i.lower() == "-nocolor":
color = False
elif i.lower() == "-force" or i.lower() == "-f":
force = True
this_force = True
elif i.lower() == "-no-broadcasts":
broadcast_listen = False
elif i.lower() == "snapshot":
state = 2
elif i.lower() == "devices":
state = 3
elif i.lower() == "json":
state = 4
elif i.lower() == "-yes" or i.lower() == "-y":
assume_yes = True
elif i.lower() == "-debug" or i.lower() == "-d":
tinytuya.set_debug(True)
elif last_force and len(i) > 6:
this_force = True
force_list.append( i )
else:
try:
retries = int(i)
except:
state = 10

last_force = this_force
last_force = this_force

if force and len(force_list) > 0:
force = force_list
if force and len(force_list) > 0:
force = force_list

# State 0 = Run Network Scan
if state == 0:
scanner.scan(scantime=retries, color=color, forcescan=force, discover=broadcast_listen, assume_yes=assume_yes)
# State 0 = Run Network Scan
if state == 0:
scanner.scan(scantime=retries, color=color, forcescan=force, discover=broadcast_listen, assume_yes=assume_yes)

# State 1 = Run Setup Wizard
if state == 1:
wizard.wizard(color=color, retries=retries, forcescan=force, quicklist=assume_yes)
# State 1 = Run Setup Wizard
if state == 1:
wizard.wizard(color=color, retries=retries, forcescan=force, quicklist=assume_yes)

# State 2 = Snapshot Display and Scan
if state == 2:
scanner.snapshot(color=color)
# State 2 = Snapshot Display and Scan
if state == 2:
scanner.snapshot(color=color)

# State 3 = Scan All Devices
if state == 3:
scanner.alldevices(color=color, scantime=retries)
# State 3 = Scan All Devices
if state == 3:
scanner.alldevices(color=color, scantime=retries)

# State 4 = Scan All Devices
if state == 4:
scanner.snapshotjson()
# State 4 = Scan All Devices
if state == 4:
scanner.snapshotjson()

# State 10 = Show Usage
if state == 10:
print("TinyTuya [%s]\n" % (tinytuya.version))
print("Usage:\n")
print(" python -m tinytuya <command> [<max_time>] [-debug] [-nocolor] [-force [192.168.0.0/24 192.168.1.0/24 ...]] [-h]")
print("")
print(" wizard Launch Setup Wizard to get Tuya Local KEYs.")
print(" scan Scan local network for Tuya devices.")
print(" devices Scan all devices listed in devices.json file.")
print(" snapshot Scan devices listed in snapshot.json file.")
print(" json Scan devices listed in snapshot.json file [JSON].")
print(" <max_time> Maximum time to find Tuya devices [Default=%s]" % tinytuya.SCANTIME)
print(" -nocolor Disable color text output.")
print(" -force Force network scan of device IP addresses based on format:")
print(" [net1/mask1 net2/mask2 ...] Auto-detects if none provided.")
print(" -no-broadcasts Ignore broadcast packets when force scanning.")
print(" -debug Activate debug mode.")
print(" -h Show usage.")
print("")
# State 10 = Show Usage
if state == 10:
print("TinyTuya [%s]\n" % (tinytuya.version))
print("Usage:\n")
print(" python -m tinytuya <command> [<max_time>] [-debug] [-nocolor] [-force [192.168.0.0/24 192.168.1.0/24 ...]] [-h]")
print("")
print(" wizard Launch Setup Wizard to get Tuya Local KEYs.")
print(" scan Scan local network for Tuya devices.")
print(" devices Scan all devices listed in devices.json file.")
print(" snapshot Scan devices listed in snapshot.json file.")
print(" json Scan devices listed in snapshot.json file [JSON].")
print(" <max_time> Maximum time to find Tuya devices [Default=%s]" % tinytuya.SCANTIME)
print(" -nocolor Disable color text output.")
print(" -force Force network scan of device IP addresses based on format:")
print(" [net1/mask1 net2/mask2 ...] Auto-detects if none provided.")
print(" -no-broadcasts Ignore broadcast packets when force scanning.")
print(" -debug Activate debug mode.")
print(" -h Show usage.")
print("")

# End
if __name__ == "__main__":
main()
5 changes: 3 additions & 2 deletions tinytuya/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
pass

for clib in ('pyca/cryptography', 'PyCryptodomex', 'PyCrypto', 'pyaes'):
Crypto = AES = CRYPTOLIB = None
Crypto = Crypto_modes = AES = CRYPTOLIB = None
try:
if clib == 'pyca/cryptography': # https://cryptography.io/en/latest/
from cryptography import __version__ as Crypto_version
Expand Down Expand Up @@ -123,7 +123,7 @@
# Colorama terminal color capability for all platforms
init()

version_tuple = (1, 13, 2)
version_tuple = (1, 13, 3)
version = __version__ = "%d.%d.%d" % version_tuple
__author__ = "jasonacox"

Expand Down Expand Up @@ -496,6 +496,7 @@ def pack_message(msg, hmac_key=None):

def unpack_message(data, hmac_key=None, header=None, no_retcode=False):
"""Unpack bytes into a TuyaMessage."""
crc_good = False
if header is None:
header = parse_header(data)

Expand Down
1 change: 1 addition & 0 deletions tinytuya/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ def tuyaLookup(deviceid):
)

#debug_ips = ['172.20.10.144', '172.20.10.91', '172.20.10.51', '172.20.10.136']
scan_ips = None
debug_ips = []
networks = []
scanned_devices = {}
Expand Down
Loading