powerSwitchAccessory.ts 5.1 KB

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