Add SmokeDetectorAccessory

This commit is contained in:
2025-03-03 14:40:36 +01:00
parent 99bd9361cd
commit 6634f66820
2 changed files with 204 additions and 9 deletions
+160
View File
@@ -0,0 +1,160 @@
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 smokeDetectorAccessory extends accessory {
private service: Service
private state = {
SmokeDetected: false,
Tampered: false, // TODO
LowBattery: false,
Active: false
}
static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
return new Promise((resolve, reject) => {
let smokedetectorDevices: device[] = []
httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/smokedetector')
.then((response) => {
let devices = JSON.parse(response.body)
for (const device of devices) {
if (device.enabled == "true") {
let object: device = {
type: "SmokeDetector",
typeId: device.id,
uniqueId: "SmokeDetector." + device.id,
deviceName: device.name,
switchable: "false",
dimmable: "false",
detailedType: device.type
}
smokedetectorDevices.push(object)
}
}
resolve(smokedetectorDevices)
})
.catch((error) => {
reject('smokeDetectorAccessory::discoverDevices Error ->' + error)
})
})
}
constructor(
private readonly platform: domoticaPlatform,
private readonly accessory: PlatformAccessory,
) {
super("SmokeDetector", accessory.context.device.typeId)
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'SmokeDetector')
.setCharacteristic(this.platform.Characteristic.Model, 'ZSDR-850')
.setCharacteristic(this.platform.Characteristic.SerialNumber, '10000' + accessory.context.device.typeId)
this.service = this.accessory.getService(this.platform.Service.SmokeSensor) || this.accessory.addService(this.platform.Service.SmokeSensor)
this.service.getCharacteristic(this.platform.Characteristic.SmokeDetected)
.onGet(this.getSmokeDetected.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 == "smoke") {
this.state.SmokeDetected = true
} else {
this.state.SmokeDetected = false
}
this.service.updateCharacteristic(this.platform.Characteristic.SmokeDetected, this.state.SmokeDetected)
}
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: 'smokedetector',
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('smokedetector')) {
for (const device of devices.smokedetector) {
if (device.id == this.accessory.context.device.typeId) {
if (device.state == 'smoke') {
this.state.SmokeDetected = true
} else {
this.state.SmokeDetected = false
}
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.SmokeDetected, this.state.SmokeDetected)
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('smokeDetectorAccessory::update Error ->' + error)
})
}
getSmokeDetected(): CharacteristicValue {
return this.state.SmokeDetected
}
getLowBattery(): CharacteristicValue {
return this.state.LowBattery
}
getActive(): CharacteristicValue {
return this.state.Active
}
}