Last active
May 9, 2018 17:56
-
-
Save konsumer/90962de1727fc322289e14a039a93fab to your computer and use it in GitHub Desktop.
Example using https://github.com/konsumer/kasa_control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const KasaControl = require('kasa_control') | |
const { KASA_USER, KASA_PASS } = process.env | |
if (!KASA_USER || !KASA_PASS) { | |
throw new Error('Put your credentials in the environment variables KASA_USER & KASA_PASS.') | |
} | |
const main = async () => { | |
// login and get basic info about connection | |
const kasa = new KasaControl() | |
const login = await kasa.login(KASA_USER, KASA_PASS) | |
// console.log(login) | |
// get list of devices in kasa app | |
const devices = await kasa.getDevices() | |
// console.log(devices) | |
// an example of getting 1 device, by name, out of the list | |
const livingroom = devices.find(l => l.alias === 'Livingroom') | |
// console.log(livingroom) | |
const info = await kasa.info(livingroom.deviceId) | |
// console.log(info) | |
// check the status of a device | |
console.log(`your Livingroom is ${info.light_state.on_off ? 'on' : 'off'}. Turning it ${info.light_state.on_off ? 'off' : 'on'}`) | |
// toggle power | |
await kasa.power(livingroom.deviceId, !info.light_state.on_off) | |
// put it back | |
console.log('Putting it back.') | |
await kasa.power(livingroom.deviceId, info.light_state.on_off) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment