Skip to content

Commit

Permalink
V 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanh94 committed Jul 23, 2021
1 parent c6ed786 commit 7553fe0
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 40 deletions.
31 changes: 0 additions & 31 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,6 @@
],
"required": true
},
"endPoint": {
"title": "API Region",
"type": "string",
"default": "form",
"oneOf": [{
"title": "America",
"enum": ["https://openapi.tuyaus.com"]
},
{
"title": "China",
"enum": ["https://openapi.tuyacn.com"]
},
{
"title": "Europe",
"enum": ["https://openapi.tuyaeu.com"]
},
{
"title": "India",
"enum": ["https://openapi.tuyain.com"]
},
{
"title": "EasternAmerica",
"enum": ["https://openapi-ueaz.tuyaus.com"]
},
{
"title": "WesternEurope",
"enum": ["https://openapi-weaz.tuyaeu.com"]
}
],
"required": true
},
"countryCode": {
"title": "Country Code",
"type": "number",
Expand Down
1 change: 0 additions & 1 deletion config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"accessId": "",
"accessKey": "",
"lang": "en",
"endPoint": "",
"projectType": "2",
"appSchema": "tuyaSmart",
"countryCode": 86,
Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const Fanv2Accessory = require('./lib/fanv2_accessory');
const HeaterAccessory = require('./lib/heater_accessory');
const GarageDoorAccessory = require('./lib/garagedoor_accessory');
const AirPurifierAccessory = require('./lib/air_purifier_accessory')
const WindowCoveringAccessory = require('./lib/window_covering_accessory')

const LogUtil = require('./util/logutil')
const DataUtil = require('./util/datautil')

Expand Down Expand Up @@ -72,7 +74,6 @@ class TuyaPlatform {
}
} else {
api = new TuyaSHOpenAPI(
config.options.endPoint,
config.options.accessId,
config.options.accessKey,
config.options.username,
Expand Down Expand Up @@ -123,6 +124,7 @@ class TuyaPlatform {
case 'fwd':
case 'tgq':
case 'xdd':
case 'dc':
deviceAccessory = new LightAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
Expand Down Expand Up @@ -162,6 +164,11 @@ class TuyaPlatform {
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'cl':
deviceAccessory = new WindowCoveringAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
default:
break;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/light_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ getAccessoryCharacteristic(name) {
case 'bright_value':
if (this.deviceCategorie == 'dj') {
defaultBrightRange = { 'min': 25, 'max': 255 }
}else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd' || this.deviceCategorie == 'tgq') {
}else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd' || this.deviceCategorie == 'tgq' || this.deviceCategorie == 'dd' || this.deviceCategorie == 'dc') {
defaultBrightRange = { 'min': 10, 'max': 1000 }
}
break;
Expand All @@ -200,7 +200,7 @@ getAccessoryCharacteristic(name) {
case 'temp_value':
if (this.deviceCategorie == 'dj') {
defaultTempRange = { 'min': 0, 'max': 255 }
}else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd') {
}else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd' || this.deviceCategorie == 'dd' || this.deviceCategorie == 'dc') {
defaultTempRange = { 'min': 0, 'max': 1000 }
}
break;
Expand All @@ -210,7 +210,7 @@ getAccessoryCharacteristic(name) {
case 'colour_data':
if (this.deviceCategorie == 'dj') {
defaultSaturationRange = { 'min': 0, 'max': 255 }
}else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd') {
}else if (this.deviceCategorie == 'xdd' || this.deviceCategorie == 'fwd' || this.deviceCategorie == 'dd' || this.deviceCategorie == 'dc') {
defaultSaturationRange = { 'min': 0, 'max': 1000 }
}
break;
Expand Down
9 changes: 5 additions & 4 deletions lib/tuyashopenapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const axios = require('axios').default;
const Crypto = require('crypto-js');
const uuid = require('uuid');
const nonce = uuid.v1();

const DEFAULT_ENDPOINT = 'https://openapi.tuyacn.com';
class TuyaSHOpenAPI {

constructor(endpoint, accessId, accessKey, username, password, countryCode, appSchema, log, lang = 'en') {
this.endpoint = endpoint;
constructor(accessId, accessKey, username, password, countryCode, appSchema, log, lang = 'en') {
this.endpoint = DEFAULT_ENDPOINT;
this.access_id = accessId;
this.access_key = accessKey;
this.lang = lang;
Expand Down Expand Up @@ -45,7 +45,8 @@ class TuyaSHOpenAPI {
'password': md5pwd,
'schema': this.appSchema
});
let { access_token, refresh_token, uid, expire_time } = res.result;
let { access_token, refresh_token, uid, expire_time , platform_url} = res.result;
this.endpoint = platform_url ? platform_url : DEFAULT_ENDPOINT;
this.tokenInfo = {
access_token: access_token,
refresh_token: refresh_token,
Expand Down
143 changes: 143 additions & 0 deletions lib/window_covering_accessory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
const BaseAccessory = require('./base_accessory')

let Accessory;
let Service;
let Characteristic;

class WindowCoveringAccessory extends BaseAccessory {

constructor(platform, homebridgeAccessory, deviceConfig) {
({ Accessory, Characteristic, Service } = platform.api.hap);
super(
platform,
homebridgeAccessory,
deviceConfig,
Accessory.Categories.WINDOW_COVERING,
Service.WindowCovering
);
this.isRefesh = false;
this.statusArr = deviceConfig.status;

this.refreshAccessoryServiceIfNeed(this.statusArr, false);
};

/**
* init Or refresh AccessoryService
*/
refreshAccessoryServiceIfNeed(stateArr, isRefesh) {
this.isRefesh = isRefesh;
for (const statusMap of stateArr) {
// Characteristic.TargetPosition
if ( statusMap.code === 'percent_control') {
this.percentControlMap = statusMap
this.normalAsync(Characteristic.TargetPosition, 0, isRefesh);
}

if ( statusMap.code === 'percent_state') {
// Characteristic.CurrentPosition
this.positionMap = statusMap
this.normalAsync(Characteristic.CurrentPosition, this.positionMap.value, isRefesh);

// Characteristic.PositionState
let hbValue = this.getHomeBridgeParam(Characteristic.PositionState, this.positionMap);
this.normalAsync(Characteristic.PositionState, hbValue, isRefesh);
}
}
}

/**
* add get/set Accessory service Characteristic Listner
*/
getAccessoryCharacteristic(name) {
//set Accessory service Characteristic
this.service.getCharacteristic(name)
.on('get', callback => {
if (this.hasValidCache()) {
callback(null, this.getCachedState(name));
}
})
.on('set', (hbValue, callback) => {
let tuyaParam = this.getTuyaParam(name, hbValue);
this.platform.tuyaOpenApi.sendCommand(this.deviceId, tuyaParam).then(() => {
//store homebridge value
this.setCachedState(name, hbValue);
//store targetPosition value
this.targetPosition = hbValue;
callback();
}).catch((error) => {
this.log.error('[SET][%s] Characteristic Error: %s', this.homebridgeAccessory.displayName, error);
this.invalidateCache();
callback(error);
});
});
}



/**
* get Tuya param from HomeBridge param
*/
getTuyaParam(name, hbParam) {
let code;
let value;
if (Characteristic.TargetPosition === name) {
code = this.percentControlMap.code;
value = hbParam;
}
return {
"commands": [
{
"code": code,
"value": value
}
]
};
}

/**
* get HomeBridge param from tuya param
*/
getHomeBridgeParam(name, tuyaParam) {
if (Characteristic.PositionState === name) {
if (this.targetPosition) {
if (this.targetPosition > tuyaParam.value) {
return Characteristic.PositionState.INCREASING;
}else if (this.targetPosition < tuyaParam.value) {
return Characteristic.PositionState.DECREASING;
}else{
return Characteristic.PositionState.STOPPED;
}
}else{
return Characteristic.PositionState.STOPPED;
}
}
}

/**
* update HomeBridge state
* @param {*} name HomeBridge Name
* @param {*} hbValue HomeBridge Value
* @param {*} isRefesh
*/
normalAsync(name, hbValue, isRefresh) {
//store homebridge value
this.setCachedState(name, hbValue);
if (isRefresh) {
this.service
.getCharacteristic(name)
.updateValue(hbValue);
} else {
this.getAccessoryCharacteristic(name);
}
}

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

}

module.exports = WindowCoveringAccessory;

0 comments on commit 7553fe0

Please sign in to comment.