Skip to content

Instantly share code, notes, and snippets.

@konsumer
Last active May 9, 2018
Embed
What would you like to do?
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