UNPKG

homebridge-switchbot-bluetooth-platform

Version:

A Homebridge platform Plugin for controlling SwitchBot bots using BLE (Bluetooth Low Energry)

88 lines 4.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BotAccessory = void 0; const metaDataClient_1 = require("../clients/metaDataClient"); const switchBotClient_1 = require("../clients/switchBotClient"); class BotAccessory { constructor(name, hap, log, accessoryParams) { this.name = name; this.hap = hap; this.log = log; this.accessoryParams = accessoryParams; this.isSwitchOn = false; this.getBatteryLevel = () => { const { address, scanDuration } = this.accessoryParams; const batteryLevel = this.metadataClient.getDeviceBatteryStatus(address, scanDuration); this.log.info(`Current battery level of switch is ${batteryLevel}`); return batteryLevel; }; this.handleGetSwitchValue = (callback) => { this.log.info(`Current power state of the switch is: ${this.isSwitchOn ? 'ON' : 'OFF'}`); callback(0 /* HAPStatus.SUCCESS */, this.isSwitchOn); }; this.SetPowerStateAfterDelay = (targetPowerState, delay) => { setTimeout(() => { var _a; this.isSwitchOn = targetPowerState; (_a = this.switchService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.hap.Characteristic.On).updateValue(targetPowerState); }, delay); }; this.setPowerState = (targetPowerState) => { var _a; this.isSwitchOn = targetPowerState; (_a = this.switchService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.hap.Characteristic.On).updateValue(this.isSwitchOn); }; this.handleSetSwitchValue = async (value, callback) => { try { const targetPowerState = Boolean(value); const { address, scanDuration, scanRetryCooldown, scanRetries, autoTurnOffInPressMode, } = this.accessoryParams; const device = await this.switchBotClient.getDevice(address, scanDuration, scanRetries, scanRetryCooldown); if (!device) { throw Error('Failed fetching switchbot device'); } const operationMode = this.metadataClient.getDeviceOperationMode(address, scanDuration); await this.switchBotClient.setDeviceState(device, targetPowerState, scanRetries, scanRetryCooldown); this.setPowerState(targetPowerState); // In case the switchbot is configured to be in 'press' mode, and autoOff setting is enabled // then it the bot button should be automatically set back to 'off' mode after triggering. const shouldTriggerAutoOff = autoTurnOffInPressMode && operationMode === 'press'; if (shouldTriggerAutoOff) { this.SetPowerStateAfterDelay(false, 1000); } callback(0 /* HAPStatus.SUCCESS */); } catch (e) { this.log.error(`${e}`); callback(-70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */); } }; this.switchBotClient = new switchBotClient_1.SwitchBotClient(log); this.metadataClient = new metaDataClient_1.MetadataClient(log); this.switchService = new this.hap.Service.Switch(name); this.switchService .getCharacteristic(hap.Characteristic.On) .on("get" /* CharacteristicEventTypes.GET */, this.handleGetSwitchValue) .on("set" /* CharacteristicEventTypes.SET */, this.handleSetSwitchValue); this.infoService = new hap.Service.AccessoryInformation() .setCharacteristic(hap.Characteristic.Manufacturer, 'SwitchBot') .setCharacteristic(hap.Characteristic.Model, 'SWITCHBOT-S1') .setCharacteristic(hap.Characteristic.SerialNumber, this.accessoryParams.address); this.batteryService = new hap.Service.Battery(`${name} Battery`); this.batteryService .getCharacteristic(hap.Characteristic.BatteryLevel) .onGet(this.getBatteryLevel); this.batteryService .getCharacteristic(hap.Characteristic.StatusLowBattery) .onGet(() => { const batteryLevel = this.getBatteryLevel(); return batteryLevel < 20 ? hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL; }); } getServices() { return [this.infoService, this.switchService, this.batteryService]; } } exports.BotAccessory = BotAccessory; //# sourceMappingURL=botAccessory.js.map