More RGBTW tweaks

This commit is contained in:
tsightler
2020-10-05 02:05:34 -04:00
parent 3705efaaed
commit 4ff6fc221e
2 changed files with 33 additions and 19 deletions

View File

@@ -26,7 +26,7 @@ class RGBTWLight extends TuyaDevice {
white_value_state: { white_value_state: {
key: this.config.dpsWhiteValue, key: this.config.dpsWhiteValue,
type: 'int', type: 'int',
min: (this.config.whiteValueScale = 1000) ? 10 : 1, min: (this.config.whiteValueScale == 1000) ? 10 : 1,
max: this.config.whiteValueScale, max: this.config.whiteValueScale,
scale: this.config.whiteValueScale scale: this.config.whiteValueScale
}, },
@@ -40,6 +40,11 @@ class RGBTWLight extends TuyaDevice {
type: this.config.colorType, type: this.config.colorType,
components: 'b' components: 'b'
}, },
hsb_state: {
key: this.config.dpsColor,
type: this.config.colorType,
components: 'h,s,b'
},
mode_state: { mode_state: {
key: this.config.dpsMode, key: this.config.dpsMode,
type: 'str' type: 'str'

View File

@@ -211,7 +211,7 @@ class TuyaDevice {
const deviceTopic = this.deviceTopics.hasOwnProperty(stateTopic) ? this.deviceTopics[stateTopic] : '' const deviceTopic = this.deviceTopics.hasOwnProperty(stateTopic) ? this.deviceTopics[stateTopic] : ''
if (deviceTopic) { if (deviceTopic) {
debug('Device '+this.options.id+' recieved command topic: '+commandTopic+', message: '+message) debug('Device '+this.options.id+' received command topic: '+commandTopic+', message: '+message)
const command = this.getCommandFromMessage(message) const command = this.getCommandFromMessage(message)
let setResult = this.setTuyaState(command, deviceTopic) let setResult = this.setTuyaState(command, deviceTopic)
if (!setResult) { if (!setResult) {
@@ -324,12 +324,12 @@ class TuyaDevice {
tuyaCommand.set = '!!!INVALID!!!' tuyaCommand.set = '!!!INVALID!!!'
} else if (deviceTopic.hasOwnProperty('min') && deviceTopic.hasOwnProperty('max')) { } else if (deviceTopic.hasOwnProperty('min') && deviceTopic.hasOwnProperty('max')) {
if (command >= deviceTopic.min && command <= deviceTopic.max ) { if (command >= deviceTopic.min && command <= deviceTopic.max ) {
tuyaCommand.set = deviceTopic.type = 'int' ? parseInt(command) : parseFloat(command) tuyaCommand.set = deviceTopic.type === 'int' ? parseInt(command) : parseFloat(command)
} else { } else {
tuyaCommand.set = '!!!INVALID!!!' tuyaCommand.set = '!!!INVALID!!!'
} }
} else { } else {
tuyaCommand.set = deviceTopic.type = 'int' ? parseInt(command) : parseFloat(command) tuyaCommand.set = deviceTopic.type === 'int' ? parseInt(command) : parseFloat(command)
} }
break; break;
case 'hsb': case 'hsb':
@@ -373,7 +373,11 @@ class TuyaDevice {
// Initialize the set color values for first time. Used to conflicts // Initialize the set color values for first time. Used to conflicts
// when mulitple HSB components are updated in quick succession // when mulitple HSB components are updated in quick succession
if (!this.state.setColor) { if (!this.state.setColor) {
this.state.setColor = this.state.color this.state.setColor = {
'h': this.state.color.h,
's': this.state.color.s,
'b': this.state.color.b
}
} }
} }
@@ -433,28 +437,33 @@ class TuyaDevice {
// Set white/colour mode based on target mode // Set white/colour mode based on target mode
async setLight(topic, command) { async setLight(topic, command) {
const currentMode = this.state.dps[this.config.dpsMode].val
let targetMode = undefined let targetMode = undefined
if (this.config.dpsWhiteValue === topic.key) { if (topic.key === this.config.dpsWhiteValue) {
// If setting white level, light should be in white mode // If setting white level, light should be in white mode
targetMode = 'white' targetMode = 'white'
} else if (this.config.dpsColor === topic.key) { } else if (topic.key === this.config.dpsColor) {
if (this.state.setColor.s > 0) { if (this.state.setColor.s === 0 && this.state.setColor.s !== this.state.color.s) {
// If setting an HSB value with saturation > 0, light should be in color mode // If setting saturation to 0 and not already zero, target mode is 'white'
targetMode = 'colour'
} else {
// If setting an HSB value but saturation is 0, put light in white mode
targetMode = 'white' targetMode = 'white'
} else if ((this.state.setColor.s > 0 && this.state.setColor.s !== this.state.color.s) ||
this.state.setColor.h !== this.state.color.h ||
this.state.setColor.b !== this.state.color.b) {
// If setting saturation > 0, or changing any other color value, target mode is 'colour'
targetMode = 'colour'
} }
} }
// If mode change required, add it to the set command
// Set the correct light mode if (targetMode && currentMode !== targetMode) {
if (targetMode && targetMode !== this.state.dps[this.config.dpsMode].val) { command = {
const modeCommand = { multiple: true,
dps: this.config.dpsMode, data: {
set: targetMode [command.dps]: command.set,
[this.config.dpsMode]: targetMode
} }
await this.set(modeCommand)
} }
}
console.log(command)
this.set(command) this.set(command)
} }