| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge'
- import { accessory } from './accessory'
- import { device } from './device'
- import { httpRequest, response } from './httpPromise'
- import { domoticaPlatform } from './platform'
- export class thermostatAccessory extends accessory {
- private service: Service
- private state = {
- CurrentHeatingCoolingState: 0,
- TargetHeatingCoolingState: 0,
- CurrentTemperature: 0.0,
- TargetTemperature: 10.0,
- TemperatureDisplayUnits: 0
- }
- static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
- return new Promise((resolve, reject) => {
- let thermostatDevices: device[] = []
- httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/toonthermostat')
- .then((response) => {
- let devices = JSON.parse(response.body)
- for (const device of devices) {
- if (device.enabled == "true") {
- let object: device = {
- type: "Thermostat",
- typeId: device.id,
- uniqueId: "Thermostat." + device.id,
- deviceName: device.name,
- switchable: "false",
- dimmable: "false",
- detailedType: ""
- }
- thermostatDevices.push(object)
- }
- }
- resolve(thermostatDevices)
- })
- .catch((error) => {
- reject('thermostatAccessory::discoverDevices Error ->' + error)
- })
- })
- }
- constructor(
- private readonly platform: domoticaPlatform,
- private readonly accessory: PlatformAccessory,
- ) {
- // https://developers.homebridge.io/#/service/Thermostat
- super("Thermostat", accessory.context.device.typeId)
- this.accessory.getService(this.platform.Service.AccessoryInformation)!
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Thermostat')
- .setCharacteristic(this.platform.Characteristic.Model, 'Domotica-Thermostat')
- .setCharacteristic(this.platform.Characteristic.SerialNumber, '09000' + accessory.context.device.typeId)
- this.service = this.accessory.getService(this.platform.Service.Thermostat) || this.accessory.addService(this.platform.Service.Thermostat)
- this.service.getCharacteristic(this.platform.Characteristic.CurrentHeatingCoolingState)
- .onGet(this.getCurrentHeatingCoolingState.bind(this));
- this.service.getCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState)
- .onGet(this.getTargetHeatingCoolingState.bind(this));
- //.onSet(this.setTargetHeatingCoolingState.bind(this));
- this.service.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
- .onGet(this.getCurrentTemperature.bind(this));
- this.service.getCharacteristic(this.platform.Characteristic.TargetTemperature)
- .onGet(this.getTargetTemperature.bind(this))
- .onSet(this.setTargetTemperature.bind(this));
- this.service.getCharacteristic(this.platform.Characteristic.TemperatureDisplayUnits)
- .onGet(this.getTemperatureDisplayUnits.bind(this));
- //.onSet(this.setTemperatureDisplayUnits.bind(this));
-
- this.setName(accessory.context.device.deviceName)
- this.update()
- }
- setName(name) {
- this.service.setCharacteristic(this.platform.Characteristic.Name, name)
- }
- setValue(key, value) {
- if (key == "burnerState" && value != "") {
- if (value == 'Off') {
- this.state.CurrentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.OFF
- this.state.TargetHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.OFF
-
- } else {
- this.state.CurrentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.HEAT
- this.state.TargetHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.HEAT
- }
- this.service.updateCharacteristic(this.platform.Characteristic.CurrentHeatingCoolingState, this.state.CurrentHeatingCoolingState)
- this.service.updateCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState, this.state.TargetHeatingCoolingState)
- } else if (key == "currentSetPoint" && value != "") {
- this.state.TargetTemperature = value / 100
- this.service.updateCharacteristic(this.platform.Characteristic.TargetTemperature, this.state.TargetTemperature)
- } else if (key == "currentTemperature" && value != "") {
- this.state.CurrentTemperature = value / 100
- this.service.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.CurrentTemperature)
- }
- }
- update() {
- // curl -X POST "http://192.168.101.100:8080/API/Status" -d '[{"type": "toonthermostat", "query": "state"}]'
- // {"toonthermostat":
- // [{"burnerlevel":"low",
- // "burnerstate":"heater",
- // "currentsetpoint":"1800",
- // "currenttemperature":"1805",
- // "device_id":"113",
- // "id":"1",
- // "name":"Toon",
- // "programstate":"off",
- // "temperaturestate":"Home"}]}
- const data = JSON.stringify([{
- type: 'toonthermostat',
- query: 'state'
- }])
- httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
- .then((response) => {
- const devices = JSON.parse(response.body)
- if (devices.hasOwnProperty('toonthermostat')) {
- for (const device of devices.toonthermostat) {
- if (device.id == this.accessory.context.device.typeId) {
- if (device.burnerstate == 'off') {
- this.state.CurrentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.OFF
- this.state.TargetHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.OFF
- } else {
- this.state.CurrentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.HEAT
- this.state.TargetHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.HEAT
- }
- this.state.CurrentTemperature = device.currenttemperature / 100
- this.state.TargetTemperature = device.currentsetpoint / 100
- this.state.TemperatureDisplayUnits = this.platform.Characteristic.TemperatureDisplayUnits.CELSIUS
- this.service.updateCharacteristic(this.platform.Characteristic.CurrentHeatingCoolingState, this.state.CurrentHeatingCoolingState)
- this.service.updateCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState, this.state.TargetHeatingCoolingState)
- this.service.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.CurrentTemperature)
- this.service.updateCharacteristic(this.platform.Characteristic.TargetTemperature, this.state.TargetTemperature)
- this.service.updateCharacteristic(this.platform.Characteristic.TemperatureDisplayUnits, this.state.TemperatureDisplayUnits)
-
- break
- }
- }
- }
- })
- .catch((error) => {
- this.platform.log.debug('thermostatAccessory::update Error ->' + error)
- })
- }
- getCurrentHeatingCoolingState(): CharacteristicValue {
- // https://developers.homebridge.io/#/characteristic/CurrentHeatingCoolingState
- return this.state.CurrentHeatingCoolingState
- }
- getTargetHeatingCoolingState(): CharacteristicValue {
- // https://developers.homebridge.io/#/characteristic/TargetHeatingCoolingState
- return this.state.TargetHeatingCoolingState
- }
- setTargetHeatingCoolingState(value) {
- // https://developers.homebridge.io/#/characteristic/TargetHeatingCoolingState
- }
- getCurrentTemperature(): CharacteristicValue {
- // https://developers.homebridge.io/#/characteristic/CurrentTemperature
- return this.state.CurrentTemperature
- }
- getTargetTemperature(): CharacteristicValue {
- // https://developers.homebridge.io/#/characteristic/TargetTemperature
- return this.state.TargetTemperature
- }
- async setTargetTemperature(value) {
- // https://developers.homebridge.io/#/characteristic/TargetTemperature
- this.state.TargetTemperature = value
- // http://$domoticaIP/external/WebUpdate.php?data=Toon/$id/
- httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=Toon/' + this.accessory.context.device.typeId + '///' + (this.state.TargetTemperature * 100))
- .catch((error) => {
- this.platform.log.debug('thermostatAccessory::setOn Error ->' + error)
- })
- }
- getTemperatureDisplayUnits(): CharacteristicValue {
- // https://developers.homebridge.io/#/characteristic/TemperatureDisplayUnits
- return this.state.TemperatureDisplayUnits
- }
- setTemperatureDisplayUnits(value) {
- // https://developers.homebridge.io/#/characteristic/TemperatureDisplayUnits
- }
- }
|