doorWindowSensorAccessory.ts 4.2 KB

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