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 environmentSensorAccessory extends accessory { private serviceTemperature: Service private serviceHumidity: Service private state = { Temperature: 0, Humidity: 0, LowBattery: false, Active: false } static async discoverDevices(platform: domoticaPlatform): Promise { return new Promise((resolve, reject) => { let environmentSensorDevices: device[] = [] httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/environmentsensor') .then((response) => { let devices = JSON.parse(response.body) for (const device of devices) { if (device.enabled == "true") { let object: device = { type: "EnvironmentSensor", typeId: device.id, uniqueId: "EnvironmentSensor." + device.id, deviceName: device.name, switchable: "false", dimmable: "false", detailedType: "" } environmentSensorDevices.push(object) } } resolve(environmentSensorDevices) }) .catch((error) => { reject('environmentSensorAccessory::discoverDevices Error ->' + error) }) }) } constructor( private readonly platform: domoticaPlatform, private readonly accessory: PlatformAccessory, ) { super("EnvironmentSensor", accessory.context.device.typeId) this.accessory.getService(this.platform.Service.AccessoryInformation)! .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Xiaomi') .setCharacteristic(this.platform.Characteristic.Model, 'WSDCGQ01LM') .setCharacteristic(this.platform.Characteristic.SerialNumber, '03000' + accessory.context.device.typeId) this.serviceTemperature = this.accessory.getService(this.platform.Service.TemperatureSensor) || this.accessory.addService(this.platform.Service.TemperatureSensor) this.serviceTemperature.getCharacteristic(this.platform.Characteristic.CurrentTemperature) .onGet(this.getTemperature.bind(this)) this.serviceHumidity = this.accessory.getService(this.platform.Service.HumiditySensor) || this.accessory.addService(this.platform.Service.HumiditySensor) this.serviceHumidity.getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity) .onGet(this.getHumidity.bind(this)) this.serviceTemperature.getCharacteristic(this.platform.Characteristic.StatusLowBattery) .onGet(this.getLowBattery.bind(this)) this.serviceHumidity.getCharacteristic(this.platform.Characteristic.StatusLowBattery) .onGet(this.getLowBattery.bind(this)) this.serviceTemperature.getCharacteristic(this.platform.Characteristic.StatusActive) .onGet(this.getActive.bind(this)) this.serviceHumidity.getCharacteristic(this.platform.Characteristic.StatusActive) .onGet(this.getActive.bind(this)) this.setName(accessory.context.device.deviceName) this.update() } setName(name) { this.serviceTemperature.setCharacteristic(this.platform.Characteristic.Name, name) this.serviceHumidity.setCharacteristic(this.platform.Characteristic.Name, name) } setValue(key, value) { if (key == "temperature" && value != "") { this.state.Temperature = value / 100 this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.Temperature) } else if (key == "humidity" && value != "") { this.state.Humidity = value / 100 this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.state.Humidity) } else if (key == "battery" && value != "") { if (value < 25) { this.state.LowBattery = true } else { this.state.LowBattery = false } this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) this.serviceHumidity.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.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) } } update() { const data = JSON.stringify([{ type: 'environment', 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('environment')) { for (const device of devices.environment) { if (device.id == this.accessory.context.device.typeId) { this.state.Temperature = device.temperature this.state.Humidity = device.humidity 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.serviceTemperature.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.Temperature) this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.state.Humidity) this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) break } } } }) .catch((error) => { this.platform.log.debug('environmentSensorAccessory::getTemperature Error ->' + error) }) } getTemperature(): CharacteristicValue { return this.state.Temperature } getHumidity(): CharacteristicValue { return this.state.Humidity } getLowBattery(): CharacteristicValue { return this.state.LowBattery } getActive(): CharacteristicValue { return this.state.Active } }