powerSocketAccessory.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 powerSocketAccessory extends accessory {
  7. private service: Service
  8. private state = {
  9. On: false
  10. }
  11. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  12. return new Promise((resolve, reject) => {
  13. let powersocketDevices: device[] = []
  14. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/powersocket')
  15. .then((response) => {
  16. let devices = JSON.parse(response.body)
  17. for (const device of devices) {
  18. if (device.enabled == "true" &&
  19. device.switchable == "true") {
  20. let object: device = {
  21. type: "PowerSocket",
  22. typeId: device.id,
  23. uniqueId: "PowerSocket." + device.id,
  24. deviceName: device.name,
  25. switchable: device.switchable,
  26. dimmable: "false",
  27. detailedType: device.type
  28. }
  29. powersocketDevices.push(object)
  30. }
  31. }
  32. resolve(powersocketDevices)
  33. })
  34. .catch((error) => {
  35. reject('powerSocketAccessory::discoverDevices Error ->' + error)
  36. })
  37. })
  38. }
  39. constructor(
  40. private readonly platform: domoticaPlatform,
  41. private readonly accessory: PlatformAccessory,
  42. ) {
  43. super("PowerSocket", accessory.context.device.typeId)
  44. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  45. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'PowerSocket')
  46. .setCharacteristic(this.platform.Characteristic.Model, 'BW-SHP6')
  47. .setCharacteristic(this.platform.Characteristic.SerialNumber, '02000' + 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 { // powersocket
  51. this.service = this.accessory.getService(this.platform.Service.Outlet) || this.accessory.addService(this.platform.Service.Outlet)
  52. }
  53. if (accessory.context.device.switchable == "true") {
  54. this.service.getCharacteristic(this.platform.Characteristic.On)
  55. .onSet(this.setOn.bind(this))
  56. .onGet(this.getOn.bind(this))
  57. } else {
  58. this.service.getCharacteristic(this.platform.Characteristic.On)
  59. .onGet(this.getOn.bind(this))
  60. }
  61. this.setName(accessory.context.device.deviceName)
  62. this.update()
  63. }
  64. setName(name) {
  65. this.service.setCharacteristic(this.platform.Characteristic.Name, name)
  66. }
  67. setValue(key, value) {
  68. if (key == "state" && value != "") {
  69. this.state.On = (value == "1")
  70. this.service.updateCharacteristic(this.platform.Characteristic.On, this.state.On)
  71. }
  72. }
  73. update() {
  74. const data = JSON.stringify([{
  75. type: 'powersocket',
  76. query: 'state'
  77. }])
  78. httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
  79. .then((response) => {
  80. const devices = JSON.parse(response.body)
  81. for (const device of devices.powersocket) {
  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. break
  90. }
  91. }
  92. })
  93. .catch((error) => {
  94. this.platform.log.debug('powerSocketAccessory::update Error ->' + error)
  95. })
  96. }
  97. async setOn(value: CharacteristicValue) {
  98. this.state.On = value as boolean
  99. // http://$domoticaIP/external/WebUpdate.php?data=PowerSocket/$id/$state/
  100. httpRequest("http://" + this.platform.config.hostname + '/external/WebUpdate.php?data=PowerSocket/' + this.accessory.context.device.typeId + '/' + (this.state.On? '1': '0') + '/')
  101. .catch((error) => {
  102. this.platform.log.debug('powerSocketAccessory::setOn Error ->' + error)
  103. })
  104. }
  105. getOn(): CharacteristicValue {
  106. return this.state.On
  107. }
  108. }