motionSensorAccessory.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  11. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  12. return new Promise((resolve, reject) => {
  13. let motionSensorDevices: device[] = []
  14. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/motionsensor')
  15. .then((response) => {
  16. let devices = JSON.parse(response.body)
  17. for (const device of devices) {
  18. if (device.enabled == "true") {
  19. let object: device = {
  20. type: "MotionSensor",
  21. typeId: device.id,
  22. uniqueId: "MotionSensor." + device.id,
  23. deviceName: device.name,
  24. switchable: "false",
  25. dimmable: "false",
  26. detailedType: device.type
  27. }
  28. motionSensorDevices.push(object)
  29. }
  30. }
  31. resolve(motionSensorDevices)
  32. })
  33. .catch((error) => {
  34. reject('motionSensorAccessory::discoverDevices Error ->' + error)
  35. })
  36. })
  37. }
  38. constructor(
  39. private readonly platform: domoticaPlatform,
  40. private readonly accessory: PlatformAccessory,
  41. ) {
  42. super("MotionSensor", accessory.context.device.typeId)
  43. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  44. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Xiaomi')
  45. .setCharacteristic(this.platform.Characteristic.Model, 'RTCGQ01LM')
  46. .setCharacteristic(this.platform.Characteristic.SerialNumber, '05000' + accessory.context.device.typeId)
  47. this.service = this.accessory.getService(this.platform.Service.MotionSensor) || this.accessory.addService(this.platform.Service.MotionSensor)
  48. this.service.getCharacteristic(this.platform.Characteristic.MotionDetected)
  49. .onGet(this.getState.bind(this));
  50. this.setName(accessory.context.device.deviceName)
  51. this.update()
  52. }
  53. setName(name) {
  54. this.service.setCharacteristic(this.platform.Characteristic.Name, name)
  55. }
  56. setValue(key, value) {
  57. if (key == "state" && value != "") {
  58. if (value == "On") {
  59. this.state.Motion = true
  60. } else {
  61. this.state.Motion = false
  62. }
  63. this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.state.Motion)
  64. }
  65. }
  66. update() {
  67. const data = JSON.stringify([{
  68. type: 'motion',
  69. query: 'state'
  70. }])
  71. httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
  72. .then((response) => {
  73. const devices = JSON.parse(response.body)
  74. for (const device of devices.motion) {
  75. if (device.id == this.accessory.context.device.typeId) {
  76. if (device.state == 'absent') {
  77. this.state.Motion = false
  78. } else { // present
  79. this.state.Motion = true
  80. }
  81. return this.state.Motion
  82. this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.state.Motion)
  83. break
  84. }
  85. }
  86. })
  87. .catch((error) => {
  88. this.platform.log.debug('motionSensorAccessory::update Error ->' + error)
  89. })
  90. }
  91. getState(): CharacteristicValue {
  92. return this.state.Motion
  93. }
  94. }