thermostatAccessory.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 thermostatAccessory extends accessory {
  7. private service: Service
  8. private state = {
  9. CurrentHeatingCoolingState: 0,
  10. TargetHeatingCoolingState: 0,
  11. CurrentTemperature: 0.0,
  12. TargetTemperature: 10.0,
  13. TemperatureDisplayUnits: 0
  14. }
  15. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  16. return new Promise((resolve, reject) => {
  17. let thermostatDevices: device[] = []
  18. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/toonthermostat')
  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: "Thermostat",
  25. typeId: device.id,
  26. uniqueId: "Thermostat." + device.id,
  27. deviceName: device.name,
  28. switchable: "false",
  29. dimmable: "false",
  30. detailedType: ""
  31. }
  32. thermostatDevices.push(object)
  33. }
  34. }
  35. resolve(thermostatDevices)
  36. })
  37. .catch((error) => {
  38. reject('thermostatAccessory::discoverDevices Error ->' + error)
  39. })
  40. })
  41. }
  42. constructor(
  43. private readonly platform: domoticaPlatform,
  44. private readonly accessory: PlatformAccessory,
  45. ) {
  46. // https://developers.homebridge.io/#/service/Thermostat
  47. super("Thermostat", accessory.context.device.typeId)
  48. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  49. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Thermostat')
  50. .setCharacteristic(this.platform.Characteristic.Model, 'Domotica-Thermostat')
  51. .setCharacteristic(this.platform.Characteristic.SerialNumber, '09000' + accessory.context.device.typeId)
  52. this.service = this.accessory.getService(this.platform.Service.Thermostat) || this.accessory.addService(this.platform.Service.Thermostat)
  53. this.service.getCharacteristic(this.platform.Characteristic.CurrentHeatingCoolingState)
  54. .onGet(this.getCurrentHeatingCoolingState.bind(this));
  55. this.service.getCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState)
  56. .onGet(this.getTargetHeatingCoolingState.bind(this));
  57. //.onSet(this.setTargetHeatingCoolingState.bind(this));
  58. this.service.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
  59. .onGet(this.getCurrentTemperature.bind(this));
  60. this.service.getCharacteristic(this.platform.Characteristic.TargetTemperature)
  61. .onGet(this.getTargetTemperature.bind(this))
  62. .onSet(this.setTargetTemperature.bind(this));
  63. this.service.getCharacteristic(this.platform.Characteristic.TemperatureDisplayUnits)
  64. .onGet(this.getTemperatureDisplayUnits.bind(this));
  65. //.onSet(this.setTemperatureDisplayUnits.bind(this));
  66. this.setName(accessory.context.device.deviceName)
  67. this.update()
  68. }
  69. setName(name) {
  70. this.service.setCharacteristic(this.platform.Characteristic.Name, name)
  71. }
  72. setValue(key, value) {
  73. if (key == "burnerState" && value != "") {
  74. if (value == 'Off') {
  75. this.state.CurrentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.OFF
  76. this.state.TargetHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.OFF
  77. } else {
  78. this.state.CurrentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.HEAT
  79. this.state.TargetHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.HEAT
  80. }
  81. this.service.updateCharacteristic(this.platform.Characteristic.CurrentHeatingCoolingState, this.state.CurrentHeatingCoolingState)
  82. this.service.updateCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState, this.state.TargetHeatingCoolingState)
  83. } else if (key == "currentSetPoint" && value != "") {
  84. this.state.TargetTemperature = value / 100
  85. this.service.updateCharacteristic(this.platform.Characteristic.TargetTemperature, this.state.TargetTemperature)
  86. } else if (key == "currentTemperature" && value != "") {
  87. this.state.CurrentTemperature = value / 100
  88. this.service.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.CurrentTemperature)
  89. }
  90. }
  91. update() {
  92. // curl -X POST "http://192.168.101.100:8080/API/Status" -d '[{"type": "toonthermostat", "query": "state"}]'
  93. // {"toonthermostat":
  94. // [{"burnerlevel":"low",
  95. // "burnerstate":"heater",
  96. // "currentsetpoint":"1800",
  97. // "currenttemperature":"1805",
  98. // "device_id":"113",
  99. // "id":"1",
  100. // "name":"Toon",
  101. // "programstate":"off",
  102. // "temperaturestate":"Home"}]}
  103. const data = JSON.stringify([{
  104. type: 'toonthermostat',
  105. query: 'state'
  106. }])
  107. httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
  108. .then((response) => {
  109. const devices = JSON.parse(response.body)
  110. if (devices.hasOwnProperty('toonthermostat')) {
  111. for (const device of devices.toonthermostat) {
  112. if (device.id == this.accessory.context.device.typeId) {
  113. if (device.burnerstate == 'off') {
  114. this.state.CurrentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.OFF
  115. this.state.TargetHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.OFF
  116. } else {
  117. this.state.CurrentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.HEAT
  118. this.state.TargetHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.HEAT
  119. }
  120. this.state.CurrentTemperature = device.currenttemperature / 100
  121. this.state.TargetTemperature = device.currentsetpoint / 100
  122. this.state.TemperatureDisplayUnits = this.platform.Characteristic.TemperatureDisplayUnits.CELSIUS
  123. this.service.updateCharacteristic(this.platform.Characteristic.CurrentHeatingCoolingState, this.state.CurrentHeatingCoolingState)
  124. this.service.updateCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState, this.state.TargetHeatingCoolingState)
  125. this.service.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.CurrentTemperature)
  126. this.service.updateCharacteristic(this.platform.Characteristic.TargetTemperature, this.state.TargetTemperature)
  127. this.service.updateCharacteristic(this.platform.Characteristic.TemperatureDisplayUnits, this.state.TemperatureDisplayUnits)
  128. break
  129. }
  130. }
  131. }
  132. })
  133. .catch((error) => {
  134. this.platform.log.debug('thermostatAccessory::update Error ->' + error)
  135. })
  136. }
  137. getCurrentHeatingCoolingState(): CharacteristicValue {
  138. // https://developers.homebridge.io/#/characteristic/CurrentHeatingCoolingState
  139. return this.state.CurrentHeatingCoolingState
  140. }
  141. getTargetHeatingCoolingState(): CharacteristicValue {
  142. // https://developers.homebridge.io/#/characteristic/TargetHeatingCoolingState
  143. return this.state.TargetHeatingCoolingState
  144. }
  145. setTargetHeatingCoolingState(value) {
  146. // https://developers.homebridge.io/#/characteristic/TargetHeatingCoolingState
  147. }
  148. getCurrentTemperature(): CharacteristicValue {
  149. // https://developers.homebridge.io/#/characteristic/CurrentTemperature
  150. return this.state.CurrentTemperature
  151. }
  152. getTargetTemperature(): CharacteristicValue {
  153. // https://developers.homebridge.io/#/characteristic/TargetTemperature
  154. return this.state.TargetTemperature
  155. }
  156. async setTargetTemperature(value) {
  157. // https://developers.homebridge.io/#/characteristic/TargetTemperature
  158. this.state.TargetTemperature = value
  159. // http://$domoticaIP/external/WebUpdate.php?data=Toon/$id/
  160. httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=Toon/' + this.accessory.context.device.typeId + '///' + (this.state.TargetTemperature * 100))
  161. .catch((error) => {
  162. this.platform.log.debug('thermostatAccessory::setOn Error ->' + error)
  163. })
  164. }
  165. getTemperatureDisplayUnits(): CharacteristicValue {
  166. // https://developers.homebridge.io/#/characteristic/TemperatureDisplayUnits
  167. return this.state.TemperatureDisplayUnits
  168. }
  169. setTemperatureDisplayUnits(value) {
  170. // https://developers.homebridge.io/#/characteristic/TemperatureDisplayUnits
  171. }
  172. }