Add ActivityAccessory
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
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 activityAccessory extends accessory {
|
||||
private service: Service
|
||||
|
||||
static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let activityDevices: device[] = []
|
||||
|
||||
httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/activity')
|
||||
.then((response) => {
|
||||
let devices = JSON.parse(response.body)
|
||||
|
||||
for (const device of devices) {
|
||||
if (device.enabled == "true") {
|
||||
let object: device = {
|
||||
type: "Activity",
|
||||
typeId: device.id,
|
||||
uniqueId: "Activity." + device.id,
|
||||
deviceName: device.name,
|
||||
switchable: device.switchable,
|
||||
dimmable: "true",
|
||||
detailedType: device.type
|
||||
}
|
||||
activityDevices.push(object)
|
||||
}
|
||||
}
|
||||
|
||||
resolve(activityDevices)
|
||||
})
|
||||
.catch((error) => {
|
||||
reject('activityAccessory::discoverDevices Error ->' + error)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
constructor(
|
||||
private readonly platform: domoticaPlatform,
|
||||
private readonly accessory: PlatformAccessory,
|
||||
) {
|
||||
super("Activity", accessory.context.device.typeId)
|
||||
this.accessory.getService(this.platform.Service.AccessoryInformation)!
|
||||
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Activity')
|
||||
.setCharacteristic(this.platform.Characteristic.Model, 'Domotica-Activity')
|
||||
.setCharacteristic(this.platform.Characteristic.SerialNumber, '08000' + accessory.context.device.typeId)
|
||||
|
||||
//*
|
||||
this.service = this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch)
|
||||
|
||||
this.service.getCharacteristic(this.platform.Characteristic.On)
|
||||
.onGet(this.getOn.bind(this))
|
||||
.onSet(this.setOn.bind(this));
|
||||
/*/
|
||||
this.service = this.accessory.getService(this.platform.Service.StatefulProgrammableSwitch) || this.accessory.addService(this.platform.Service.StatefulProgrammableSwitch)
|
||||
|
||||
this.service.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent)
|
||||
.onGet(this.getOn.bind(this));
|
||||
|
||||
this.service.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState)
|
||||
.onGet(this.getOn.bind(this))
|
||||
.onSet(this.setOn.bind(this));
|
||||
//*/
|
||||
this.setName(accessory.context.device.deviceName)
|
||||
}
|
||||
|
||||
setName(name) {
|
||||
this.service.setCharacteristic(this.platform.Characteristic.Name, name)
|
||||
}
|
||||
|
||||
setValue(key, value) {
|
||||
this.service.updateCharacteristic(this.platform.Characteristic.On, false)
|
||||
}
|
||||
|
||||
update() {
|
||||
}
|
||||
|
||||
getOn(): CharacteristicValue {
|
||||
return false;
|
||||
}
|
||||
|
||||
async setOn(value: CharacteristicValue) {
|
||||
// http://$domoticaIP/external/WebUpdate.php?data=Activity/$id/
|
||||
httpRequest("http://" + this.platform.config.hostname + '/external/WebUpdate.php?data=Activity/' + this.accessory.context.device.typeId + '/')
|
||||
.catch((error) => {
|
||||
this.platform.log.debug('activityAccessory::setOn Error ->' + error)
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -48,7 +48,7 @@ export class httpServer {
|
||||
var type = urlQuery.type
|
||||
var typeId = urlQuery.typeId
|
||||
|
||||
if (type != "PowerSocket" && type != "PowerSwitch" && type != "Xiaomi" && type != "Camera") {
|
||||
if (type != "PowerSocket" && type != "PowerSwitch" && type != "Xiaomi" && type != "Camera" && type != "Activity") {
|
||||
log.debug(request.url)
|
||||
}
|
||||
|
||||
|
||||
+15
@@ -11,6 +11,7 @@ import { environmentSensorAccessory } from './environmentSensorAccessory'
|
||||
import { doorWindowSensorAccessory } from './doorWindowSensorAccessory'
|
||||
import { motionSensorAccessory } from './motionSensorAccessory'
|
||||
import { cameraAccessory } from './cameraAccessory'
|
||||
import { activityAccessory } from './activityAccessory'
|
||||
import { httpServer } from './httpServer'
|
||||
|
||||
export class domoticaPlatform implements DynamicPlatformPlugin {
|
||||
@@ -111,6 +112,16 @@ export class domoticaPlatform implements DynamicPlatformPlugin {
|
||||
.catch((error) => {
|
||||
this.log.debug('cameraAccessory.discoverDevices Error ->' + error)
|
||||
})
|
||||
|
||||
this.discoveryStarted('activityAccessory')
|
||||
activityAccessory.discoverDevices(this)
|
||||
.then((devices) => {
|
||||
this.processDiscoveredDevices(devices)
|
||||
this.discoveryCompleted('activityAccessory')
|
||||
})
|
||||
.catch((error) => {
|
||||
this.log.debug('activityAccessory.discoverDevices Error ->' + error)
|
||||
})
|
||||
}
|
||||
|
||||
processDiscoveredDevices(domoticaDevices: device[]) {
|
||||
@@ -143,6 +154,8 @@ export class domoticaPlatform implements DynamicPlatformPlugin {
|
||||
this.accessories.push(new motionSensorAccessory(this, existingAccessory))
|
||||
} else if (existingAccessory.context.device.type == 'Camera') {
|
||||
this.accessories.push(new cameraAccessory(this, existingAccessory))
|
||||
} else if (existingAccessory.context.device.type == 'Activity') {
|
||||
this.accessories.push(new activityAccessory(this, existingAccessory))
|
||||
}
|
||||
} else {
|
||||
this.log.info('+ ' + device.type + ': ' + device.deviceName)
|
||||
@@ -164,6 +177,8 @@ export class domoticaPlatform implements DynamicPlatformPlugin {
|
||||
this.accessories.push(new motionSensorAccessory(this, accessory))
|
||||
} else if (accessory.context.device.type == 'Camera') {
|
||||
this.accessories.push(new cameraAccessory(this, accessory))
|
||||
} else if (accessory.context.device.type == 'Activity') {
|
||||
this.accessories.push(new activityAccessory(this, accessory))
|
||||
}
|
||||
|
||||
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory])
|
||||
|
||||
Reference in New Issue
Block a user