Initial commit
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
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 powerSwitchAccessory extends accessory {
|
||||
private service: Service
|
||||
|
||||
private state = {
|
||||
On: false,
|
||||
Brightness: 100
|
||||
}
|
||||
|
||||
static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let powerswitchDevices: device[] = []
|
||||
|
||||
httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/powerswitch')
|
||||
.then((response) => {
|
||||
let devices = JSON.parse(response.body)
|
||||
|
||||
for (const device of devices) {
|
||||
if (device.enabled == "true") {
|
||||
let object: device = {
|
||||
type: "PowerSwitch",
|
||||
typeId: device.id,
|
||||
uniqueId: "PowerSwitch." + device.id,
|
||||
deviceName: device.name,
|
||||
switchable: "true",
|
||||
dimmable: device.dimmable,
|
||||
detailedType: device.type
|
||||
}
|
||||
powerswitchDevices.push(object)
|
||||
}
|
||||
}
|
||||
|
||||
resolve(powerswitchDevices)
|
||||
})
|
||||
.catch((error) => {
|
||||
reject('powerSwitchAccessory::discoverDevices Error ->' + error)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
constructor(
|
||||
private readonly platform: domoticaPlatform,
|
||||
private readonly accessory: PlatformAccessory,
|
||||
) {
|
||||
super("PowerSwitch", accessory.context.device.typeId)
|
||||
this.accessory.getService(this.platform.Service.AccessoryInformation)!
|
||||
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'PowerSwitch')
|
||||
.setCharacteristic(this.platform.Characteristic.Model, 'Shelly')
|
||||
.setCharacteristic(this.platform.Characteristic.SerialNumber, '06000' + 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 if (accessory.context.device.detailedType == "fan") {
|
||||
this.service = this.accessory.getService(this.platform.Service.Fan) || this.accessory.addService(this.platform.Service.Fan)
|
||||
} else { // other
|
||||
this.service = this.accessory.getService(this.platform.Service.Outlet) || this.accessory.addService(this.platform.Service.Outlet)
|
||||
}
|
||||
|
||||
this.service.getCharacteristic(this.platform.Characteristic.On)
|
||||
.onSet(this.setOn.bind(this))
|
||||
.onGet(this.getOn.bind(this))
|
||||
|
||||
if (accessory.context.device.dimmable != "false") { // WTF?
|
||||
this.service.getCharacteristic(this.platform.Characteristic.Brightness)
|
||||
.onSet(this.setBrightness.bind(this))
|
||||
.onGet(this.getBrightness.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)
|
||||
} else if (key == "dimLevel" && value != "") {
|
||||
this.state.Brightness = value
|
||||
this.service.updateCharacteristic(this.platform.Characteristic.Brightness, this.state.Brightness)
|
||||
}
|
||||
}
|
||||
|
||||
update() {
|
||||
const data = JSON.stringify([{
|
||||
type: 'powerswitch',
|
||||
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.powerswitch) {
|
||||
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)
|
||||
|
||||
if (device.dimmable != 'false') { // WTF?
|
||||
this.state.Brightness = device.dimlevel;
|
||||
this.service.updateCharacteristic(this.platform.Characteristic.Brightness, this.state.Brightness)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
this.platform.log.debug('powerSwitchAccessory::update Error ->' + error)
|
||||
})
|
||||
}
|
||||
|
||||
async setOn(value: CharacteristicValue) {
|
||||
this.state.On = value as boolean
|
||||
|
||||
// http://$domoticaIP/external/WebUpdate.php?data=PowerSwitch/$id/$state/
|
||||
httpRequest("http://" + this.platform.config.hostname + '/external/WebUpdate.php?data=PowerSwitch/' + this.accessory.context.device.typeId + '/' + (this.state.On? '1': '0') + '/')
|
||||
.catch((error) => {
|
||||
this.platform.log.debug('powerSwitchAccessory::setOn Error ->' + error)
|
||||
})
|
||||
}
|
||||
|
||||
getOn(): CharacteristicValue {
|
||||
return this.state.On
|
||||
}
|
||||
|
||||
async setBrightness(value: CharacteristicValue) {
|
||||
this.state.Brightness = value as number
|
||||
|
||||
// http://$domoticaIP/external/WebUpdate.php?data=PowerSwitch/$id/$state/$dimLevel
|
||||
httpRequest("http://" + this.platform.config.hostname + '/external/WebUpdate.php?data=PowerSwitch/' + this.accessory.context.device.typeId + '//' + this.state.Brightness)
|
||||
.catch((error) => {
|
||||
this.platform.log.debug('powerSwitchAccessory::setBrightness Error ->' + error)
|
||||
})
|
||||
}
|
||||
|
||||
getBrightness(): CharacteristicValue {
|
||||
return this.state.Brightness
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user