From ec668261b3182cb4b403e0fdceadc07a1d3cbb98 Mon Sep 17 00:00:00 2001 From: Irinel Nistor Date: Tue, 6 Jul 2021 14:57:32 +0300 Subject: [PATCH] Get/Set temperature and mode --- src/platformAccessory.ts | 91 ++++++++++++++++++++++++++++++++++++---- src/samsungApi.ts | 26 ++++++++++++ 2 files changed, 110 insertions(+), 7 deletions(-) diff --git a/src/platformAccessory.ts b/src/platformAccessory.ts index 055fa9e..1a6b464 100644 --- a/src/platformAccessory.ts +++ b/src/platformAccessory.ts @@ -11,6 +11,14 @@ export class SamsungACPlatformAccessory { Off: 'off', }; + private deviceMode = { + Cool: 'cool', + Heat: 'heat', + Dry: 'dry', + Fan: 'wind', + Auto: 'auto' + }; + constructor( private readonly platform: SamsungAC, private readonly accessory: PlatformAccessory, @@ -40,6 +48,24 @@ export class SamsungACPlatformAccessory { this.service.getCharacteristic(this.platform.Characteristic.CurrentTemperature) .onGet(this.handleCurrentTemperatureGet.bind(this)); + + this.service.getCharacteristic(this.platform.Characteristic.CoolingThresholdTemperature) + .setProps({ + minValue: 16, + maxValue: 30, + minStep: 1 + }) + .onSet(this.handleCoolingTemperatureSet.bind(this)) + .onGet(this.handleCoolingTemperatureGet.bind(this)); + + this.service.getCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature) + .setProps({ + minValue: 16, + maxValue: 30, + minStep: 1 + }) + .onSet(this.handleCoolingTemperatureSet.bind(this)) + .onGet(this.handleCoolingTemperatureGet.bind(this)); } /** @@ -56,7 +82,6 @@ export class SamsungACPlatformAccessory { * Handle requests to set the "Active" characteristic */ async handleActiveSet(value) { - console.log(value + " value"); let statusValue = value === 1 ? this.states.On : this.states.Off ; await SamsungAPI.setDeviceStatus(this.accessory.context.device.deviceId, statusValue, this.accessory.context.token); } @@ -64,9 +89,25 @@ export class SamsungACPlatformAccessory { /** * Handle requests to get the current value of the "Current Heater-Cooler State" characteristic */ - handleCurrentHeaterCoolerStateGet() { + async handleCurrentHeaterCoolerStateGet() { // set this to a valid value for CurrentHeaterCoolerState - const currentValue = this.platform.Characteristic.CurrentHeaterCoolerState.INACTIVE; + let currentValue = this.platform.Characteristic.CurrentHeaterCoolerState.IDLE; + let deviceMode = await SamsungAPI.getDeviceMode(this.accessory.context.device.deviceId, this.accessory.context.token); + switch (deviceMode){ + case this.deviceMode.Dry: + case this.deviceMode.Cool: { + currentValue = this.platform.Characteristic.CurrentHeaterCoolerState.COOLING; + break; + } + case this.deviceMode.Heat: { + currentValue = this.platform.Characteristic.CurrentHeaterCoolerState.HEATING; + break; + } + case this.deviceMode.Fan: { + currentValue = this.platform.Characteristic.CurrentHeaterCoolerState.INACTIVE; + break; + } + } return currentValue; } @@ -75,9 +116,21 @@ export class SamsungACPlatformAccessory { /** * Handle requests to get the current value of the "Target Heater-Cooler State" characteristic */ - handleTargetHeaterCoolerStateGet() { + async handleTargetHeaterCoolerStateGet() { // set this to a valid value for TargetHeaterCoolerState - const currentValue = this.platform.Characteristic.TargetHeaterCoolerState.AUTO; + let currentValue = this.platform.Characteristic.TargetHeaterCoolerState.AUTO; + let deviceMode = await SamsungAPI.getDeviceMode(this.accessory.context.device.deviceId, this.accessory.context.token); + switch (deviceMode){ + case this.deviceMode.Dry: + case this.deviceMode.Cool: { + currentValue = this.platform.Characteristic.TargetHeaterCoolerState.COOL; + break; + } + case this.deviceMode.Heat: { + currentValue = this.platform.Characteristic.TargetHeaterCoolerState.HEAT; + break; + } + } return currentValue; } @@ -85,8 +138,21 @@ export class SamsungACPlatformAccessory { /** * Handle requests to set the "Target Heater-Cooler State" characteristic */ - handleTargetHeaterCoolerStateSet(value) { - // do something + async handleTargetHeaterCoolerStateSet(value) { + console.log(value + " value"); + let modeValue = this.deviceMode.Auto; + switch (value){ + case this.platform.Characteristic.TargetHeaterCoolerState.COOL: { + modeValue = this.deviceMode.Cool; + break; + } + case this.platform.Characteristic.TargetHeaterCoolerState.HEAT: { + modeValue = this.deviceMode.Heat; + break; + } + } + + await SamsungAPI.setDeviceMode(this.accessory.context.device.deviceId, modeValue, this.accessory.context.token); } /** @@ -97,4 +163,15 @@ export class SamsungACPlatformAccessory { let temperature = await SamsungAPI.getDeviceTemperature(this.accessory.context.device.deviceId, this.accessory.context.token); return temperature; } + + async handleCoolingTemperatureGet() { + // get value for DesiredTemperature + let temperature = await SamsungAPI.getDesiredTemperature(this.accessory.context.device.deviceId, this.accessory.context.token); + return temperature; + } + + async handleCoolingTemperatureSet(temp) { + // set this to a valid value for DesiredTemperature + await SamsungAPI.setDesiredTemperature(this.accessory.context.device.deviceId, temp, this.accessory.context.token); + } } diff --git a/src/samsungApi.ts b/src/samsungApi.ts index 520d535..6dee3f8 100644 --- a/src/samsungApi.ts +++ b/src/samsungApi.ts @@ -30,4 +30,30 @@ export class SamsungAPI { const { data: { temperature = { } } = {} } = await Axios.get(`https://api.smartthings.com/v1/devices/${deviceId}/components/main/capabilities/temperatureMeasurement/status`, this.setToken(token)); return temperature.value; } + + static async getDeviceMode(deviceId, token) { + const { data: { airConditionerMode = { } } = {} } = await Axios.get(`https://api.smartthings.com/v1/devices/${deviceId}/components/main/capabilities/airConditionerMode/status`, this.setToken(token)); + return airConditionerMode.value; + } + + static async setDeviceMode(deviceId, mode, token) { + let data = { + "commands" : [{"capability": "airConditionerMode", "command": "setAirConditionerMode", "argument": [mode]}] + }; + + await Axios.post(`https://api.smartthings.com/v1/devices/${deviceId}/commands`, data, this.setToken(token)); + } + + static async getDesiredTemperature(deviceId, token) { + const { data: { coolingSetpoint = { } } = {} } = await Axios.get(`https://api.smartthings.com/v1/devices/${deviceId}/components/main/capabilities/thermostatCoolingSetpoint/status`, this.setToken(token)); + return coolingSetpoint.value; + } + + static async setDesiredTemperature(deviceId, temperature, token) { + let data = { + "commands" : [{"capability": "thermostatCoolingSetpoint", "command": "setCoolingSetpoint", "argument": [temperature]}] + }; + + await Axios.post(`https://api.smartthings.com/v1/devices/${deviceId}/commands`, data, this.setToken(token)); + } } \ No newline at end of file