UNPKG

@mpathirage/homebridge-accessory-example

Version:

An example accessory plugin for homebridge written in Typescript

94 lines 3.83 kB
"use strict"; const mqtt_1 = require("mqtt"); let hap; class GosundDimmerSwitch { constructor(log, config, api) { this.brightness = 0; this.switchOn = false; this.log = log; this.name = config.name; this.mqttBrokerUrl = config.mqttBrokerUrl; this.deviceTypeId = config.deviceTypeId === "" ? 'gosundsw201' : config.deviceTypeId; this.deviceId = config.deviceId; // Setup MQTT this.client = mqtt_1.connect(this.mqttBrokerUrl); this.client.subscribe("${this.deviceTypeId}/light/${this.deviceId}/state"); this.client.on("connect", () => { this.log.info("Connected to MQTT broker ${brokerUrl}"); }); this.client.on("error", (error) => { this.log.info("Cannot connect to MQTT broker ${brokerUrl}. Error: ${error}."); }); this.client.on("message", this.onMessage.bind(this)); this.switchService = new hap.Service.Lightbulb(this.name); this.switchService.getCharacteristic(hap.Characteristic.On) .on("get" /* GET */, this.getOnCharacteristicHandler.bind(this)) .on("set" /* SET */, this.setOnCharacteristicHandler.bind(this)); this.switchService.getCharacteristic(hap.Characteristic.Brightness) .setProps({ minValue: 0, maxValue: 100 }) .on("get" /* GET */, this.getBrightness.bind(this)) .on("set" /* SET */, this.setBrightness.bind(this)); this.informationService = new hap.Service.AccessoryInformation() .setCharacteristic(hap.Characteristic.Manufacturer, "Gosund") .setCharacteristic(hap.Characteristic.Model, "Smart Dimmer Switch"); log.info("Gosund Dimmer Switch (ESPHome) finished initializing!"); } onMessage(topic, message, packet) { if (topic === "${this.deviceTypeId}/light/${this.deviceId}/state") { const payload = message.toString(); try { const state = JSON.parse(payload); this.switchOn = state.state == "ON" ? true : false; this.brightness = state.brightness != null ? ((state.brightness / 255) * 100) | 0 : 0; } catch (e) { this.log.error("Invalid JSON format for state ${payload}."); } } } /* * This method is optional to implement. It is called when HomeKit ask to identify the accessory. * Typical this only ever happens at the pairing process. */ identify() { this.log("Gosund Dimmer Switch (ESPHome) identify!"); } /* * This method is called directly after creation of this instance. * It should return all services which should be added to the accessory. */ getServices() { return [ this.informationService, this.switchService, ]; } getBrightness(callback) { callback(null, this.brightness); } setBrightness(value, callback) { this.client.publish("${this.deviceTypeId}/light/${this.deviceId}/command/brightness_pct", JSON.stringify(value)); callback(null, value); this.brightness = Number(value); } setOnCharacteristicHandler(value, callback) { this.switchOn = value === true ? true : false; const msg = { state: value === true ? "OFF" : "ON", brightness: this.brightness | 0 }; this.client.publish("${this.deviceTypeId}/light/${this.deviceId}/command", JSON.stringify(msg)); callback(null); } getOnCharacteristicHandler(callback) { callback(null, this.switchOn); } } module.exports = (api) => { hap = api.hap; api.registerAccessory("GosundDimmerSwitch", GosundDimmerSwitch); }; //# sourceMappingURL=accessory.js.map