Module
- 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('jondoe@example.co.uk', '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.
Standalone
- 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 information
# 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.
Unit tests
npm testInteractive tests
tuyacli 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.
- port and optimize TuyaPy
- implement integration tests
- implement pure CLI
- list devices (short / long format)
- control a device's state
- control a device's custom attributes
- use Configstore for credentials & device cache (not
session.json)- encrypt credentials at rest
- implement unit tests
- add support for CommonJS
- document code & generate JSDoc
- dockerize
- implement classes for other IoT devices (climate, fan, lock, etc.)
-
implement TUI with blessed-contrib
Inspired by TuyaPy (backend) and SmartLife (web) interfaces to Tuya's SmartAtHome for IoT smart device control.