Giter Site home page Giter Site logo

kasa_control's Introduction

kasa-control

This will let you use kasa-cloud to hit your TPLink bulbs in nodejs.

If you'd like to hit them directly, on your local network, use tplink-lightbulb. This library is for situations where you want to use a username/password (from tplink cloud.) It will allow you to acces them remotely.

installation

npm i kasa_control

example usage

const KasaControl = require('kasa_control')
const kasa = new KasaControl()

const main = async () => {
  await kasa.login('email', 'password')
  const devices = await kasa.getDevices()

  // turn off first device
  await kasa.power(devices[0].deviceId, false)
}
main()

api

sendPromise

Send a message to a lightbulb (for RAW JS message objects)

getDevicesPromise

Get a list of devices for your Kasa account

infoPromise

Get info about a device

powerPromise

Set power-state of lightbulb

send ⇒ Promise

Send a message to a lightbulb (for RAW JS message objects)

Returns: Promise - Resolves with answer

Param Type Description
deviceId string The deviceId of the device in your kasa app
msg Object Message to send to bulb

Example

kasa.send('80126E22B048C76F341BEED1A3EA8E77177F3484', {
  'smartlife.iot.smartbulb.lightingservice': {
    'transition_light_state': {
      'on_off': 1,
      'transition_period': 0
    }
})
  .then(response => {
    console.log(response)
  })
  .catch(e => console.error(e))

getDevices ⇒ Promise

Get a list of devices for your Kasa account

Returns: Promise - Resolves to an array of device-objects Example

kasa.getDevices()
  .then(devices => {
    console.log(devices)
  })
  .catch(e => console.error(e))

info ⇒ Promise

Get info about a device

Returns: Promise - Resolves to an info-pbject about your device example

kasa.info('80126E22B048C76F341BEED1A3EA8E77177F3484')
  .then(info => {
    console.log(info)
  })
  .catch(e => console.error(e))
Param Type Description
deviceId string The deviceId of the device in your kasa app

power ⇒ Promise

Set power-state of lightbulb

Returns: Promise - Resolves to output of command

Param Type Description
deviceId string The deviceId of the device in your kasa app
powerState Boolean On or off
transition Number Transition to new state in this time
options Object Object containing mode, hue, saturation, color_temp, brightness

Example

// turn a light on
kasa.power('80126E22B048C76F341BEED1A3EA8E77177F3484', true)
  .then(status => {
    console.log(status)
  })
  .catch(err => console.error(err))

thanks

Thanks to itnerd for these 3 articles:

kasa_control's People

Contributors

dependabot[bot] avatar konsumer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

kasa_control's Issues

error with a dependency uuid4

I installed Kasa_control normally but when a try to npm run dev there is an error:
This dependency was not found:

  • uuid4 in ./node_modules/kasa_control/build/lib.js
    To install it, you can run: npm install --save uuid4

I installed the dependency but still have the same error.
Any idea?
Thanks

issues in newer versions of Node.js

it seems kasa_control v0.1.1 doesn't wanna work with node V16

I keep getting this error

node:internal/modules/cjs/loader:1109
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/ryanfarber/code/botmaster/node_modules/kasa_control/node_modules/node-fetch/src/index.js
require() of ES modules is not supported.
require() of /Users/ryanfarber/code/botmaster/node_modules/kasa_control/node_modules/node-fetch/src/index.js from /Users/ryanfarber/code/botmaster/node_modules/kasa_control/build/lib.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/ryanfarber/code/botmaster/node_modules/kasa_control/node_modules/node-fetch/package.json.

    at new NodeError (node:internal/errors:329:5)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1109:13)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:996:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (/Users/ryanfarber/code/botmaster/node_modules/kasa_control/build/lib.js:15:13)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:996:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (/Users/ryanfarber/code/botmaster/plugins/kasa/src.js:11:21)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:996:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (/Users/ryanfarber/code/botmaster/plugins/kasa/index.js:3:18)
    at Module._compile (node:internal/modules/cjs/loader:1092:14) {
  code: 'ERR_REQUIRE_ESM'
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.