homebridge-blynk-plugin
Version:
Based on Peter J Wojciechowski but updated to use the new API
126 lines • 6.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlynkAccessory = exports.HOMEKIT_TYPES = void 0;
var HOMEKIT_TYPES;
(function (HOMEKIT_TYPES) {
HOMEKIT_TYPES["HUMIDITY_SENSOR"] = "HUMIDITY_SENSOR";
HOMEKIT_TYPES["LIGHTBULB"] = "LIGHTBULB";
HOMEKIT_TYPES["OUTLET"] = "OUTLET";
HOMEKIT_TYPES["TEMPERATURE_SENSOR"] = "TEMPERATURE_SENSOR";
HOMEKIT_TYPES["UNDEFINED"] = "UNDEFINED";
})(HOMEKIT_TYPES = exports.HOMEKIT_TYPES || (exports.HOMEKIT_TYPES = {}));
class BlynkAccessory {
constructor(hap, log, config) {
this.got = require('got');
this.hap = hap;
this.log = log;
this.myConfig = config;
this.name = this.myConfig.getName();
this.log.debug(`Switch ${this.name} has been created`);
}
attachAccessory(accessory) {
var _a, _b, _c, _d, _e, _f, _g;
this.accessory = accessory;
this.accessory.displayName = this.name;
const typeOf = this.myConfig.getTypeOf();
let serviceType = this.hap.Service.Lightbulb;
this.log.info(`${this.myConfig.getName()} is a ${typeOf}`);
switch (typeOf) {
case HOMEKIT_TYPES.OUTLET:
serviceType = this.hap.Service.Outlet;
this.accessoryService = (_a = this.accessory.getService(serviceType)) !== null && _a !== void 0 ? _a : this.accessory.addService(serviceType);
this.accessoryService
.getCharacteristic(this.hap.Characteristic.On)
.onGet(this.getOnHandler.bind(this))
.onSet(this.setOnHandler.bind(this));
break;
case HOMEKIT_TYPES.LIGHTBULB:
serviceType = this.hap.Service.Lightbulb;
this.accessoryService = (_b = this.accessory.getService(serviceType)) !== null && _b !== void 0 ? _b : this.accessory.addService(serviceType);
this.accessoryService
.getCharacteristic(this.hap.Characteristic.On)
.onGet(this.getOnHandler.bind(this))
.onSet(this.setOnHandler.bind(this));
if (this.myConfig.getWidgetType() === "SLIDER") {
this.accessoryService
.getCharacteristic(this.hap.Characteristic.Brightness)
.onGet(this.getNumericValueOnBlynkWidget.bind(this))
.onSet(this.setNumericValueOnBlynkWidget.bind(this));
}
break;
case HOMEKIT_TYPES.HUMIDITY_SENSOR:
serviceType = this.hap.Service.HumiditySensor;
this.accessoryService = (_c = this.accessory.getService(serviceType)) !== null && _c !== void 0 ? _c : this.accessory.addService(serviceType);
this.accessoryService
.getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
.onGet(this.getNumericValueOnBlynkWidget.bind(this));
break;
case HOMEKIT_TYPES.TEMPERATURE_SENSOR:
serviceType = this.hap.Service.TemperatureSensor;
this.accessoryService = (_d = this.accessory.getService(serviceType)) !== null && _d !== void 0 ? _d : this.accessory.addService(serviceType);
this.accessoryService
.getCharacteristic(this.hap.Characteristic.TemperatureDisplayUnits)
.onGet(this.getTemperatureUnit.bind(this));
this.log.info(`setting temperature sensor`);
break;
default:
serviceType = this.hap.Service.Lightbulb;
this.accessoryService = (_e = this.accessory.getService(serviceType)) !== null && _e !== void 0 ? _e : this.accessory.addService(serviceType);
this.log.debug(`fall through case for attaching events`);
this.accessoryService
.getCharacteristic(this.hap.Characteristic.On)
.onGet(this.getOnHandler.bind(this))
.onSet(this.setOnHandler.bind(this));
if (this.myConfig.getWidgetType() === "SLIDER") {
this.accessoryService
.getCharacteristic(this.hap.Characteristic.Brightness)
.onGet(this.getNumericValueOnBlynkWidget.bind(this))
.onSet(this.setNumericValueOnBlynkWidget.bind(this));
}
break;
}
(_f = this.accessoryService.getCharacteristic("get")) === null || _f === void 0 ? void 0 : _f.getValue();
this.infoService = (_g = accessory.getService(this.hap.Service.AccessoryInformation)) !== null && _g !== void 0 ? _g : this.accessory.addService(this.hap.Service.AccessoryInformation);
this.infoService
.setCharacteristic(this.hap.Characteristic.SerialNumber, this.accessory.UUID)
.setCharacteristic(this.hap.Characteristic.Manufacturer, this.myConfig.getManufacturer())
.setCharacteristic(this.hap.Characteristic.Model, this.myConfig.getModel());
this.log.debug(`${this.myConfig.getTypeOf()} ${this.name} has been attached.`);
}
getTemperatureUnit() {
return this.hap.Characteristic.TemperatureDisplayUnits.CELSIUS;
}
getNumericValueOnBlynkWidget() {
return this.myConfig.getValue();
}
setNumericValueOnBlynkWidget(value) {
this.myConfig.setValue(`["${value.toString()}"]`);
}
getOnHandler() {
return this.myConfig.getValue() > 0.1;
}
setOnHandler(value) {
this.myConfig.setValue(value.toString());
}
getStatus() {
var _a, _b;
try {
const onCharacter = (_a = this.accessoryService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.hap.Characteristic.On);
if (!onCharacter) {
throw new Error(`Service missing on ${this.name}`);
}
onCharacter.updateValue(this.myConfig.getValue() > 0.1);
if (this.myConfig.getWidgetType() === "SLIDER") {
const bright = (_b = this.accessoryService) === null || _b === void 0 ? void 0 : _b.getCharacteristic(this.hap.Characteristic.Brightness);
if (bright) {
bright.updateValue(this.myConfig.getValue());
}
}
}
catch (error) {
this.log.warn(`problem refresh state: ${error}`);
}
}
}
exports.BlynkAccessory = BlynkAccessory;
//# sourceMappingURL=accessories.js.map