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 motionSensorAccessory extends accessory { private service: Service private state = { Motion: false, LowBattery: false, Active: false } static async discoverDevices(platform: domoticaPlatform): Promise { return new Promise((resolve, reject) => { let motionSensorDevices: device[] = [] httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/motionsensor') .then((response) => { let devices = JSON.parse(response.body) for (const device of devices) { if (device.enabled == "true") { let object: device = { type: "MotionSensor", typeId: device.id, uniqueId: "MotionSensor." + device.id, deviceName: device.name, switchable: "false", dimmable: "false", detailedType: device.type } motionSensorDevices.push(object) } } resolve(motionSensorDevices) }) .catch((error) => { reject('motionSensorAccessory::discoverDevices Error ->' + error) }) }) } constructor( private readonly platform: domoticaPlatform, private readonly accessory: PlatformAccessory, ) { super("MotionSensor", accessory.context.device.typeId) this.accessory.getService(this.platform.Service.AccessoryInformation)! .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Xiaomi') .setCharacteristic(this.platform.Characteristic.Model, 'RTCGQ01LM') .setCharacteristic(this.platform.Characteristic.SerialNumber, '05000' + accessory.context.device.typeId) this.service = this.accessory.getService(this.platform.Service.MotionSensor) || this.accessory.addService(this.platform.Service.MotionSensor) this.service.getCharacteristic(this.platform.Characteristic.MotionDetected) .onGet(this.getState.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 == "On") { this.state.Motion = true } else { this.state.Motion = false } this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.state.Motion) } 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: 'motion', 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('motion')) { for (const device of devices.motion) { if (device.id == this.accessory.context.device.typeId) { if (device.state == 'absent') { this.state.Motion = false } else { // present this.state.Motion = true } 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.MotionDetected, this.state.Motion) 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('motionSensorAccessory::update Error ->' + error) }) } getState(): CharacteristicValue { return this.state.Motion } getLowBattery(): CharacteristicValue { return this.state.LowBattery } getActive(): CharacteristicValue { return this.state.Active } }