Skip to content

Instantly share code, notes, and snippets.

@nurikk
Created June 29, 2021 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nurikk/ba32b9f074fc1046d5b8359f7f636f95 to your computer and use it in GitHub Desktop.
Save nurikk/ba32b9f074fc1046d5b8359f7f636f95 to your computer and use it in GitHub Desktop.
Extension
const {
Socket
} = require('net');
//Kitchen Yeelight
const kitchenLight = {
host: '192.168.1.19',
port: 55443
};
const controllYeelight = function ({ host, port}, payload) {
const client = new Socket();
try {
client.connect(port, host, function () {
client.write(JSON.stringify(payload) + '\r\n');
client.destroy();
}).on('error', (err) => { console.log(err) });
} catch(e) { console.log(e); }
};
const minutes = function (m) {
return m * 1000 * 60
};
const timers = {};
const doAfter = function (delay, code, ...args) {
clearTimeout(timers[code.name]);
timers[code.name] = setTimeout((...params) => {
code(...params)
}, delay, ...args);
}
const turnOnYeelight = {
"id": 1,
"method": "set_power",
"params": ["on", "smooth", 500]
}
const turnOffYeelight = {
"id": 1,
"method": "set_power",
"params": ["off", "smooth", 500]
}
class MyExampleExtension1614405068547 {
constructor(zigbee, mqtt, state, publishEntityState, eventBus, settings, logger) {
logger.info('Loaded MyExampleExtension1614405068547');
mqtt.publish('example/extension', 'hello from 123 MyExampleExtension1614405068547');
logger.info(this.constructor.name);
this.mqttBaseTopic = settings.get().mqtt.base_topic;
this.eventBus = eventBus;
this.mqtt = mqtt;
this.eventBus.on('stateChange', this.onStateChange.bind(this), this.constructor.name);
}
async onStateChange(data) {
const {
ID,
update
} = data;
console.log({ID, update});
const myRemoteIeeeAddre = '0x00158d000224154d'; // change this
const myLampIeeAddr = '0x00124b001e73227f'; // change this
const workLightCommandTopic = `${this.mqttBaseTopic}/${myLampIeeAddr}/set`;
//example how to toggle state
if (ID === myRemoteIeeeAddre) { //state changed for myRemote (button click)
if (update.action === 'single') { //toggle work light on single click
//sending message to z2m, so it can toggle sonoff zbr state
this.mqtt.onMessage(workLightCommandTopic, JSON.stringify({
state: 'toggle'
}));
}
if (update.action === 'double') { //toggle dinning light (ESPHome) on single click
//sending data right to the mqtt broker
this.mqtt.publish('switch/sonoff_basic_relay/command', 'toggle', {}, 'dinning');
}
}
if (ID === '0x00158d0002c48958') { //work chair vibration sensor
if (update.action) { // someone sat on the chair
this.mqtt.onMessage(workLightCommandTopic, JSON.stringify({
state: 'on'
}));
//turn off ligth after 30 minutes without any activity
doAfter(minutes(30), function TurnOffWorkLight(topic) {
this.mqtt.onMessage(topic, JSON.stringify({
state: 'off'
}));
}.bind(this), workLightCommandTopic);
}
}
if (ID === '0x00158d0002c4a78f') { //baby chair vibration sensor
if (update.action) { // someone sat on the chair
//sending data right to the mqtt broker
this.mqtt.publish('switch/sonoff_basic_relay/command', 'on', {}, 'dinning');
}
}
if (ID === '0x00158d0003d20443') { // kitchen door
if (update.contact !== true) { //the door is opened
controllYeelight(kitchenLight, turnOnYeelight);
//turn off ligth after 5 minutes if no more movement
doAfter(minutes(5), function TurnOffYeelight(light) {
controllYeelight(light, turnOffYeelight);
}, kitchenLight);
}
}
if (ID === '0x00158d000450d0e6') { //kitchen motion sensor
if (update.occupancy === true) { // detected motion
controllYeelight(kitchenLight, turnOnYeelight);
doAfter(minutes(5), function TurnOffYeelight(light) {
controllYeelight(light, turnOffYeelight);
}, kitchenLight);
}
}
}
async onMQTTMessage(topic, message) {
// console.log({topic, message});
}
async stop() {
this.eventBus.removeListenersExtension(this.constructor.name);
}
}
module.exports = MyExampleExtension1614405068547;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment