jasonacox / tinytuya

Python API for Tuya WiFi smart devices using a direct local area network (LAN) connection or the cloud (TuyaCloud API).
MIT License
867 stars 157 forks source link

Rewrite main to use argparse and add additional options #503

Closed uzlonewolf closed 1 month ago

uzlonewolf commented 1 month ago

I just need to add a few comments to explain some of the code and bang on it a bit to make sure I didn't forget to implement any of the options and I think it's done.

Also closes #500

v0.14.0

Also closes #501

jasonacox commented 1 month ago

I like this. It includes my updates so I'm good pushing this one forward instead.

jasonacox commented 1 month ago

To test pipx from parent directory of tinytuya:

pipx install ./tinytuya

It required that I create a pyproject.toml to install dependencies in the build phase:

[build-system]
requires = ["setuptools", "colorama", "requests", "cryptography"]
jasonacox commented 1 month ago

I was able to pipx install this PR and started some testing...

$ tinytuya 
TinyTuya Setup Wizard [1.14.0]

    Enter API Key from tuya.com: 

It looks like default is wizard. Do we want that or the usage map?

tinytuya -h  
usage: python -m tinytuya [-h] [-debug] {wizard,scan,devices,snapshot,json} ...

TinyTuya [1.14.0]

positional arguments:
  {wizard,scan,devices,snapshot,json}

options:
  -h, --help            show this help message and exit
  -debug, -d            Enable debug messages

This was the old help - I think we can probably get some of the descriptors back in there:

TinyTuya [1.13.2]

Usage:

    python -m tinytuya <command> [<max_time>] [-debug] [-nocolor] [-force [192.168.0.0/24 192.168.1.0/24 ...]] [-h]

      wizard         Launch Setup Wizard to get Tuya Local KEYs.
      scan           Scan local network for Tuya devices.
      devices        Scan all devices listed in devices.json file.
      snapshot       Scan devices listed in snapshot.json file.
      json           Scan devices listed in snapshot.json file [JSON].
      <max_time>     Maximum time to find Tuya devices [Default=18]
      -nocolor       Disable color text output.
      -force         Force network scan of device IP addresses based on format:
                     [net1/mask1 net2/mask2 ...] Auto-detects if none provided.
      -no-broadcasts Ignore broadcast packets when force scanning.
      -debug         Activate debug mode.
      -h             Show usage.
uzlonewolf commented 1 month ago

It looks like default is wizard. Do we want that or the usage map?

I was just about to ask that. Although I'm leaning towards displaying the help, I'm fine either way. I made it auto-run the wizard since that's what it does now. What do you think would be better?

I think we can probably get some of the descriptors back in there

The issue is how argparse handles subparsers. I don't know of any good way to add them without having the command specified. The list of options also changes depending on which command is given.

jasonacox commented 1 month ago

Although I'm leaning towards displaying the help

Same! Yes, I think the usage help is the right option. It shows the version and commands.

The issue is how argparse handles subparsers. I don't know of any good way to add them without having the command specified. The list of options also changes depending on which command is given.

I know, it may break it from current use, but is probably the cleaner thing to do. We could have each command as a subparser. This is what we did for pypowerwall:

https://github.com/jasonacox/pypowerwall/blob/110f43870d0c6fd0661c343c850e9f1dd504e1a0/pypowerwall/__main__.py#L34-L191

Gives:

$ python3 -m pypowerwall -h                                                            main
usage: PyPowerwall [-h] [-debug] {setup,fleetapi,scan,set,get,version} ...

PyPowerwall Module v0.9.1

options:
  -h, --help            show this help message and exit
  -debug                Enable debug output

commands (run <command> -h to see usage information):
  {setup,fleetapi,scan,set,get,version}
    setup               Setup Tesla Login for Cloud Mode access
    fleetapi            Setup Tesla FleetAPI for Cloud Mode access
    scan                Scan local network for Powerwall gateway
    set                 Set Powerwall Mode and Reserve Level
    get                 Get Powerwall Settings and Power Levels
    version             Print version information
uzlonewolf commented 1 month ago

This is what we did for pypowerwall

Ah, that's what I wanted but wasn't able to figure out how to do. Thanks for the example, I'll get that implemented.

uzlonewolf commented 1 month ago
$ tinytuya
usage: tinytuya [-h] [-debug] {wizard,scan,devices,snapshot,json} ...

TinyTuya [1.14.0]

options:
  -h, --help            show this help message and exit
  -debug, -d            Enable debug messages

commands (run <command> -h to see usage information):
  {wizard,scan,devices,snapshot,json}
    wizard              Launch Setup Wizard to get Device Local Keys
    scan                Scan local network for Tuya devices
    devices             Scan all devices listed in device-file
    snapshot            Scan devices listed in snapshot-file
    json                Scan devices listed in snapshot-file and display the result as JSON
$ python3 -m tinytuya
usage: python3 -m tinytuya [-h] [-debug] {wizard,scan,devices,snapshot,json} ...

TinyTuya [1.14.0]

options:
  -h, --help            show this help message and exit
  -debug, -d            Enable debug messages

commands (run <command> -h to see usage information):
  {wizard,scan,devices,snapshot,json}
    wizard              Launch Setup Wizard to get Device Local Keys
    scan                Scan local network for Tuya devices
    devices             Scan all devices listed in device-file
    snapshot            Scan devices listed in snapshot-file
    json                Scan devices listed in snapshot-file and display the result as JSON
uzlonewolf commented 1 month ago

I banged on it pretty good and only found 1 minor bug with wizard. That's fixed and I made a couple other tweaks while I was at it. So, I'm going to call this one good to go.

felipecrs commented 1 month ago

Two notes from my side:

  1. Maybe you should refactor README now to suggest using pipx instead of pip. (developers who understands and wants it to be used as a library will know they should install it with pip/poetry whatever).
  2. This is just a very minor detail, probably it doesn't matter, but the --help function shows python -m tinytuya, and I think that's unnecessary now, it can probably just be tinytuya.
uzlonewolf commented 1 month ago
  1. Maybe you should refactor README now to suggest using pipx instead of pip. (developers who understands and wants it to be used as a library will know they should install it with pip/poetry whatever).

No, pip would be the recommended way with pipx just being an option as the tinytuya CLI is rather pointless by itself. The vast majority of users use tinytuya as a library to control devices using their own scripts.

2. but the --help function shows python -m tinytuya, and I think that's unnecessary now, it can probably just be tinytuya

That actually changes depending on how it was called, as shown in the example I posted above.

$ tinytuya
usage: tinytuya [-h] [-debug] {wizard,scan,devices,snapshot,json} ...
$ python3 -m tinytuya
usage: python3 -m tinytuya [-h] [-debug] {wizard,scan,devices,snapshot,json} ...
felipecrs commented 1 month ago

No, pip would be the recommended way with pipx just being an option as the tinytuya CLI is rather pointless by itself. The vast majority of users use tinytuya as a library to control devices using their own scripts.

Sure, it's your call. By the way I use tinytuya because of tuya-local. To find out the devices information to supply to it. I suppose several users also do the same.

That actually changes depending on how it was called, as shown in the example I posted above.

I see. I was testing with 1.13.3b1, maybe that's why.

uzlonewolf commented 1 month ago

By the way I use tinytuya because of tuya-local. To find out the devices information to supply to it. I suppose several users also do the same.

Sure, you can use it for that, but if that's all you need it for there are other much easier ways of getting the keys (such as https://github.com/blakadder/tuya-uncover ).

felipecrs commented 1 month ago

Oh. That's cool. Thank you!

jasonacox commented 1 month ago

Great work on this @uzlonewolf - I love the move to argparse and the additional arguments. Thank you! 🙇 I tweaked the docs to represent this change and added a RELEASE note.

@felipecrs Thanks for your help and feedback! While I agree with @uzlonewolf that this is primarily a library, the pipx CLI support is a very nice addition. I use the command line options quite a bit myself. 😁 I updated the README to highlight that and how to install and use. Again, thank you! 🙏

felipecrs commented 1 month ago

Awesome!

jasonacox commented 1 month ago

Released: https://pypi.org/project/tinytuya/1.14.0/

pipx install tinytuya