import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge' import { accessory } from './accessory' import { device } from './device' import { httpRequest, response } from './httpPromise' import { domoticaPlatform } from './platform' export class doorWindowSensorAccessory extends accessory { private service: Service private state = { ContactSensorState: this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED, LowBattery: false, Active: false } static async discoverDevices(platform: domoticaPlatform): Promise { return new Promise((resolve, reject) => { let doorWindowSensorDevices: device[] = [] httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/doorwindowsensor') .then((response) => { let devices = JSON.parse(response.body) for (const device of devices) { if (device.enabled == "true") { let object: device = { type: "DoorWindowSensor", typeId: device.id, uniqueId: "DoorWindowSensor." + device.id, deviceName: device.name, switchable: "false", dimmable: "false", detailedType: device.type } doorWindowSensorDevices.push(object) } } resolve(doorWindowSensorDevices) }) .catch((error) => { reject('doorWindowSensorAccessory::discoverDevices Error ->' + error) }) }) } constructor( private readonly platform: domoticaPlatform, private readonly accessory: PlatformAccessory, ) { super("DoorWindowSensor", accessory.context.device.typeId) this.accessory.getService(this.platform.Service.AccessoryInformation)! .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Xiaomi') .setCharacteristic(this.platform.Characteristic.Model, 'MCCGQ01LM') .setCharacteristic(this.platform.Characteristic.SerialNumber, '04000' + accessory.context.device.typeId) if (accessory.context.device.detailedType == "door") { this.service = this.accessory.getService(this.platform.Service.ContactSensor) || this.accessory.addService(this.platform.Service.ContactSensor) } else { // window this.service = this.accessory.getService(this.platform.Service.ContactSensor) || this.accessory.addService(this.platform.Service.ContactSensor) } this.service.getCharacteristic(this.platform.Characteristic.ContactSensorState) .onGet(this.getContactSensorState.bind(this)); this.service.getCharacteristic(this.platform.Characteristic.StatusLowBattery) .onGet(this.getLowBattery.bind(this)) this.service.getCharacteristic(this.platform.Characteristic.StatusActive) .onGet(this.getActive.bind(this)) this.setName(accessory.context.device.deviceName) this.update() } setName(name) { this.service.setCharacteristic(this.platform.Characteristic.Name, name) } setValue(key, value) { if (key == "state" && value != "") { if (value == "Off") { this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED } else { this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED } this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.state.ContactSensorState) } else if (key == "battery" && value != "") { if (value < 25) { this.state.LowBattery = true } else { this.state.LowBattery = false } this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) } else if (key == "online" && value != "") { if (value == "false") { this.state.Active = false } else { this.state.Active = true } this.service.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) } } update() { const data = JSON.stringify([{ type: 'doorwindow', query: 'state' }]) httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data) .then((response) => { const devices = JSON.parse(response.body) if (devices.hasOwnProperty('doorwindow')) { for (const device of devices.doorwindow) { if (device.id == this.accessory.context.device.typeId) { if (device.state == 'closed') { this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED } else { // open this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED } if (device.battery < 25) { this.state.LowBattery = true } else { this.state.LowBattery = false } if (device.online == "false") { this.state.Active = false } else { this.state.Active = true } this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.state.ContactSensorState) this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) this.service.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) break } } } }) .catch((error) => { this.platform.log.debug('doorWindowSensorAccessory::update Error ->' + error) }) } getContactSensorState(): CharacteristicValue { return this.state.ContactSensorState } getLowBattery(): CharacteristicValue { return this.state.LowBattery } getActive(): CharacteristicValue { return this.state.Active } }