UNPKG

@palekseii/homebridge-tuya-platform

Version:

Fork version of official Tuya Homebridge plugin. Brings a bunch of bug fix and new device support.

90 lines 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.onProgrammableSwitchEvent = exports.configureProgrammableSwitchEvent = void 0; const TuyaDevice_1 = require("../../device/TuyaDevice"); const SINGLE_PRESS = 0; const DOUBLE_PRESS = 1; const LONG_PRESS = 2; function configureProgrammableSwitchEvent(accessory, service, schema) { if (!schema) { return; } let props; if (schema.type === TuyaDevice_1.TuyaDeviceSchemaType.Enum) { const { range } = schema.property; props = GetStatelessSwitchProps(range.includes('click') || range.includes('single_click') || range.includes('1'), range.includes('double_click'), range.includes('press') || range.includes('long_press')); } else { props = GetStatelessSwitchProps(true, false, false); } service.getCharacteristic(accessory.Characteristic.ProgrammableSwitchEvent) .setProps(props); } exports.configureProgrammableSwitchEvent = configureProgrammableSwitchEvent; function onProgrammableSwitchEvent(accessory, service, status) { if (!accessory.intialized) { return; } let value; const schema = accessory.getSchema(status.code); if (schema.type === TuyaDevice_1.TuyaDeviceSchemaType.Raw || schema.type === TuyaDevice_1.TuyaDeviceSchemaType.String) { // doorbell_pic or alarm_message const url = Buffer.from(status.value, 'base64').toString('binary'); if (url.length === 0) { return; } accessory.log.info('Alarm message:', url); value = SINGLE_PRESS; } else if (schema.type === TuyaDevice_1.TuyaDeviceSchemaType.Enum) { if (status.value === 'click' || status.value === 'single_click' || status.value === '1') { value = SINGLE_PRESS; } else if (status.value === 'double_click') { value = DOUBLE_PRESS; } else if (status.value === 'press' || status.value === 'long_press') { value = LONG_PRESS; } } else if (schema.type === TuyaDevice_1.TuyaDeviceSchemaType.Integer) { if (status.value > 0) { value = SINGLE_PRESS; } } if (value === undefined) { accessory.log.warn('Unknown ProgrammableSwitchEvent status:', status); return; } accessory.log.debug('ProgrammableSwitchEvent updateValue: %o %o', status.code, value); service.getCharacteristic(accessory.Characteristic.ProgrammableSwitchEvent) .updateValue(value); } exports.onProgrammableSwitchEvent = onProgrammableSwitchEvent; // Modified version of // https://github.com/benzman81/homebridge-http-webhooks/blob/master/src/homekit/accessories/HttpWebHookStatelessSwitchAccessory.js function GetStatelessSwitchProps(single_press, double_press, long_press) { const props = {}; if (single_press) { props.minValue = SINGLE_PRESS; } else if (double_press) { props.minValue = DOUBLE_PRESS; } else if (long_press) { props.minValue = LONG_PRESS; } if (single_press) { props.maxValue = SINGLE_PRESS; } if (double_press) { props.maxValue = DOUBLE_PRESS; } if (long_press) { props.maxValue = LONG_PRESS; } if (single_press && !double_press && long_press) { props.validValues = [SINGLE_PRESS, LONG_PRESS]; } return props; } //# sourceMappingURL=ProgrammableSwitchEvent.js.map