Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save legndery/2ede07d00edd568b59958ed46da62f52 to your computer and use it in GitHub Desktop.
Save legndery/2ede07d00edd568b59958ed46da62f52 to your computer and use it in GitHub Desktop.
Workaround for Tuya: "No permissions. Your subscription to cloud development plan has expired."
const schedule = require('node-schedule');
const { TuyaContext } = require('@tuya/tuya-connector-nodejs');
const { exec } = require('child_process');
const answers = {
apiKey: 'xxxx', // from tuya
apiSecret: 'xxxx'
}
const deviceId = 'xx'; // from tuya
const commands = (switchFlag) => { //you can get this from `Debug Device` window
return {
"commands": [{ "code": "switch_1", "value": switchFlag }, { "code": "countdown_1", "value": 0 }, { "code": "child_lock", "value": false }]
}
};
const plugStarter = async (turnOn = false) => {
const api = new TuyaContext({
baseUrl: `https://openapi.tuyaXX.com`, // xx can be in, eu, us, cn etc
accessKey: answers.apiKey,
secretKey: answers.apiSecret
});
const result = await api.request({
method: 'POST',
path: `/v1.0/devices/${deviceId}/commands`,
body: commands(turnOn)
});
}
// this to automatically turn on charging when battery <20, otherwise turn off when >80
const getBatteryPercentage = () => {
const batterPercentageCommand = 'pmset -g batt | egrep "([0-9]+\%).*" -o';
return new Promise((res, rej) => {
exec(batterPercentageCommand, (err, stdout, stderr) => {
// console.log(err);
console.log(stdout);
// console.log(stderr);
if (err) {
console.error(`child_process failed with error code: ${err.code}`);
console.error(`error: ${stderr}`);
rej(null);
} else {
try {
const p = stdout.split('%;')[0];
res(p);
} catch (e) {
console.error(`error while converting to percentage: ${e}`);
console.error(`Payload: ${stdout}`);
}
}
});
});
}
const job = schedule.scheduleJob('plugStarter', '*/15 * * * *', () => {
console.log("running job!")
getBatteryPercentage().then(v => {
if (v > 90) {
plugStarter(false).then(() => console.log("stopped charging"));
} else if (v < 20) {
plugStarter(true).then(() => console.log('Stopped charging'));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment