smokeDetectorAccessory.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 smokeDetectorAccessory extends accessory {
  7. private service: Service
  8. private state = {
  9. SmokeDetected: false,
  10. Tampered: false, // TODO
  11. LowBattery: false,
  12. Active: false
  13. }
  14. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  15. return new Promise((resolve, reject) => {
  16. let smokedetectorDevices: device[] = []
  17. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/smokedetector')
  18. .then((response) => {
  19. let devices = JSON.parse(response.body)
  20. for (const device of devices) {
  21. if (device.enabled == "true") {
  22. let object: device = {
  23. type: "SmokeDetector",
  24. typeId: device.id,
  25. uniqueId: "SmokeDetector." + device.id,
  26. deviceName: device.name,
  27. switchable: "false",
  28. dimmable: "false",
  29. detailedType: device.type
  30. }
  31. smokedetectorDevices.push(object)
  32. }
  33. }
  34. resolve(smokedetectorDevices)
  35. })
  36. .catch((error) => {
  37. reject('smokeDetectorAccessory::discoverDevices Error ->' + error)
  38. })
  39. })
  40. }
  41. constructor(
  42. private readonly platform: domoticaPlatform,
  43. private readonly accessory: PlatformAccessory,
  44. ) {
  45. super("SmokeDetector", accessory.context.device.typeId)
  46. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  47. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SmokeDetector')
  48. .setCharacteristic(this.platform.Characteristic.Model, 'ZSDR-850')
  49. .setCharacteristic(this.platform.Characteristic.SerialNumber, '10000' + accessory.context.device.typeId)
  50. this.service = this.accessory.getService(this.platform.Service.SmokeSensor) || this.accessory.addService(this.platform.Service.SmokeSensor)
  51. this.service.getCharacteristic(this.platform.Characteristic.SmokeDetected)
  52. .onGet(this.getSmokeDetected.bind(this))
  53. this.service.getCharacteristic(this.platform.Characteristic.StatusLowBattery)
  54. .onGet(this.getLowBattery.bind(this))
  55. this.service.getCharacteristic(this.platform.Characteristic.StatusActive)
  56. .onGet(this.getActive.bind(this))
  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. if (value == "smoke") {
  66. this.state.SmokeDetected = true
  67. } else {
  68. this.state.SmokeDetected = false
  69. }
  70. this.service.updateCharacteristic(this.platform.Characteristic.SmokeDetected, this.state.SmokeDetected)
  71. }
  72. else if (key == "battery" && value != "") {
  73. if (value < 25) {
  74. this.state.LowBattery = true
  75. } else {
  76. this.state.LowBattery = false
  77. }
  78. this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery)
  79. }
  80. else if (key == "online" && value != "") {
  81. if (value == "false") {
  82. this.state.Active = false
  83. } else {
  84. this.state.Active = true
  85. }
  86. this.service.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active)
  87. }
  88. }
  89. update() {
  90. const data = JSON.stringify([{
  91. type: 'smokedetector',
  92. query: 'state'
  93. }])
  94. httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
  95. .then((response) => {
  96. const devices = JSON.parse(response.body)
  97. if (devices.hasOwnProperty('smokedetector')) {
  98. for (const device of devices.smokedetector) {
  99. if (device.id == this.accessory.context.device.typeId) {
  100. if (device.state == 'smoke') {
  101. this.state.SmokeDetected = true
  102. } else {
  103. this.state.SmokeDetected = false
  104. }
  105. if (device.battery < 25) {
  106. this.state.LowBattery = true
  107. } else {
  108. this.state.LowBattery = false
  109. }
  110. if (device.online == "false") {
  111. this.state.Active = false
  112. } else {
  113. this.state.Active = true
  114. }
  115. this.service.updateCharacteristic(this.platform.Characteristic.SmokeDetected, this.state.SmokeDetected)
  116. this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery)
  117. this.service.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active)
  118. break
  119. }
  120. }
  121. }
  122. })
  123. .catch((error) => {
  124. this.platform.log.debug('smokeDetectorAccessory::update Error ->' + error)
  125. })
  126. }
  127. getSmokeDetected(): CharacteristicValue {
  128. return this.state.SmokeDetected
  129. }
  130. getLowBattery(): CharacteristicValue {
  131. return this.state.LowBattery
  132. }
  133. getActive(): CharacteristicValue {
  134. return this.state.Active
  135. }
  136. }