environmentSensorAccessory.ts 4.1 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 environmentSensorAccessory extends accessory {
  7. private serviceTemperature: Service
  8. private serviceHumidity: Service
  9. private state = {
  10. Temperature: 0,
  11. Humidity: 0
  12. }
  13. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  14. return new Promise((resolve, reject) => {
  15. let environmentSensorDevices: device[] = []
  16. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/environmentsensor')
  17. .then((response) => {
  18. let devices = JSON.parse(response.body)
  19. for (const device of devices) {
  20. if (device.enabled == "true") {
  21. let object: device = {
  22. type: "EnvironmentSensor",
  23. typeId: device.id,
  24. uniqueId: "EnvironmentSensor." + device.id,
  25. deviceName: device.name,
  26. switchable: "false",
  27. dimmable: "false",
  28. detailedType: ""
  29. }
  30. environmentSensorDevices.push(object)
  31. }
  32. }
  33. resolve(environmentSensorDevices)
  34. })
  35. .catch((error) => {
  36. reject('environmentSensorAccessory::discoverDevices Error ->' + error)
  37. })
  38. })
  39. }
  40. constructor(
  41. private readonly platform: domoticaPlatform,
  42. private readonly accessory: PlatformAccessory,
  43. ) {
  44. super("EnvironmentSensor", accessory.context.device.typeId)
  45. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  46. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Xiaomi')
  47. .setCharacteristic(this.platform.Characteristic.Model, 'WSDCGQ01LM')
  48. .setCharacteristic(this.platform.Characteristic.SerialNumber, '03000' + accessory.context.device.typeId)
  49. this.serviceTemperature = this.accessory.getService(this.platform.Service.TemperatureSensor) || this.accessory.addService(this.platform.Service.TemperatureSensor)
  50. this.serviceTemperature.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
  51. .onGet(this.getTemperature.bind(this))
  52. this.serviceHumidity = this.accessory.getService(this.platform.Service.HumiditySensor) || this.accessory.addService(this.platform.Service.HumiditySensor)
  53. this.serviceHumidity.getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
  54. .onGet(this.getHumidity.bind(this))
  55. this.setName(accessory.context.device.deviceName)
  56. this.update()
  57. }
  58. setName(name) {
  59. this.serviceTemperature.setCharacteristic(this.platform.Characteristic.Name, name)
  60. this.serviceHumidity.setCharacteristic(this.platform.Characteristic.Name, name)
  61. }
  62. setValue(key, value) {
  63. if (key == "temperature" && value != "") {
  64. this.state.Temperature = value / 100
  65. this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.Temperature)
  66. }
  67. if (key == "humidity" && value != "") {
  68. this.state.Humidity = value / 100
  69. this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.state.Humidity)
  70. }
  71. }
  72. update() {
  73. const data = JSON.stringify([{
  74. type: 'environment',
  75. query: 'state'
  76. }])
  77. httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
  78. .then((response) => {
  79. const devices = JSON.parse(response.body)
  80. for (const device of devices.environment) {
  81. if (device.id == this.accessory.context.device.typeId) {
  82. this.state.Temperature = device.temperature
  83. this.state.Humidity = device.humidity
  84. this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.Temperature)
  85. this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.state.Humidity)
  86. break
  87. }
  88. }
  89. })
  90. .catch((error) => {
  91. this.platform.log.debug('environmentSensorAccessory::getTemperature Error ->' + error)
  92. })
  93. }
  94. getTemperature(): CharacteristicValue {
  95. return this.state.Temperature
  96. }
  97. getHumidity(): CharacteristicValue {
  98. return this.state.Humidity
  99. }
  100. }