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