activityAccessory.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge'
  2. import { accessory } from './accessory'
  3. import { device } from './device'
  4. import { httpRequest, response } from './httpPromise'
  5. import { domoticaPlatform } from './platform'
  6. export class activityAccessory extends accessory {
  7. private service: Service
  8. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  9. return new Promise((resolve, reject) => {
  10. let activityDevices: device[] = []
  11. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/activity')
  12. .then((response) => {
  13. let devices = JSON.parse(response.body)
  14. for (const device of devices) {
  15. if (device.enabled == "true") {
  16. let object: device = {
  17. type: "Activity",
  18. typeId: device.id,
  19. uniqueId: "Activity." + device.id,
  20. deviceName: device.name,
  21. switchable: device.switchable,
  22. dimmable: "true",
  23. detailedType: device.type
  24. }
  25. activityDevices.push(object)
  26. }
  27. }
  28. resolve(activityDevices)
  29. })
  30. .catch((error) => {
  31. reject('activityAccessory::discoverDevices Error ->' + error)
  32. })
  33. })
  34. }
  35. constructor(
  36. private readonly platform: domoticaPlatform,
  37. private readonly accessory: PlatformAccessory,
  38. ) {
  39. super("Activity", accessory.context.device.typeId)
  40. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  41. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Activity')
  42. .setCharacteristic(this.platform.Characteristic.Model, 'Domotica-Activity')
  43. .setCharacteristic(this.platform.Characteristic.SerialNumber, '08000' + accessory.context.device.typeId)
  44. //*
  45. this.service = this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch)
  46. this.service.getCharacteristic(this.platform.Characteristic.On)
  47. .onGet(this.getOn.bind(this))
  48. .onSet(this.setOn.bind(this));
  49. /*/
  50. this.service = this.accessory.getService(this.platform.Service.StatefulProgrammableSwitch) || this.accessory.addService(this.platform.Service.StatefulProgrammableSwitch)
  51. this.service.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent)
  52. .onGet(this.getOn.bind(this));
  53. this.service.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState)
  54. .onGet(this.getOn.bind(this))
  55. .onSet(this.setOn.bind(this));
  56. //*/
  57. this.setName(accessory.context.device.deviceName)
  58. }
  59. setName(name) {
  60. this.service.setCharacteristic(this.platform.Characteristic.Name, name)
  61. }
  62. setValue(key, value) {
  63. this.service.updateCharacteristic(this.platform.Characteristic.On, false)
  64. }
  65. update() {
  66. }
  67. getOn(): CharacteristicValue {
  68. return false;
  69. }
  70. async setOn(value: CharacteristicValue) {
  71. // http://$domoticaIP/external/WebUpdate.php?data=Activity/$id/
  72. httpRequest("http://" + this.platform.config.hostname + '/external/WebUpdate.php?data=Activity/' + this.accessory.context.device.typeId + '/')
  73. .catch((error) => {
  74. this.platform.log.debug('activityAccessory::setOn Error ->' + error)
  75. })
  76. }
  77. }