doorWindowSensorAccessory.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. Position: 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. /*
  55. this.service.getCharacteristic(this.platform.Characteristic.CurrentPosition)
  56. .onGet(this.getCurrentPosition.bind(this));
  57. this.service.getCharacteristic(this.platform.Characteristic.PositionState)
  58. .onGet(this.getPositionState.bind(this));
  59. this.service.getCharacteristic(this.platform.Characteristic.TargetPosition)
  60. .onGet(this.getTargetPosition.bind(this))
  61. */
  62. this.setName(accessory.context.device.deviceName)
  63. this.update()
  64. }
  65. setName(name) {
  66. this.service.setCharacteristic(this.platform.Characteristic.Name, name)
  67. }
  68. setValue(key, value) {
  69. if (key == "state" && value != "") {
  70. if (value == "Off") {
  71. this.state.Position = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED
  72. } else {
  73. this.state.Position = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
  74. }
  75. /*
  76. this.service.updateCharacteristic(this.platform.Characteristic.CurrentPosition, this.state.Position)
  77. this.service.updateCharacteristic(this.platform.Characteristic.TargetPosition, this.state.Position)
  78. */
  79. }
  80. }
  81. update() {
  82. const data = JSON.stringify([{
  83. type: 'doorwindow',
  84. query: 'state'
  85. }])
  86. httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
  87. .then((response) => {
  88. const devices = JSON.parse(response.body)
  89. for (const device of devices.doorwindow) {
  90. if (device.id == this.accessory.context.device.typeId) {
  91. if (device.state == 'closed') {
  92. this.state.Position = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED
  93. } else { // open
  94. this.state.Position = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
  95. }
  96. /*
  97. this.service.updateCharacteristic(this.platform.Characteristic.CurrentPosition, this.state.Position)
  98. this.service.updateCharacteristic(this.platform.Characteristic.TargetPosition, this.state.Position)
  99. */
  100. break
  101. }
  102. }
  103. })
  104. .catch((error) => {
  105. this.platform.log.debug('doorWindowSensorAccessory::update Error ->' + error)
  106. })
  107. }
  108. /*
  109. getCurrentPosition(): CharacteristicValue {
  110. return this.state.Position
  111. }
  112. getPositionState(): CharacteristicValue {
  113. return this.platform.Characteristic.PositionState.STOPPED;
  114. }
  115. getTargetPosition(): CharacteristicValue {
  116. return this.state.Position
  117. }
  118. getPosition(): CharacteristicValue {
  119. return this.state.Position
  120. }
  121. */
  122. getContactSensorState(): CharacteristicValue {
  123. return this.state.Position
  124. }
  125. }