Files
HomebridgeDomotica/doorWindowSensorAccessory.ts
T
2025-03-03 14:27:52 +01:00

118 lines
4.0 KiB
TypeScript

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,
}
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.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.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)
}
}
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
}
this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.state.ContactSensorState)
break
}
}
}
})
.catch((error) => {
this.platform.log.debug('doorWindowSensorAccessory::update Error ->' + error)
})
}
getContactSensorState(): CharacteristicValue {
return this.state.ContactSensorState
}
}