From eb9e2acc305924f1de989e7e60d6114b898e0d08 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 4 Jan 2018 20:17:58 +0100 Subject: [PATCH] homematic switch --- src/HomematicSwitchItem.js | 39 ++++++++++++++++++++++++++++++++++++++ src/main.js | 8 ++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/HomematicSwitchItem.js diff --git a/src/HomematicSwitchItem.js b/src/HomematicSwitchItem.js new file mode 100644 index 0000000..267bf9c --- /dev/null +++ b/src/HomematicSwitchItem.js @@ -0,0 +1,39 @@ +let logger = require('./log'); +let mqtt = require('./mqttHandler'); +let AHomematicItem = require('./AHomematicItem') + + +class HomematicSwitchItem extends AHomematicItem { + constructor(floor, room, item, hmId) { + super(floor, room, item, hmId); + this.stateTopic = `${this.topicFirstPart}/state`; + this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`; + this.actionTopic = `${this.actionTopicPre}/1/STATE`; + this.subscribeTopics = [ + this.stateTopic + ]; + this.state = 'OFF'; + this.oldState = undefined; + } + + + processMessage(topic, payload) { + switch (topic) { + case this.stateTopic: + this.state = payload; + mqtt.send(this.stateFeedbackTopic, this.state); + if (this.state != this.oldState) { + if (this.state == 'ON') { + mqtt.send(this.actionTopic, 'true'); + } else { + mqtt.send(this.actionTopic, 'false'); + } + this.oldState = this.state; + } + break; + } + } +} + +module.exports = HomematicSwitchItem; + diff --git a/src/main.js b/src/main.js index 8842f7c..c22b98a 100644 --- a/src/main.js +++ b/src/main.js @@ -6,6 +6,7 @@ logger.info("Hello world!"); let HomematicDimmerItemClass = require('./HomematicDimmerItem'); +let HomematicSwitchItemClass = require('./HomematicSwitchItem'); let M433SwitchItem = require('./M433SwitchItem'); let HomematicFourButtonThing = require('./HomematicFourButtonThing'); let HomematicFourButtonSingleItem = require('./HomematicFourButtonSingleItem'); @@ -24,7 +25,7 @@ let testFourButton = new HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton' new HomematicFourButtonSingleItem('button2', 'test/button/2'), new HomematicFourButtonSingleItem('button3', 'test/button/3'), new HomematicFourButtonSingleItem('button4', 'test/button/4') -]) +]); testFourButton.start(); let testDimmerAdaptor = new DimmerAdaptor('Gnd', 'Hallway', 'Testlight'); @@ -33,8 +34,11 @@ testDimmerAdaptor.start(); let testLight = new HomematicDimmerItemClass('Gnd', 'Hallway', 'Testlight', 8); testLight.start(); +let testLight2 = new HomematicSwitchItemClass('Gnd', 'Hallway', 'Testlight2', 5); +testLight2.start(); + let testForwarder = new Forwarder('Gnd', 'Hallway', 'TestForwarder', 'state', [ - 'dispatcher_ng/items/Gnd/Hallway/Testlight/state', + 'dispatcher_ng/items/Gnd/Hallway/Testlight2/state', 'dispatcher_ng/items/Gnd/Hallway/DeskLight/state' ]); testForwarder.start();