Initial commit
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
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
|
||||
}
|
||||
|
||||
static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
|
||||
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.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)
|
||||
}
|
||||
if (key == "humidity" && value != "") {
|
||||
this.state.Humidity = value / 100
|
||||
this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.state.Humidity)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
for (const device of devices.environment) {
|
||||
if (device.id == this.accessory.context.device.typeId) {
|
||||
this.state.Temperature = device.temperature
|
||||
this.state.Humidity = device.humidity
|
||||
this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.Temperature)
|
||||
this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.state.Humidity)
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
this.platform.log.debug('environmentSensorAccessory::getTemperature Error ->' + error)
|
||||
})
|
||||
}
|
||||
|
||||
getTemperature(): CharacteristicValue {
|
||||
return this.state.Temperature
|
||||
}
|
||||
|
||||
getHumidity(): CharacteristicValue {
|
||||
return this.state.Humidity
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user