tuya-smartlife-api
v0.8.4
Published
Library/CLI interface for Tuya IoT devices through the Smart Life API
Maintainers
Readme
Tuya Smart Life API + CLI
Usage 
- install package locally
npm install -S tuya-smartlife-api- import module (ESM or CJS)
/* ESM */
import { TuyaSmartLifeClient } from "tuya-smartlife-api";
/* CommonJS */
// const TuyaSmartLifeClient = require("tuya-smartlife-api").TuyaSmartLifeClient;
const client = new TuyaSmartLifeClient();
try {
await client.init('[email protected]', 'password', 'eu');
await client.discoverDevices();
const tDevices = client.getAllDevices();
console.log(tDevices);
const myLightBulbs = await client.getDevicesByType('light');
await myLightBulbs[0].turnOn();
} catch (e) {
console.error('Failed because', e);
}Note: Consider using an
.envfile with dotenv.
Note: check your Tuya region.
- install package globally
npm install -g tuya-smartlife-api- verify installation
tuyacliUsage: tuyacli [options] [command]
Options:
-V, --version output the version number
-h, --help display help for command
Commands:
auth login with SmartLife
test live test a selected device's functions set
list [options] list devices and their state / attributes
control [options] <name-or-id> control a device's state
help output usage informationExamples
# Authenticate /will be automatically called later if skipped the first time/
tuyacli auth
# List all devices
tuyacli list [--format={short|long}]
# Perform interactiv device tests
tuyacli test
# Turn device ID on / off
tuyacli control <ID|Name> --state [1|on]
tuyacli control <ID|Name> --state [0|off]
tuyacli control <ID|Name> --toggle
# Set light brightness, color temp & color
tuyacli control <ID|Name> --brightness 30
tuyacli control <ID|Name> --temperature 3500 # set warm temp
tuyacli control <ID|Name> --hsl 78.34,1,100 # HSL chill green
tuyacli control <ID|Name> --hsl 324.77,1,42 # HSL chill purple
tuyacli control <ID|Name> --rgb 90,30,115 # RGB somethingNote: Use
node tuyacliin module's root folder if not installed globally.
Note: The
<ID|Name>portion is treated as a filtering pattern rather than a full identifier. Conviniently,tuyacli control bulb -s offwould turn off all devices with the pattern "bulb" in their names, but use with caution.IDwould be used for large setups, whereNameis not applicable.
Tests
npm testtuyacli testNote: Use
node tuyacliin module's root folder if not installed globally.
Note: Tests might fail if attempted multiple times before
LOGIN_INTERVALseconds have passed since last run.
Roadmap
- [x] port and optimize TuyaPy
- [x] implement integration tests
- [x] implement pure CLI
- [x] list devices (short / long format)
- [x] control a device's state
- [x] control a device's custom attributes
- [x] use Configstore for credentials & device cache (not
session.json)- [ ] encrypt credentials at rest
- [x] implement unit tests
- [x] add support for CommonJS
- [ ] document code & generate JSDoc
- [ ] dockerize
- [ ] implement classes for other IoT devices (climate, fan, lock, etc.)
- [ ] ~~implement TUI with blessed-contrib~~
Credits 
Inspired by TuyaPy (backend) and SmartLife (web) interfaces to Tuya's SmartAtHome for IoT smart device control.
