diff --git a/warema-bridge/srv/index.js b/warema-bridge/srv/index.js index b59eefe..aabf8d5 100644 --- a/warema-bridge/srv/index.js +++ b/warema-bridge/srv/index.js @@ -1,5 +1,4 @@ const warema = require('./warema-wms-venetian-blinds'); -const {registerDevice, devices} = require('./register-device'); const log = require('./logger'); const mqtt = require('mqtt'); @@ -20,6 +19,169 @@ const settingsPar = { wmsSerialPort: process.env.WMS_SERIAL_PORT || '/dev/ttyUSB0', }; +function registerDevice(element) { + log.info('Registering ' + element.snr) + var topic = 'homeassistant/cover/' + element.snr + '/' + element.snr + '/config' + var availability_topic = 'warema/' + element.snr + '/availability' + + var base_payload = { + name: element.snr, + availability: [ + {topic: 'warema/bridge/state'}, + {topic: availability_topic} + ], + unique_id: element.snr + } + + var base_device = { + identifiers: element.snr, + manufacturer: "Warema", + name: element.snr + } + + var model + var payload + switch (parseInt(element.type)) { + case 6: + model = 'Weather station eco' + payload = { + ...base_payload, + device: { + ...base_device, + model: model + } + } + + var illuminance_payload = { + ...payload, + state_topic: 'warema/' + element.snr + '/illuminance/state', + device_class: 'illuminance', + unique_id: element.snr + '_illuminance', + unit_of_measurement: 'lm', + } + client.publish('homeassistant/sensor/' + element.snr + '/illuminance/config', JSON.stringify(illuminance_payload), {retain: true}) + + //No temp on weather station eco + var temperature_payload = { + ...payload, + state_topic: 'warema/' + element.snr + '/temperature/state', + device_class: 'temperature', + unique_id: element.snr + '_temperature', + unit_of_measurement: 'C', + } + client.publish('homeassistant/sensor/' + element.snr + '/temperature/config', JSON.stringify(temperature_payload), {retain: true}) + + var wind_payload = { + ...payload, + state_topic: 'warema/' + element.snr + '/wind/state', + device_class: 'wind_speed', + unique_id: element.snr + '_wind', + unit_of_measurement: 'm/s', + } + client.publish('homeassistant/sensor/' + element.snr + '/wind/config', JSON.stringify(wind_payload), {retain: true}) + + //No rain on weather station eco + var rain_payload = { + ...payload, + state_topic: 'warema/' + element.snr + '/rain/state', + device_class: 'moisture', + unique_id: element.snr + '_rain' + } + client.publish('homeassistant/binary_sensor/' + element.snr + '/rain/config', JSON.stringify(rain_payload), {retain: true}) + + client.publish(availability_topic, 'online', {retain: true}) + + devices[element.snr] = {}; + // No need to add to stick, updates are broadcasted + + return; + case 7: + // WMS Remote pro + return; + case 9: + // WMS WebControl Pro - while part of the network, we have no business to do with it. + return; + case 20: + model = 'Plug receiver' + payload = { + ...base_payload, + device: { + ...base_device, + model: model + }, + position_open: 0, + position_closed: 100, + command_topic: 'warema/' + element.snr + '/set', + state_topic: 'warema/' + element.snr + '/state', + position_topic: 'warema/' + element.snr + '/position', + tilt_status_topic: 'warema/' + element.snr + '/tilt', + set_position_topic: 'warema/' + element.snr + '/set_position', + tilt_command_topic: 'warema/' + element.snr + '/set_tilt', + tilt_closed_value: -100, + tilt_opened_value: 100, + tilt_min: -100, + tilt_max: 100, + } + break; + case 21: + model = 'Actuator UP' + payload = { + ...base_payload, + device: { + ...base_device, + model: model + }, + position_open: 0, + position_closed: 100, + command_topic: 'warema/' + element.snr + '/set', + position_topic: 'warema/' + element.snr + '/position', + tilt_status_topic: 'warema/' + element.snr + '/tilt', + set_position_topic: 'warema/' + element.snr + '/set_position', + tilt_command_topic: 'warema/' + element.snr + '/set_tilt', + tilt_closed_value: -100, + tilt_opened_value: 100, + tilt_min: -100, + tilt_max: 100, + } + + break; + case 25: + model = 'Vertical awning' + payload = { + ...base_payload, + device: { + ...base_device, + model: model + }, + position_open: 0, + position_closed: 100, + state_topic: 'warema/' + element.snr + '/state', + command_topic: 'warema/' + element.snr + '/set', + position_topic: 'warema/' + element.snr + '/position', + set_position_topic: 'warema/' + element.snr + '/set_position', + } + + break; + default: + log.info('Unrecognized device type: ' + element.type) + model = 'Unknown model ' + element.type + return + } + + if (ignoredDevices.includes(element.snr.toString())) { + log.info('Ignoring and removing device ' + element.snr + ' (type ' + element.type + ')') + } else { + log.info('Adding device ' + element.snr + ' (type ' + element.type + ')') + + stickUsb.vnBlindAdd(parseInt(element.snr), element.snr.toString()); + + devices[element.snr] = {}; + + client.publish(availability_topic, 'online', {retain: true}) + client.publish(topic, JSON.stringify(payload), {retain: true}) + } +} + function callback(err, msg) { if (err) { log.error(err); diff --git a/warema-bridge/srv/register-device.js b/warema-bridge/srv/register-device.js deleted file mode 100644 index 2f96d6e..0000000 --- a/warema-bridge/srv/register-device.js +++ /dev/null @@ -1,168 +0,0 @@ -const log = require("./logger"); - -var devices = []; - -function registerDevice(element) { - log.info('Registering ' + element.snr) - var topic = 'homeassistant/cover/' + element.snr + '/' + element.snr + '/config' - var availability_topic = 'warema/' + element.snr + '/availability' - - var base_payload = { - name: element.snr, - availability: [ - {topic: 'warema/bridge/state'}, - {topic: availability_topic} - ], - unique_id: element.snr - } - - var base_device = { - identifiers: element.snr, - manufacturer: "Warema", - name: element.snr - } - - var model - var payload - switch (parseInt(element.type)) { - case 6: - model = 'Weather station eco' - payload = { - ...base_payload, - device: { - ...base_device, - model: model - } - } - - var illuminance_payload = { - ...payload, - state_topic: 'warema/' + element.snr + '/illuminance/state', - device_class: 'illuminance', - unique_id: element.snr + '_illuminance', - unit_of_measurement: 'lm', - } - client.publish('homeassistant/sensor/' + element.snr + '/illuminance/config', JSON.stringify(illuminance_payload), {retain: true}) - - //No temp on weather station eco - var temperature_payload = { - ...payload, - state_topic: 'warema/' + element.snr + '/temperature/state', - device_class: 'temperature', - unique_id: element.snr + '_temperature', - unit_of_measurement: 'C', - } - client.publish('homeassistant/sensor/' + element.snr + '/temperature/config', JSON.stringify(temperature_payload), {retain: true}) - - var wind_payload = { - ...payload, - state_topic: 'warema/' + element.snr + '/wind/state', - device_class: 'wind_speed', - unique_id: element.snr + '_wind', - unit_of_measurement: 'm/s', - } - client.publish('homeassistant/sensor/' + element.snr + '/wind/config', JSON.stringify(wind_payload), {retain: true}) - - //No rain on weather station eco - var rain_payload = { - ...payload, - state_topic: 'warema/' + element.snr + '/rain/state', - device_class: 'moisture', - unique_id: element.snr + '_rain' - } - client.publish('homeassistant/binary_sensor/' + element.snr + '/rain/config', JSON.stringify(rain_payload), {retain: true}) - - client.publish(availability_topic, 'online', {retain: true}) - - devices[element.snr] = {}; - // No need to add to stick, updates are broadcasted - - return; - case 7: - // WMS Remote pro - return; - case 9: - // WMS WebControl Pro - while part of the network, we have no business to do with it. - return; - case 20: - model = 'Plug receiver' - payload = { - ...base_payload, - device: { - ...base_device, - model: model - }, - position_open: 0, - position_closed: 100, - command_topic: 'warema/' + element.snr + '/set', - state_topic: 'warema/' + element.snr + '/state', - position_topic: 'warema/' + element.snr + '/position', - tilt_status_topic: 'warema/' + element.snr + '/tilt', - set_position_topic: 'warema/' + element.snr + '/set_position', - tilt_command_topic: 'warema/' + element.snr + '/set_tilt', - tilt_closed_value: -100, - tilt_opened_value: 100, - tilt_min: -100, - tilt_max: 100, - } - break; - case 21: - model = 'Actuator UP' - payload = { - ...base_payload, - device: { - ...base_device, - model: model - }, - position_open: 0, - position_closed: 100, - command_topic: 'warema/' + element.snr + '/set', - position_topic: 'warema/' + element.snr + '/position', - tilt_status_topic: 'warema/' + element.snr + '/tilt', - set_position_topic: 'warema/' + element.snr + '/set_position', - tilt_command_topic: 'warema/' + element.snr + '/set_tilt', - tilt_closed_value: -100, - tilt_opened_value: 100, - tilt_min: -100, - tilt_max: 100, - } - - break; - case 25: - model = 'Vertical awning' - payload = { - ...base_payload, - device: { - ...base_device, - model: model - }, - position_open: 0, - position_closed: 100, - state_topic: 'warema/' + element.snr + '/state', - command_topic: 'warema/' + element.snr + '/set', - position_topic: 'warema/' + element.snr + '/position', - set_position_topic: 'warema/' + element.snr + '/set_position', - } - - break; - default: - log.info('Unrecognized device type: ' + element.type) - model = 'Unknown model ' + element.type - return - } - - if (ignoredDevices.includes(element.snr.toString())) { - log.info('Ignoring and removing device ' + element.snr + ' (type ' + element.type + ')') - } else { - log.info('Adding device ' + element.snr + ' (type ' + element.type + ')') - - stickUsb.vnBlindAdd(parseInt(element.snr), element.snr.toString()); - - devices[element.snr] = {}; - - client.publish(availability_topic, 'online', {retain: true}) - client.publish(topic, JSON.stringify(payload), {retain: true}) - } -} - -module.exports = {registerDevice, devices}; \ No newline at end of file