klikAanKlikUitAccessory.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 klikAanKlikUitAccessory extends accessory {
  7. private service: Service
  8. private state = {
  9. On: false,
  10. Brightness: 100
  11. }
  12. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  13. return new Promise((resolve, reject) => {
  14. let klikaanklikuitDevices: device[] = []
  15. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/klikaanklikuit')
  16. .then((response) => {
  17. let devices = JSON.parse(response.body)
  18. for (const device of devices) {
  19. if (device.enabled == "true") {
  20. let object: device = {
  21. type: "KlikAanKlikUit",
  22. typeId: device.id,
  23. uniqueId: "KlikAanKlikUit." + device.id,
  24. deviceName: device.name,
  25. switchable: "true",
  26. dimmable: device.dimmable,
  27. detailedType: ""
  28. }
  29. klikaanklikuitDevices.push(object)
  30. }
  31. }
  32. resolve(klikaanklikuitDevices)
  33. })
  34. .catch((error) => {
  35. reject('klikAanKlikUitAccessory::discoverDevices Error ->' + error)
  36. })
  37. })
  38. }
  39. constructor(
  40. private readonly platform: domoticaPlatform,
  41. private readonly accessory: PlatformAccessory,
  42. ) {
  43. super("KlikAanKlikUit", accessory.context.device.typeId)
  44. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  45. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'KlikAanKlikUit')
  46. .setCharacteristic(this.platform.Characteristic.Model, 'AC-1000')
  47. .setCharacteristic(this.platform.Characteristic.SerialNumber, '01000' + accessory.context.device.typeId)
  48. this.service = this.accessory.getService(this.platform.Service.Lightbulb) || this.accessory.addService(this.platform.Service.Lightbulb)
  49. this.service.getCharacteristic(this.platform.Characteristic.On)
  50. .onSet(this.setOn.bind(this))
  51. .onGet(this.getOn.bind(this))
  52. if (accessory.context.device.dimmable != "false") { // WTF?
  53. this.service.getCharacteristic(this.platform.Characteristic.Brightness)
  54. .onSet(this.setBrightness.bind(this))
  55. .onGet(this.getBrightness.bind(this))
  56. }
  57. this.setName(accessory.context.device.deviceName)
  58. this.update()
  59. }
  60. setName(name) {
  61. this.service.setCharacteristic(this.platform.Characteristic.Name, name)
  62. }
  63. setValue(key, value) {
  64. if (key == "state" && value != "") {
  65. this.state.On = (value == "on")
  66. this.service.updateCharacteristic(this.platform.Characteristic.On, this.state.On)
  67. } else if (key == "dimLevel" && value!= "") {
  68. this.state.Brightness = value
  69. this.service.updateCharacteristic(this.platform.Characteristic.Brightness, this.state.Brightness)
  70. }
  71. }
  72. update() {
  73. const data = JSON.stringify([{
  74. type: 'klikaanklikuit',
  75. query: 'state'
  76. }])
  77. httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
  78. .then((response) => {
  79. const devices = JSON.parse(response.body)
  80. if (devices.hasOwnProperty('klikaanklikuit')) {
  81. for (const device of devices.klikaanklikuit) {
  82. if (device.id == this.accessory.context.device.typeId) {
  83. if (device.state == 'on') {
  84. this.state.On = true
  85. } else {
  86. this.state.On = false
  87. }
  88. this.service.updateCharacteristic(this.platform.Characteristic.On, this.state.On)
  89. if (device.dimmable != 'false') { // WTF?
  90. this.state.Brightness = device.dimLevel;
  91. this.service.updateCharacteristic(this.platform.Characteristic.Brightness, this.state.Brightness)
  92. }
  93. break
  94. }
  95. }
  96. }
  97. })
  98. .catch((error) => {
  99. this.platform.log.debug('klikAanKlikUitAccessory::update Error ->' + error)
  100. })
  101. }
  102. async setOn(value: CharacteristicValue) {
  103. this.state.On = value as boolean
  104. // http://$domoticaIP/external/WebUpdate.php?data=KlikAanKlikUit/$id/$state/
  105. httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=KlikAanKlikUit/' + this.accessory.context.device.typeId + '/' + (this.state.On? '1': '0') + '/')
  106. .catch((error) => {
  107. this.platform.log.debug('klikAanKlikUitAccessory::setOn Error ->' + error)
  108. })
  109. }
  110. getOn(): CharacteristicValue {
  111. return this.state.On
  112. }
  113. async setBrightness(value: CharacteristicValue) {
  114. this.state.Brightness = value as number
  115. // http://$domoticaIP/external/WebUpdate.php?data=KlikAanKlikUit/$id/$state/$dimLevel
  116. httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=KlikAanKlikUit/' + this.accessory.context.device.typeId + '//' + this.state.Brightness)
  117. .catch((error) => {
  118. this.platform.log.debug('klikAanKlikUitAccessory::setBrightness Error ->' + error)
  119. })
  120. }
  121. getBrightness(): CharacteristicValue {
  122. return this.state.Brightness
  123. }
  124. }