homebridge-mqtt-pressure
Version:
Plugin to HomeBridge optimized for work with Itead Sonoff and Electrodragon Relay Board hardware with firmware Sonoff-Tasmota via MQTT. Also works with other accessories sending the pressure as a number (payload ex. 1000).
164 lines (133 loc) • 4.91 kB
JavaScript
var Service, Characteristic;
var mqtt = require('mqtt');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
CommunityTypes = require('hap-nodejs-community-types')(homebridge);
homebridge.registerAccessory("homebridge-mqtt-pressure", "mqtt-pressure", PressureAccessory);
}
function PressureAccessory(log, config) {
this.log = log;
this.name = config["name"] || "Sonoff";
this.manufacturer = config['manufacturer'] || "ITEAD";
this.model = config['model'] || "Sonoff";
this.serialNumberMAC = config['serialNumberMAC'] || "";
this.url = config['url'];
this.topic = config['topic'];
this.sensorPropertyName = config["sensorPropertyName"] || "PressureSensor";
if (config["activityTopic"] !== undefined) {
this.activityTopic = config["activityTopic"];
this.activityParameter = config["activityParameter"];
}
else {
this.activityTopic = "";
this.activityParameter = "";
}
this.client_Id = 'mqttjs_' + Math.random().toString(16).substr(2, 8);7
this.options = {
keepalive: 10,
clientId: this.client_Id,
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
reconnectPeriod: 1000,
connectTimeout: 30 * 1000,
will: {
topic: 'WillMsg',
payload: 'Connection Closed abnormally..!',
qos: 0,
retain: false
},
username: config["username"],
password: config["password"],
rejectUnauthorized: false
};
this.service = new Service.TemperatureSensor(this.name);
this.service.addCharacteristic(CommunityTypes.AtmosphericPressureLevel);
if(this.activityTopic !== "") {
this.service.addOptionalCharacteristic(Characteristic.StatusActive);
}
this.client = mqtt.connect(this.url, this.options);
this.client.on('error', function () {
that.log('Error event on MQTT')
});
this.client.on('connect', function () {
if (config["startCmd"] !== undefined) {
that.client.publish(config["startCmd"], config["startParameter"] !== undefined ? config["startParameter"] : "");
}
});
var that = this;
this.client.subscribe(this.topic);
if(this.activityTopic !== ""){
this.client.subscribe(this.activityTopic);
}
this.client.on('message', function (topic, message) {
if (topic == that.topic) {
that.temperature = -49
that.pressure = 300
data = null
try {
data = JSON.parse(message);
} catch (e) {
that.log('JSON input Error',e)
}
if (data === null) {
that.pressure = parseFloat(message);
that.temperature = 0
} else if (data.hasOwnProperty(that.sensorPropertyName)) {
that.pressure = parseFloat(data[that.sensorPropertyName].Pressure);
that.temperature = parseFloat(data[that.sensorPropertyName].Temperature);
} else if (data.hasOwnProperty("BMP280")) {
that.pressure = parseFloat(data.BMP280.Pressure);
that.temperature = parseFloat(data.BMP280.Temperature);
} else { return null }
that.service.setCharacteristic(Characteristic.CurrentTemperature, that.temperature);
that.service.setCharacteristic(CommunityTypes.AtmosphericPressureLevel, that.pressure);
} else if (topic == that.activityTopic) {
var status = message.toString();
that.activeStat = (status == that.activityParameter) ;
that.service.setCharacteristic(Characteristic.StatusActive, that.activeStat);
}
});
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getTemperature.bind(this));
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({minValue: -50});
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({maxValue: 100});
this.service
.getCharacteristic(CommunityTypes.AtmosphericPressureLevel)
.on('get', this.getPressure.bind(this));
this.service
.getCharacteristic(CommunityTypes.AtmosphericPressureLevel)
.setProps({minValue: 300});
this.service
.getCharacteristic(CommunityTypes.AtmosphericPressureLevel)
.setProps({maxValue: 1100});
if(this.activityTopic !== "") {
this.service
.getCharacteristic(Characteristic.StatusActive)
.on('get', this.getStatusActive.bind(this));
}
}
PressureAccessory.prototype.getTemperature = function(callback) {
callback(null, this.temperature);
}
PressureAccessory.prototype.getPressure = function(callback) {
callback(null, this.pressure);
}
PressureAccessory.prototype.getStatusActive = function(callback) {
callback(null, this.activeStat);
}
PressureAccessory.prototype.getServices = function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serialNumberMAC);
return [informationService, this.service];
}