environmentSensorAccessory.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 environmentSensorAccessory extends accessory {
  7. private serviceTemperature: Service
  8. private serviceHumidity: Service
  9. private state = {
  10. Temperature: 0,
  11. Humidity: 0,
  12. LowBattery: false,
  13. Active: false
  14. }
  15. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  16. return new Promise((resolve, reject) => {
  17. let environmentSensorDevices: device[] = []
  18. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/environmentsensor')
  19. .then((response) => {
  20. let devices = JSON.parse(response.body)
  21. for (const device of devices) {
  22. if (device.enabled == "true") {
  23. let object: device = {
  24. type: "EnvironmentSensor",
  25. typeId: device.id,
  26. uniqueId: "EnvironmentSensor." + device.id,
  27. deviceName: device.name,
  28. switchable: "false",
  29. dimmable: "false",
  30. detailedType: ""
  31. }
  32. environmentSensorDevices.push(object)
  33. }
  34. }
  35. resolve(environmentSensorDevices)
  36. })
  37. .catch((error) => {
  38. reject('environmentSensorAccessory::discoverDevices Error ->' + error)
  39. })
  40. })
  41. }
  42. constructor(
  43. private readonly platform: domoticaPlatform,
  44. private readonly accessory: PlatformAccessory,
  45. ) {
  46. super("EnvironmentSensor", accessory.context.device.typeId)
  47. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  48. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Xiaomi')
  49. .setCharacteristic(this.platform.Characteristic.Model, 'WSDCGQ01LM')
  50. .setCharacteristic(this.platform.Characteristic.SerialNumber, '03000' + accessory.context.device.typeId)
  51. this.serviceTemperature = this.accessory.getService(this.platform.Service.TemperatureSensor) || this.accessory.addService(this.platform.Service.TemperatureSensor)
  52. this.serviceTemperature.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
  53. .onGet(this.getTemperature.bind(this))
  54. this.serviceHumidity = this.accessory.getService(this.platform.Service.HumiditySensor) || this.accessory.addService(this.platform.Service.HumiditySensor)
  55. this.serviceHumidity.getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
  56. .onGet(this.getHumidity.bind(this))
  57. this.serviceTemperature.getCharacteristic(this.platform.Characteristic.StatusLowBattery)
  58. .onGet(this.getLowBattery.bind(this))
  59. this.serviceHumidity.getCharacteristic(this.platform.Characteristic.StatusLowBattery)
  60. .onGet(this.getLowBattery.bind(this))
  61. this.serviceTemperature.getCharacteristic(this.platform.Characteristic.StatusActive)
  62. .onGet(this.getActive.bind(this))
  63. this.serviceHumidity.getCharacteristic(this.platform.Characteristic.StatusActive)
  64. .onGet(this.getActive.bind(this))
  65. this.setName(accessory.context.device.deviceName)
  66. this.update()
  67. }
  68. setName(name) {
  69. this.serviceTemperature.setCharacteristic(this.platform.Characteristic.Name, name)
  70. this.serviceHumidity.setCharacteristic(this.platform.Characteristic.Name, name)
  71. }
  72. setValue(key, value) {
  73. if (key == "temperature" && value != "") {
  74. this.state.Temperature = value / 100
  75. this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.Temperature)
  76. }
  77. else if (key == "humidity" && value != "") {
  78. this.state.Humidity = value / 100
  79. this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.state.Humidity)
  80. }
  81. else if (key == "battery" && value != "") {
  82. if (value < 25) {
  83. this.state.LowBattery = true
  84. } else {
  85. this.state.LowBattery = false
  86. }
  87. this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery)
  88. this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery)
  89. }
  90. else if (key == "online" && value != "") {
  91. if (value == "false") {
  92. this.state.Active = false
  93. } else {
  94. this.state.Active = true
  95. }
  96. this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active)
  97. this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active)
  98. }
  99. }
  100. update() {
  101. const data = JSON.stringify([{
  102. type: 'environment',
  103. query: 'state'
  104. }])
  105. httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
  106. .then((response) => {
  107. const devices = JSON.parse(response.body)
  108. if (devices.hasOwnProperty('environment')) {
  109. for (const device of devices.environment) {
  110. if (device.id == this.accessory.context.device.typeId) {
  111. this.state.Temperature = device.temperature
  112. this.state.Humidity = device.humidity
  113. if (device.battery < 25) {
  114. this.state.LowBattery = true
  115. } else {
  116. this.state.LowBattery = false
  117. }
  118. if (device.online == "false") {
  119. this.state.Active = false
  120. } else {
  121. this.state.Active = true
  122. }
  123. this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.Temperature)
  124. this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.state.Humidity)
  125. this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery)
  126. this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery)
  127. this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active)
  128. this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active)
  129. break
  130. }
  131. }
  132. }
  133. })
  134. .catch((error) => {
  135. this.platform.log.debug('environmentSensorAccessory::getTemperature Error ->' + error)
  136. })
  137. }
  138. getTemperature(): CharacteristicValue {
  139. return this.state.Temperature
  140. }
  141. getHumidity(): CharacteristicValue {
  142. return this.state.Humidity
  143. }
  144. getLowBattery(): CharacteristicValue {
  145. return this.state.LowBattery
  146. }
  147. getActive(): CharacteristicValue {
  148. return this.state.Active
  149. }
  150. }