Initial commit
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
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 = {
|
||||
Position: 0
|
||||
}
|
||||
|
||||
static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
|
||||
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.Door) || this.accessory.addService(this.platform.Service.Door)
|
||||
} else { // window
|
||||
this.service = this.accessory.getService(this.platform.Service.Window) || this.accessory.addService(this.platform.Service.Window)
|
||||
}
|
||||
|
||||
this.service.getCharacteristic(this.platform.Characteristic.CurrentPosition)
|
||||
.onGet(this.getCurrentPosition.bind(this));
|
||||
|
||||
this.service.getCharacteristic(this.platform.Characteristic.PositionState)
|
||||
.onGet(this.getPositionState.bind(this));
|
||||
|
||||
this.service.getCharacteristic(this.platform.Characteristic.TargetPosition)
|
||||
.onGet(this.getTargetPosition.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.Position = 0
|
||||
} else {
|
||||
this.state.Position = 100
|
||||
}
|
||||
this.service.updateCharacteristic(this.platform.Characteristic.CurrentPosition, this.state.Position)
|
||||
this.service.updateCharacteristic(this.platform.Characteristic.TargetPosition, this.state.Position)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
for (const device of devices.doorwindow) {
|
||||
if (device.id == this.accessory.context.device.typeId) {
|
||||
if (device.state == 'closed') {
|
||||
this.state.Position = 0
|
||||
} else { // open
|
||||
this.state.Position = 100
|
||||
}
|
||||
this.service.updateCharacteristic(this.platform.Characteristic.CurrentPosition, this.state.Position)
|
||||
this.service.updateCharacteristic(this.platform.Characteristic.TargetPosition, this.state.Position)
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
this.platform.log.debug('doorWindowSensorAccessory::update Error ->' + error)
|
||||
})
|
||||
}
|
||||
|
||||
getCurrentPosition(): CharacteristicValue {
|
||||
return this.state.Position
|
||||
}
|
||||
|
||||
getPositionState(): CharacteristicValue {
|
||||
return this.platform.Characteristic.PositionState.STOPPED;
|
||||
}
|
||||
|
||||
getTargetPosition(): CharacteristicValue {
|
||||
return this.state.Position
|
||||
}
|
||||
|
||||
getPosition(): CharacteristicValue {
|
||||
return this.state.Position
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user