Skip to content

Commit

Permalink
1.Optimize the code.
Browse files Browse the repository at this point in the history
2.Added "mcs" category
  • Loading branch information
Hanh94 committed Aug 10, 2021
1 parent 07ee82e commit ef2c303
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 101 deletions.
16 changes: 8 additions & 8 deletions lib/air_purifier_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AirPurifierAccessory extends BaseAccessory {
Accessory.Categories.AIR_PURIFIER,
Service.AirPurifier
);
this.isRefresh = false;

this.statusArr = deviceConfig.status ? deviceConfig.status : [];
this.functionArr = deviceConfig.functions ? deviceConfig.functions : [];

Expand All @@ -38,33 +38,33 @@ class AirPurifierAccessory extends BaseAccessory {
for (var statusMap of statusArr) {
if (statusMap.code === 'switch') {
this.switchMap = statusMap;
this.normalAsync(Characteristic.Active, this.switchMap.value, this.isRefresh)
this.normalAsync(Characteristic.CurrentAirPurifierState, this.switchMap.value ? 2 : 0, this.isRefresh)
this.normalAsync(Characteristic.Active, this.switchMap.value)
this.normalAsync(Characteristic.CurrentAirPurifierState, this.switchMap.value ? 2 : 0)
}

if (statusMap.code === "mode") {
this.modeMap = statusMap;
const hbAirPurifierValue = this.tuyaParamToHomeBridge(Characteristic.TargetAirPurifierState, this.modeMap.value)
this.normalAsync(Characteristic.TargetAirPurifierState, hbAirPurifierValue, this.isRefresh)
this.normalAsync(Characteristic.TargetAirPurifierState, hbAirPurifierValue)
}

if (statusMap.code === 'lock') {
this.lockMap = statusMap;
this.normalAsync(Characteristic.LockPhysicalControls, this.lockMap.value ? 0 : 1, this.isRefresh)
this.normalAsync(Characteristic.LockPhysicalControls, this.lockMap.value ? 0 : 1)
}

if (statusMap.code === 'speed' || statusMap.code === 'fan_speed_enum') {
this.speedMap = statusMap;
const hbSpeed = this.tuyaParamToHomeBridge(Characteristic.RotationSpeed, this.speedMap.value);
this.normalAsync(Characteristic.RotationSpeed, hbSpeed, this.isRefresh)
this.normalAsync(Characteristic.RotationSpeed, hbSpeed)
}
}
}


normalAsync(name, hbValue, isRefresh) {
normalAsync(name, hbValue) {
this.setCachedState(name, hbValue);
if (isRefresh) {
if (this.isRefresh) {
this.service
.getCharacteristic(name)
.updateValue(hbValue);
Expand Down
65 changes: 34 additions & 31 deletions lib/contactsensor_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,60 @@ class ContactSensorAccessory extends BaseAccessory {
Accessory.Categories.SENSOR,
Service.ContactSensor
);
this.isRefresh = false;
this.statusArr = deviceConfig.status;

this.statusArr = deviceConfig.status;
this.refreshAccessoryServiceIfNeed(this.statusArr, false);
}

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

for (var statusMap of statusArr) {
if (statusMap.code === 'doorcontact_state') {
this.sensorStatus = statusMap
var rawStatus = this.sensorStatus.value
let status

if (rawStatus === true) {
status = Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
} else {
status = Characteristic.ContactSensorState.CONTACT_DETECTED
}
this.setCachedState(Characteristic.ContactSensorState, status);
if (this.isRefresh) {
this.service
.getCharacteristic(Characteristic.ContactSensorState)
.updateValue(status);
} else {
this.getAccessoryCharacteristic(Characteristic.ContactSensorState);
}
const hbSensorState = this.tuyaParamToHomeBridge(Characteristic.ContactSensorState, this.sensorStatus.value);
this.normalAsync(Characteristic.ContactSensorState, hbSensorState)
}

if (statusMap.code === 'battery_percentage') {
this.batteryStatus = statusMap
var rawStatus = this.batteryStatus.value
let status
const hbBatteryStatus = this.tuyaParamToHomeBridge(Characteristic.StatusLowBattery, this.batteryStatus.value);
this.normalAsync(Characteristic.StatusLowBattery, hbBatteryStatus)
}
}
}

if (rawStatus >= 20) {
status = '0';
tuyaParamToHomeBridge(name, param) {
switch (name) {
case Characteristic.ContactSensorState:
let status
if (param === true) {
status = Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
} else {
status = '1';
status = Characteristic.ContactSensorState.CONTACT_DETECTED
}
return status

this.setCachedState(Characteristic.StatusLowBattery, status);
if (this.isRefresh) {
this.service
.getCharacteristic(Characteristic.StatusLowBattery)
.updateValue(status);
case Characteristic.StatusLowBattery:
let value
if (param >= 20) {
value = Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
} else {
this.getAccessoryCharacteristic(Characteristic.StatusLowBattery);
value = Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
}
}
return value
}
}


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

Expand Down
23 changes: 11 additions & 12 deletions lib/fanv2_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Fanv2Accessory extends BaseAccessory {
Accessory.Categories.FAN,
Service.Fanv2
);
this.isRefresh = false;
this.statusArr = deviceConfig.status ? deviceConfig.status : [];
this.functionArr = deviceConfig.functions ? deviceConfig.functions : [];

Expand All @@ -31,32 +30,32 @@ class Fanv2Accessory extends BaseAccessory {
if (statusMap.code === 'switch' || statusMap.code === 'fan_switch' || statusMap.code === 'switch_fan') {
this.switchMap = statusMap
const hbSwitch = this.tuyaParamToHomeBridge(Characteristic.Active, this.switchMap.value);
this.normalAsync(Characteristic.Active, hbSwitch, this.isRefresh)
this.normalAsync(Characteristic.Active, hbSwitch)
}
if (statusMap.code === 'mode') {
this.modeMap = statusMap
const hbFanState = this.tuyaParamToHomeBridge(Characteristic.TargetFanState, this.modeMap.value);
this.normalAsync(Characteristic.TargetFanState, hbFanState, this.isRefresh)
this.normalAsync(Characteristic.TargetFanState, hbFanState)

}
if (statusMap.code === 'child_lock') {
this.lockMap = statusMap
const hbLock = this.tuyaParamToHomeBridge(Characteristic.LockPhysicalControls, this.lockMap.value);
this.normalAsync(Characteristic.LockPhysicalControls, hbLock, this.isRefresh)
this.normalAsync(Characteristic.LockPhysicalControls, hbLock)
}

if (statusMap.code === 'fan_direction') {
this.directionMap = statusMap
const hbDirection = this.tuyaParamToHomeBridge(Characteristic.RotationDirection, this.directionMap.value);
this.normalAsync(Characteristic.RotationDirection, hbDirection, this.isRefresh)
this.normalAsync(Characteristic.RotationDirection, hbDirection)
}

if (statusMap.code === 'fan_speed_percent') {
this.speedMap = statusMap
this.speed_range = this.getSpeedFunctionRange(this.speedMap.code)
const rawValue = this.speedMap.value // 1~12
const value = Math.floor((rawValue * 100 - 100 * this.speed_range.min) / (this.speed_range.max - this.speed_range.min)); // 0-100
this.normalAsync(Characteristic.RotationSpeed, value, this.isRefresh)
this.normalAsync(Characteristic.RotationSpeed, value)
}

if (statusMap.code === 'fan_speed') {
Expand All @@ -66,26 +65,26 @@ class Fanv2Accessory extends BaseAccessory {
this.speed_count = this.getSpeedFunctionLevel(this.speedMap.code)
this.speed_coefficient = 100 / this.speed_count
const hbSpeed = parseInt(this.speedMap.value * this.speed_coefficient);
this.normalAsync(Characteristic.RotationSpeed, hbSpeed, this.isRefresh)
this.normalAsync(Characteristic.RotationSpeed, hbSpeed)
}else{
this.speed_range = this.getSpeedFunctionRange(this.speedMap.code)
const rawValue = this.speedMap.value // 1~12
const value = Math.floor((rawValue * 100 - 100 * this.speed_range.min) / (this.speed_range.max - this.speed_range.min)); // 0-100
this.normalAsync(Characteristic.RotationSpeed, value, this.isRefresh)
this.normalAsync(Characteristic.RotationSpeed, value)
}
}

if (statusMap.code === 'switch_vertical') {
this.swingMap = statusMap
const hbSwing = this.tuyaParamToHomeBridge(Characteristic.SwingMode, this.swingMap.value);
this.normalAsync(Characteristic.SwingMode, hbSwing, this.isRefresh)
this.normalAsync(Characteristic.SwingMode, hbSwing)
}
}
}

normalAsync(name, hbValue, isRefresh) {
normalAsync(name, hbValue) {
this.setCachedState(name, hbValue);
if (isRefresh) {
if (this.isRefresh) {
this.service
.getCharacteristic(name)
.updateValue(hbValue);
Expand Down Expand Up @@ -227,7 +226,7 @@ class Fanv2Accessory extends BaseAccessory {
if (funcDic) {
let value = JSON.parse(funcDic.values)
let isnull = (JSON.stringify(value) == "{}")
return isnull ? DEFAULT_SPEED_COUNT : value.range.length;
return isnull || !value.range ? DEFAULT_SPEED_COUNT : value.range.length;
} else {
return DEFAULT_SPEED_COUNT;
}
Expand Down
7 changes: 3 additions & 4 deletions lib/garagedoor_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ class GarageDoorAccessory extends BaseAccessory {
Accessory.Categories.GARAGE_DOOR_OPENER,
Service.GarageDoorOpener
);
this.isRefesh = false;
this.statusArr = deviceConfig.status;

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

//init Or refresh AccessoryService
refreshAccessoryServiceIfNeed(statusArr, isRefesh) {
this.isRefesh = isRefesh;
refreshAccessoryServiceIfNeed(statusArr, isRefresh) {
this.isRefresh = isRefresh;
for (var statusMap of statusArr) {
var rawStatus = statusMap.value
let characteristicName
Expand Down Expand Up @@ -61,7 +60,7 @@ class GarageDoorAccessory extends BaseAccessory {

initAccessoryCharacteristic(characteristicName,value){
this.setCachedState(characteristicName, value);
if (this.isRefesh) {
if (this.isRefresh) {
this.service
.getCharacteristic(characteristicName)
.updateValue(value);
Expand Down
23 changes: 11 additions & 12 deletions lib/heater_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class HeaterAccessory extends BaseAccessory {
Accessory.Categories.AIR_HEATER,
Service.HeaterCooler
);
this.isRefresh = false;
this.statusArr = deviceConfig.status;
this.functionArr = deviceConfig.functions ? deviceConfig.functions : [];

Expand All @@ -38,24 +37,24 @@ class HeaterAccessory extends BaseAccessory {
if (statusMap.code === 'switch') {
this.switchMap = statusMap
const hbSwitch = this.tuyaParamToHomeBridge(Characteristic.Active, this.switchMap);
this.normalAsync(Characteristic.Active, hbSwitch, this.isRefresh)
this.normalAsync(Characteristic.CurrentHeaterCoolerState, 2, this.isRefresh)
this.normalAsync(Characteristic.TargetHeaterCoolerState, 1, this.isRefresh, {
this.normalAsync(Characteristic.Active, hbSwitch)
this.normalAsync(Characteristic.CurrentHeaterCoolerState, 2)
this.normalAsync(Characteristic.TargetHeaterCoolerState, 1, {
minValue: 1,
maxValue: 1,
validValues: [Characteristic.TargetHeaterCoolerState.HEAT]
})
}
if (statusMap.code === 'temp_current' || statusMap.code === 'temp_current_f') {
this.temperatureMap = statusMap
this.normalAsync(Characteristic.CurrentTemperature, this.temperatureMap.value, this.isRefresh, {
this.normalAsync(Characteristic.CurrentTemperature, this.temperatureMap.value, {
minValue: -20,
maxValue: 122,
minStep: 1
})

const hbUnits = this.tuyaParamToHomeBridge(Characteristic.TemperatureDisplayUnits, this.temperatureMap);
this.normalAsync(Characteristic.TemperatureDisplayUnits, hbUnits, this.isRefresh, {
this.normalAsync(Characteristic.TemperatureDisplayUnits, hbUnits, {
minValue: hbUnits,
maxValue: hbUnits,
validValues: [hbUnits]
Expand All @@ -64,17 +63,17 @@ class HeaterAccessory extends BaseAccessory {
if (statusMap.code === 'lock') {
this.lockMap = statusMap
const hbLock = this.tuyaParamToHomeBridge(Characteristic.LockPhysicalControls, this.lockMap);
this.normalAsync(Characteristic.LockPhysicalControls, hbLock, this.isRefresh)
this.normalAsync(Characteristic.LockPhysicalControls, hbLock)
}
if (statusMap.code === 'level') {
this.speedMap = statusMap;
const hbSpeed = this.tuyaParamToHomeBridge(Characteristic.RotationSpeed, this.speedMap);
this.normalAsync(Characteristic.RotationSpeed, hbSpeed, this.isRefresh)
this.normalAsync(Characteristic.RotationSpeed, hbSpeed)
}
if (statusMap.code === 'shake') {
this.shakeMap = statusMap
const hbShake = this.tuyaParamToHomeBridge(Characteristic.SwingMode, this.shakeMap);
this.normalAsync(Characteristic.SwingMode, hbShake, this.isRefresh)
this.normalAsync(Characteristic.SwingMode, hbShake)
}
if (statusMap.code === 'temp_set' || statusMap.code === 'temp_set_f') {
this.tempsetMap = statusMap
Expand All @@ -86,7 +85,7 @@ class HeaterAccessory extends BaseAccessory {
this.temp_set_range = { 'min': 32, 'max': 104 }
}
}
this.normalAsync(Characteristic.HeatingThresholdTemperature, this.tempsetMap.value, this.isRefresh, {
this.normalAsync(Characteristic.HeatingThresholdTemperature, this.tempsetMap.value, {
minValue: this.temp_set_range.min,
maxValue: this.temp_set_range.max,
minStep: 1
Expand All @@ -95,9 +94,9 @@ class HeaterAccessory extends BaseAccessory {
}
}

normalAsync(name, hbValue, isRefresh, props) {
normalAsync(name, hbValue, props) {
this.setCachedState(name, hbValue);
if (isRefresh) {
if (this.isRefresh) {
this.service
.getCharacteristic(name)
.updateValue(hbValue);
Expand Down
Loading

0 comments on commit ef2c303

Please sign in to comment.