mirror of
https://github.com/lehanspb/tuya-mqtt.git
synced 2025-12-18 16:17:30 +00:00
More RGB fixes
This commit is contained in:
@@ -119,11 +119,13 @@ class TuyaDevice {
|
|||||||
for (let topic in this.deviceTopics) {
|
for (let topic in this.deviceTopics) {
|
||||||
const deviceTopic = this.deviceTopics[topic]
|
const deviceTopic = this.deviceTopics[topic]
|
||||||
const key = deviceTopic.key
|
const key = deviceTopic.key
|
||||||
if (this.state.dps[key].updated) {
|
if (this.state.dps[key] && this.state.dps[key].updated) {
|
||||||
const state = this.getTopicState(deviceTopic, this.state.dps[key].val)
|
const state = this.getTopicState(deviceTopic, this.state.dps[key].val)
|
||||||
|
if (state) {
|
||||||
this.publishMqtt(this.baseTopic + topic, state, true)
|
this.publishMqtt(this.baseTopic + topic, state, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Publish Generic Dps Topics
|
// Publish Generic Dps Topics
|
||||||
this.publishDpsTopics()
|
this.publishDpsTopics()
|
||||||
@@ -170,9 +172,11 @@ class TuyaDevice {
|
|||||||
state = value ? 'ON' : 'OFF'
|
state = value ? 'ON' : 'OFF'
|
||||||
break;
|
break;
|
||||||
case 'int':
|
case 'int':
|
||||||
state = value ? value.toString() : 'None'
|
case 'float':
|
||||||
|
state = value ? value.toString() : ''
|
||||||
break;
|
break;
|
||||||
case 'hsb':
|
case 'hsb':
|
||||||
|
case 'hsbhex':
|
||||||
// Return comma separate array of component values for specific topic
|
// Return comma separate array of component values for specific topic
|
||||||
state = new Array()
|
state = new Array()
|
||||||
const components = deviceTopic.components.split(',')
|
const components = deviceTopic.components.split(',')
|
||||||
@@ -209,7 +213,7 @@ class TuyaDevice {
|
|||||||
if (deviceTopic) {
|
if (deviceTopic) {
|
||||||
debug('Device '+this.options.id+' recieved command topic: '+commandTopic+', message: '+message)
|
debug('Device '+this.options.id+' recieved command topic: '+commandTopic+', message: '+message)
|
||||||
const command = this.getCommandFromMessage(message)
|
const command = this.getCommandFromMessage(message)
|
||||||
let setResult = this.setState(command, deviceTopic)
|
let setResult = this.setTuyaState(command, deviceTopic)
|
||||||
if (!setResult) {
|
if (!setResult) {
|
||||||
debug('Command topic '+this.baseTopic+commandTopic+' received invalid value: '+command)
|
debug('Command topic '+this.baseTopic+commandTopic+' received invalid value: '+command)
|
||||||
}
|
}
|
||||||
@@ -299,7 +303,7 @@ class TuyaDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set state based on command topic
|
// Set state based on command topic
|
||||||
setState(command, deviceTopic) {
|
setTuyaState(command, deviceTopic) {
|
||||||
const tuyaCommand = new Object()
|
const tuyaCommand = new Object()
|
||||||
tuyaCommand.dps = deviceTopic.key
|
tuyaCommand.dps = deviceTopic.key
|
||||||
switch (deviceTopic.type) {
|
switch (deviceTopic.type) {
|
||||||
@@ -315,19 +319,26 @@ class TuyaDevice {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'int':
|
case 'int':
|
||||||
|
case 'float':
|
||||||
if (isNaN(command)) {
|
if (isNaN(command)) {
|
||||||
tuyaCommand.set = '!!!INVALID!!!'
|
tuyaCommand.set = '!!!INVALID!!!'
|
||||||
} else if (deviceTopic.hasOwnProperty('min') && deviceTopic.hasOwnProperty('max')) {
|
} else if (deviceTopic.hasOwnProperty('min') && deviceTopic.hasOwnProperty('max')) {
|
||||||
tuyaCommand.set = (command >= deviceTopic.min && command <= deviceTopic.max ) ? parseInt(command) : '!!!INVALID!!!'
|
if (command >= deviceTopic.min && command <= deviceTopic.max ) {
|
||||||
|
tuyaCommand.set = deviceTopic.type = 'int' ? parseInt(command) : parseFloat(command)
|
||||||
} else {
|
} else {
|
||||||
tuyaCommand.set = parseInt(command)
|
tuyaCommand.set = '!!!INVALID!!!'
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tuyaCommand.set = deviceTopic.type = 'int' ? parseInt(command) : parseFloat(command)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'hsb':
|
case 'hsb':
|
||||||
tuyaCommand.set = this.convertToTuyaHsbColor(command, deviceTopic.components)
|
this.updateSetColorState(command, deviceTopic.components)
|
||||||
|
tuyaCommand.set = this.getTuyaHsbColor()
|
||||||
break;
|
break;
|
||||||
case 'hsbhex':
|
case 'hsbhex':
|
||||||
tuyaCommand.set = this.convertToTuyaHsbHexColor(command, deviceTopic.components)
|
this.updateSetColorState(command, deviceTopic.components)
|
||||||
|
tuyaCommand.set = this.getTuyaHsbHexColor()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (tuyaCommand.set === '!!!INVALID!!!') {
|
if (tuyaCommand.set === '!!!INVALID!!!') {
|
||||||
@@ -342,8 +353,8 @@ class TuyaDevice {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes Tuya color value in HSB or HSBHEX format and updates
|
// Takes Tuya color value in HSB or HSBHEX format and
|
||||||
// cached HSB color state for device
|
// updates cached HSB color state for device
|
||||||
updateColorState(value) {
|
updateColorState(value) {
|
||||||
let h, s, b
|
let h, s, b
|
||||||
if (this.config.colorType === 'hsbhex') {
|
if (this.config.colorType === 'hsbhex') {
|
||||||
@@ -358,37 +369,37 @@ class TuyaDevice {
|
|||||||
this.state.color.s = Math.round(parseInt(s, 16) / 10) // Convert saturation to 100 Scale
|
this.state.color.s = Math.round(parseInt(s, 16) / 10) // Convert saturation to 100 Scale
|
||||||
this.state.color.b = parseInt(b, 16) // Convert brightness to 1000 scale
|
this.state.color.b = parseInt(b, 16) // Convert brightness to 1000 scale
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize the set color values for first time. Used to conflicts
|
||||||
|
// when mulitple HSB components are updated in quick succession
|
||||||
|
if (!this.state.setColor) {
|
||||||
|
this.state.setColor = this.state.color
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes provided decimal HSB components from MQTT topic, combines with any
|
// Updates the set color values based on received value from command topics
|
||||||
// cached (unchanged) component values and converts to Tuya HSB format
|
// This is used to cache set color values when mulitple HSB components use
|
||||||
convertToTuyaHsbColor(value, components) {
|
// different topics and updates come in quick succession
|
||||||
// Start with cached color values
|
updateSetColorState(value, components) {
|
||||||
const newColor = this.state.color
|
|
||||||
|
|
||||||
// Update any HSB component with a changed value
|
// Update any HSB component with a changed value
|
||||||
components = components.split(',')
|
components = components.split(',')
|
||||||
const values = value.split(',')
|
const values = value.split(',')
|
||||||
for (let i in components) {
|
for (let i in components) {
|
||||||
newColor[components[i]] = Math.round(values[i])
|
this.state.setColor[components[i]] = Math.round(values[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns Tuya HSB format value from current setColor HSB value
|
||||||
|
getTuyaHsbColor() {
|
||||||
// Convert new HSB color to Tuya style HSB format
|
// Convert new HSB color to Tuya style HSB format
|
||||||
const hexColor = newColor.h.toString(16).padStart(4, '0') + (10 * newColor.s).toString(16).padStart(4, '0') + (newColor.b).toString(16).padStart(4, '0')
|
let {h, s, b} = this.state.setColor
|
||||||
|
const hexColor = h.toString(16).padStart(4, '0') + (10 * s).toString(16).padStart(4, '0') + (b).toString(16).padStart(4, '0')
|
||||||
return hexColor
|
return hexColor
|
||||||
}
|
}
|
||||||
|
|
||||||
convertToTuyaHsbHexColor(value, components) {
|
// Returns Tuya HSBHEX format value from current setColor HSB value
|
||||||
// Start with cached color values
|
getTuyaHsbHexColor() {
|
||||||
const newColor = this.state.color
|
let {h, s, b} = this.state.setColor
|
||||||
|
|
||||||
// Update any HSB component with a changed value
|
|
||||||
components = components.split(',')
|
|
||||||
const values = value.split(',')
|
|
||||||
for (let i in components) {
|
|
||||||
newColor[components[i]] = Math.round(values[i])
|
|
||||||
}
|
|
||||||
let {h, s, b} = newColor
|
|
||||||
const hsb = h.toString(16).padStart(4, '0') + Math.round(2.55 * s).toString(16).padStart(2, '0') + Math.round(b * .255).toString(16).padStart(2, '0');
|
const hsb = h.toString(16).padStart(4, '0') + Math.round(2.55 * s).toString(16).padStart(2, '0') + Math.round(b * .255).toString(16).padStart(2, '0');
|
||||||
h /= 60;
|
h /= 60;
|
||||||
s /= 100;
|
s /= 100;
|
||||||
@@ -427,7 +438,7 @@ class TuyaDevice {
|
|||||||
// 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 (this.config.dpsColor === topic.key) {
|
||||||
if (this.state.color.s > 0) {
|
if (this.state.setColor.s > 0) {
|
||||||
// If setting an HSB value with saturation > 0, light should be in color mode
|
// If setting an HSB value with saturation > 0, light should be in color mode
|
||||||
targetMode = 'colour'
|
targetMode = 'colour'
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user