konsumer / tplink-lightbulb

Control TP-Link smart lightbulbs from nodejs
MIT License
191 stars 32 forks source link

Support multiple IP on/off #7

Closed Flux159 closed 7 years ago

Flux159 commented 7 years ago

Hi, great library! (Second time in a few months that TP-links servers aren't working and second time I'm using this lib).

I was looking at the CLI and I was wondering if you wanted to support multiple IPs through the CLI (or just a global 'on' and 'off' function for all scanned lights). Seems like it wouldn't be too hard to add to the CLI:

https://github.com/konsumer/tplink-lightbulb/blob/master/src/cli.js#L36

Wonder what your thoughts are on this.

Edit: This would probably require a scan of the network (and a default timeout of n-seconds) then running the on/off function across all found lights. This may break how the current CLI works for on/off?

konsumer commented 7 years ago

I like the idea of scan being the only "all" command in the CLI. You could do something like this with your own node code:

const Bulb = require('tplink-lightbulb')

// turn off all bulbs that are found
Bulb.scan()
  .on('light', light => {
    light.set(false)
  })
konsumer commented 7 years ago

I you want it to happen in 1 second:

const Bulb = require('tplink-lightbulb')

// turn off all bulbs that are found
const scan = Bulb.scan()
  .on('light', light => {
    light.set(false)
  })

setTimeout(() => {
  scan.stop()
}, 1000)
konsumer commented 7 years ago

A quick start for your own project:

npm init
npm i -S tplink-lightbulb

put above code in mything.js then run as node mything.js

Flux159 commented 7 years ago

I like the idea of scan being the only "all" command in the CLI.

That's fair and it keeps the CLI simple to understand.

I was messing around with the API and its very easy to just write a simple script to do it.

I was mainly asking because it seems like it would be a common use case. Similarly, I was thinking of using the bulb's info.alias name to get something closer to the Kasa app where you turn on/off a bulb based on its name (rather than IP).

Flux159 commented 7 years ago

Anyways, this doesn't seem like its necessary in the CLI and if anyone has this question they could also use the API or just read this issue. Thanks for the great lib!

konsumer commented 7 years ago

The problem is that the names aren't unique. In the kasa app, it actually keeps a record of the IP. I am hoping to just keep it simple and powerful, but also useful (as a library) for more advanced use-cases like yours.

if you are using *nix, you could even do the above in a bash script, with no js code:

 tplight scan -t 1 | xargs tplight off

The basic idea I have about tplight is a simple tool, that can be used with others, to compose more complex stuff, but by itself it's simple and straight-forward.

Flux159 commented 7 years ago

Had to slightly modify your bash script (I don't install globally so using the one in .bin):

./node_modules/.bin/tplight scan -t 1 | xargs -L1 ./node_modules/.bin/tplight on

Or if installing globally:

tplight scan -t 1 | xargs -L1 tplight off
tplight scan -t 1 | xargs -L1 tplight on
konsumer commented 7 years ago

Cool! A neat trick is, you might already be aware of, is that ./node_modules/.bin gets added to the top of your path if it's in an npm run script in your package.json:

{
  "scripts": {
    "alloff": "tplight scan -t 1 | xargs -L1 tplight off"
  }
}

then you can do npm run alloff

konsumer commented 7 years ago

My example above runs ok on OSX, but it's probly better to not pass it - name also. I didn't know about -L1. good trick.

Flux159 commented 7 years ago

Yeah I wrote that originally but it wasn't required to run (on OSX as well):

tplight scan -t 1 | awk '{print $1}' | xargs -L1 tplight on
tplight scan -t 1 | awk '{print $1}' | xargs -L1 tplight off