doorWindowSensorAccessory.ts 5.4 KB

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