Skip to content

Commit

Permalink
Get/Set temperature and mode
Browse files Browse the repository at this point in the history
  • Loading branch information
irinel-nistor-lego committed Jul 6, 2021
1 parent 7535f29 commit ec66826
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 7 deletions.
91 changes: 84 additions & 7 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -56,17 +82,32 @@ 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);
}

/**
* 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;
}
Expand All @@ -75,18 +116,43 @@ 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;
}

/**
* 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);
}

/**
Expand All @@ -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);
}
}
26 changes: 26 additions & 0 deletions src/samsungApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit ec66826

Please sign in to comment.