Initial commit

This commit is contained in:
2022-07-12 16:34:41 +02:00
commit e7947dc769
14 changed files with 1287 additions and 0 deletions
+127
View File
@@ -0,0 +1,127 @@
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 powerSocketAccessory extends accessory {
private service: Service
private state = {
On: false
}
static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
return new Promise((resolve, reject) => {
let powersocketDevices: device[] = []
httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/powersocket')
.then((response) => {
let devices = JSON.parse(response.body)
for (const device of devices) {
if (device.enabled == "true" &&
device.switchable == "true") {
let object: device = {
type: "PowerSocket",
typeId: device.id,
uniqueId: "PowerSocket." + device.id,
deviceName: device.name,
switchable: device.switchable,
dimmable: "false",
detailedType: device.type
}
powersocketDevices.push(object)
}
}
resolve(powersocketDevices)
})
.catch((error) => {
reject('powerSocketAccessory::discoverDevices Error ->' + error)
})
})
}
constructor(
private readonly platform: domoticaPlatform,
private readonly accessory: PlatformAccessory,
) {
super("PowerSocket", accessory.context.device.typeId)
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'PowerSocket')
.setCharacteristic(this.platform.Characteristic.Model, 'BW-SHP6')
.setCharacteristic(this.platform.Characteristic.SerialNumber, '02000' + accessory.context.device.typeId)
if (accessory.context.device.detailedType == "light") {
this.service = this.accessory.getService(this.platform.Service.Lightbulb) || this.accessory.addService(this.platform.Service.Lightbulb)
} else { // powersocket
this.service = this.accessory.getService(this.platform.Service.Outlet) || this.accessory.addService(this.platform.Service.Outlet)
}
if (accessory.context.device.switchable == "true") {
this.service.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this))
} else {
this.service.getCharacteristic(this.platform.Characteristic.On)
.onGet(this.getOn.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 != "") {
this.state.On = (value == "1")
this.service.updateCharacteristic(this.platform.Characteristic.On, this.state.On)
}
}
update() {
const data = JSON.stringify([{
type: 'powersocket',
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.powersocket) {
if (device.id == this.accessory.context.device.typeId) {
if (device.state == 'on') {
this.state.On = true
} else {
this.state.On = false
}
this.service.updateCharacteristic(this.platform.Characteristic.On, this.state.On)
break
}
}
})
.catch((error) => {
this.platform.log.debug('powerSocketAccessory::update Error ->' + error)
})
}
async setOn(value: CharacteristicValue) {
this.state.On = value as boolean
// http://$domoticaIP/external/WebUpdate.php?data=PowerSocket/$id/$state/
httpRequest("http://" + this.platform.config.hostname + '/external/WebUpdate.php?data=PowerSocket/' + this.accessory.context.device.typeId + '/' + (this.state.On? '1': '0') + '/')
.catch((error) => {
this.platform.log.debug('powerSocketAccessory::setOn Error ->' + error)
})
}
getOn(): CharacteristicValue {
return this.state.On
}
}