Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support smart ir remote AC devices #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const TuyaOpenAPI = require("./lib/tuyaopenapi");
const TuyaSHOpenAPI = require("./lib/tuyashopenapi");
const TuyaOpenMQ = require("./lib/tuyamqttapi");
const TuyaInfraredsAPI = require("./lib/tuyainfraredsapi");
const OutletAccessory = require('./lib/outlet_accessory');
const LightAccessory = require('./lib/light_accessory');
const SwitchAccessory = require('./lib/switch_accessory');
const SmokeSensorAccessory = require('./lib/smokesensor_accessory');
const Fanv2Accessory = require('./lib/fanv2_accessory');
const HeaterAccessory = require('./lib/heater_accessory');
const IRAirConditionerAccessory = require('./lib/irremote_air_conditioner_accessory');
const GarageDoorAccessory = require('./lib/garagedoor_accessory');
const AirPurifierAccessory = require('./lib/air_purifier_accessory')
const WindowCoveringAccessory = require('./lib/window_covering_accessory')
Expand Down Expand Up @@ -94,6 +96,7 @@ class TuyaPlatform {
return;
}
}
this.tuyaIRAPI = new TuyaInfraredsAPI(api);

for (const device of devices) {
this.addAccessory(device);
Expand All @@ -104,6 +107,7 @@ class TuyaPlatform {
this.tuyaOpenMQ = mq;
this.tuyaOpenMQ.start();
this.tuyaOpenMQ.addMessageListener(this.onMQTTMessage.bind(this));

}

addAccessory(device) {
Expand Down Expand Up @@ -183,6 +187,38 @@ class TuyaPlatform {
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
// <-- IR start
case 'wnykq':
Promise.all([this.tuyaIRAPI.getCategories(device.id),
this.tuyaIRAPI.getRemotes(device.id)])
.then((resp) => {
for(const remote of resp[1]) {
this.tuyaIRAPI.getACStatus(device.id, remote.remote_id).then((status) => {
let category = resp[0].find((e) => e.category_id == 5);
switch (category.category_name) {
case 'Air Conditioner':
let accessory = new IRAirConditionerAccessory(this, homebridgeAccessory, {
id: device.id,
remote_id: remote.remote_id,
name: remote.remote_name,
index: remote.remote_index,
category: remote.category_id,
brand: remote.brand_name,
status: status
});
this.accessories.set(uuid, accessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, accessory);
break;
default:
break;
}
});
}
}).catch((error) => {
this.log.error('[GET][%s] Remote Error: %s', device.id, error);
});
break;
// IR end -->
default:
break;
}
Expand Down
170 changes: 170 additions & 0 deletions lib/irremote_air_conditioner_accessory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
const BaseAccessory = require('./base_accessory')

let Accessory;
let Service;
let Characteristic;
let UUIDGen;

class IRAirConditionerAccessory extends BaseAccessory {
constructor(platform, homebridgeAccessory, deviceConfig) {

({ Accessory, Characteristic, Service } = platform.api.hap);
super(
platform,
homebridgeAccessory,
deviceConfig,
Accessory.Categories.AIR_CONDITIONER,
Service.HeaterCooler
);

this.statusArr = deviceConfig.status ? deviceConfig.status : [];
this.refreshAccessoryServiceIfNeed(this.statusArr, false);
}

//init Or refresh AccessoryService
refreshAccessoryServiceIfNeed(statusArr, isRefresh) {
this.isRefresh = isRefresh;

// power
this.normalAsync(Characteristic.Active, statusArr.power)

// temperatures
this.normalAsync(Characteristic.CurrentTemperature, statusArr.temp, {
minValue: -30,
maxValue: 100,
minStep: 1
})
this.normalAsync(Characteristic.TemperatureDisplayUnits, 0, {
minValue: 0,
maxValue: 0,
validValues: [0]
})
this.normalAsync(Characteristic.CoolingThresholdTemperature, statusArr.temp, {
minValue: 16,
maxValue: 32,
minStep: 1
})
this.normalAsync(Characteristic.HeatingThresholdTemperature, statusArr.temp, {
minValue: 16,
maxValue: 32,
minStep: 1
})

// mode
this.normalAsync(Characteristic.CurrentHeaterCoolerState, this._convertTuyaToHomebridgeMode(statusArr.mode), {
minValue: 0,
maxValue: 3,
validValues: [0, 1, 2, 3]
})
this.normalAsync(Characteristic.TargetHeaterCoolerState, this._convertTuyaToHomebridgeMode(statusArr.mode), {
minValue: 0,
maxValue: 2,
validValues: [0, 1, 2]
})
}

normalAsync(name, hbValue, props) {
this.setCachedState(name, hbValue);
if (this.isRefresh) {
this.service
.getCharacteristic(name)
.updateValue(hbValue);
} else {
this.getAccessoryCharacteristic(name, props);
}
}

getAccessoryCharacteristic(name, props) {
//set Accessory service Characteristic
this.service.getCharacteristic(name)
.setProps(props || {})
.on('get', callback => {
if (this.hasValidCache()) {
callback(null, this.getCachedState(name));
}
})
.on('set', (value, callback) => {
if (name == Characteristic.CurrentHeaterCoolerState ||
name == Characteristic.CurrentTemperature ||
name == Characteristic.TemperatureDisplayUnits) {
callback();
return;
}

var param = this.getSendParam(name, value)
this.platform.tuyaIRAPI.setACStatus(this.deviceConfig.id, this.deviceConfig.remote_id, param).then(() => {
this.setCachedState(name, value);
callback();
}).catch((error) => {
this.log.error('[SET][%s] Characteristic Error: %s', this.homebridgeAccessory.displayName, error);
this.invalidateCache();
callback(error);
});
});
}

//get Command SendData
getSendParam(name, value) {
var body = Object.assign({}, this.statusArr);
switch (name) {
case Characteristic.Active:
body.power = value;
break;
case Characteristic.TargetHeaterCoolerState:
body.power = 1;
body.mode = this._convertHomebridgeToTuyaMode(value);
break;
case Characteristic.CoolingThresholdTemperature:
case Characteristic.HeatingThresholdTemperature:
body.power = 1;
body.temp = value;
break;
case Characteristic.SwingMode:
body.power = 1;
body.swing = value;
break;
case Characteristic.RotationSpeed:
body.power = 1;
body.wind = Math.floor((value / 100) * 3);
break;
default:
break;
}
return {
"remote_index": this.deviceConfig.index,
"category_id": this.deviceConfig.category,
"power": body.power,
"mode": body.mode,
"temp": body.temp,
"swing": body.swing,
"wind": body.wind
};
}

_convertHomebridgeToTuyaMode(homebridgeState) {
if (homebridgeState === 2) {
return 0; // cooling
}
if (homebridgeState === 1) {
return 1; // heating
}
return 2; // automatic
}

_convertTuyaToHomebridgeMode(tuyaState) {
if (tuyaState === 0) {
return 2; // cooling
}
if (tuyaState === 1) {
return 1; // heating
}
return 0; // automatic
}

//update device status
updateState(data) {
this.refreshAccessoryServiceIfNeed(data.status, true);
}
}

module.exports = IRAirConditionerAccessory;
33 changes: 33 additions & 0 deletions lib/tuyainfraredsapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class TuyaInfraredsAPI {

constructor(tuyaOpenApi) {
this.tuyaOpenApi = tuyaOpenApi;
}

// get IR device categories
async getCategories(deviceID) {
let res = await this.tuyaOpenApi.get(`/v2.0/infrareds/${deviceID}/categories`);
return res.result;
}

// get remotes configured in IR device
async getRemotes(infraredID) {
let res = await this.tuyaOpenApi.get(`/v2.0/infrareds/${infraredID}/remotes`);
return res.result;
}

// get AC status
async getACStatus(infraredID, remoteID) {
let res = await this.tuyaOpenApi.get(`/v2.0/infrareds/${infraredID}/remotes/${remoteID}/ac/status`);
return res.result;
}

// set AC status
async setACStatus(infraredID, remoteID, params) {
let res = await this.tuyaOpenApi.post(`/v2.0/infrareds/${infraredID}/air-conditioners/${remoteID}/scenes/command`, params);
return res.result;
}

}

module.exports = TuyaInfraredsAPI;