doorWindowSensorAccessory.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. }
  11. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  12. return new Promise((resolve, reject) => {
  13. let doorWindowSensorDevices: device[] = []
  14. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/doorwindowsensor')
  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: "DoorWindowSensor",
  21. typeId: device.id,
  22. uniqueId: "DoorWindowSensor." + device.id,
  23. deviceName: device.name,
  24. switchable: "false",
  25. dimmable: "false",
  26. detailedType: device.type
  27. }
  28. doorWindowSensorDevices.push(object)
  29. }
  30. }
  31. resolve(doorWindowSensorDevices)
  32. })
  33. .catch((error) => {
  34. reject('doorWindowSensorAccessory::discoverDevices Error ->' + error)
  35. })
  36. })
  37. }
  38. constructor(
  39. private readonly platform: domoticaPlatform,
  40. private readonly accessory: PlatformAccessory,
  41. ) {
  42. super("DoorWindowSensor", 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, 'MCCGQ01LM')
  46. .setCharacteristic(this.platform.Characteristic.SerialNumber, '04000' + accessory.context.device.typeId)
  47. if (accessory.context.device.detailedType == "door") {
  48. this.service = this.accessory.getService(this.platform.Service.ContactSensor) || this.accessory.addService(this.platform.Service.ContactSensor)
  49. } else { // window
  50. this.service = this.accessory.getService(this.platform.Service.ContactSensor) || this.accessory.addService(this.platform.Service.ContactSensor)
  51. }
  52. this.service.getCharacteristic(this.platform.Characteristic.ContactSensorState)
  53. .onGet(this.getContactSensorState.bind(this));
  54. this.setName(accessory.context.device.deviceName)
  55. this.update()
  56. }
  57. setName(name) {
  58. this.service.setCharacteristic(this.platform.Characteristic.Name, name)
  59. }
  60. setValue(key, value) {
  61. if (key == "state" && value != "") {
  62. if (value == "Off") {
  63. this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED
  64. } else {
  65. this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
  66. }
  67. this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.state.ContactSensorState)
  68. }
  69. }
  70. update() {
  71. const data = JSON.stringify([{
  72. type: 'doorwindow',
  73. query: 'state'
  74. }])
  75. httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
  76. .then((response) => {
  77. const devices = JSON.parse(response.body)
  78. if (devices.hasOwnProperty('doorwindow')) {
  79. for (const device of devices.doorwindow) {
  80. if (device.id == this.accessory.context.device.typeId) {
  81. if (device.state == 'closed') {
  82. this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED
  83. } else { // open
  84. this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
  85. }
  86. this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.state.ContactSensorState)
  87. break
  88. }
  89. }
  90. }
  91. })
  92. .catch((error) => {
  93. this.platform.log.debug('doorWindowSensorAccessory::update Error ->' + error)
  94. })
  95. }
  96. getContactSensorState(): CharacteristicValue {
  97. return this.state.ContactSensorState
  98. }
  99. }