Last active
March 7, 2021 02:32
-
-
Save nurikk/0e46e4ae2645c2682b39baef90c21403 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
client.connect(port, host, function () { | |
client.write(JSON.stringify(payload) + '\r\n'); | |
client.destroy(); | |
}); | |
}; | |
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, | |
from, | |
to, | |
reason | |
} = data; | |
// console.log({ID, from, to, reason}); | |
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 (from.action !== to.action) { | |
if (to.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 (to.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 (from.action !== to.action && to.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 === '0x00158d0003d20443') { // kitchen door | |
if (from.contact !== to.contact && to.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 (to.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