mirror of
https://github.com/lehanspb/tuya-mqtt.git
synced 2025-12-18 08:13:23 +00:00
Color Tweaks/Cleanups
Minor tweaks and misc cleanups preparing for 3.0.0 release
This commit is contained in:
@@ -7,14 +7,19 @@ class RGBTWLight extends TuyaDevice {
|
|||||||
async init() {
|
async init() {
|
||||||
await this.guessLightInfo()
|
await this.guessLightInfo()
|
||||||
|
|
||||||
|
if (!this.guess.dpsPower && !this.config.dpsPower) {
|
||||||
|
debug('Automatic discovery of Tuya bulb settings failed and no manual configuration')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Set device specific variables
|
// Set device specific variables
|
||||||
this.config.dpsPower = this.config.dpsPower ? this.config.dpsPower : this.guess.dpsPower
|
this.config.dpsPower = this.config.dpsPower ? this.config.dpsPower : this.guess.dpsPower
|
||||||
this.config.dpsMode = this.config.dpsMode ? this.config.dpsMode : this.guess.dpsMode
|
this.config.dpsMode = this.config.dpsMode ? this.config.dpsMode : this.guess.dpsMode
|
||||||
this.config.dpsWhiteValue = this.config.dpsWhiteValue ? this.config.dpsWhiteValue : this.guess.dpsWhiteValue
|
this.config.dpsWhiteValue = this.config.dpsWhiteValue ? this.config.dpsWhiteValue : this.guess.dpsWhiteValue
|
||||||
this.config.whiteValueScale = this.config.whiteValueScale ? this.config.whiteValueScale : this.guess.whiteValueScale
|
this.config.whiteValueScale = this.config.whiteValueScale ? this.config.whiteValueScale : this.guess.whiteValueScale
|
||||||
this.config.dpsColorTemp = this.config.dpsColorTemp ? this.config.dpsColorTemp : this.guess.dpsColorTemp
|
this.config.dpsColorTemp = this.config.dpsColorTemp ? this.config.dpsColorTemp : this.guess.dpsColorTemp
|
||||||
this.config.minColorTemp = this.config.minColorTemp ? this.config.minColorTemp : 165
|
this.config.minColorTemp = this.config.minColorTemp ? this.config.minColorTemp : 160
|
||||||
this.config.maxColorTemp = this.config.maxColorTemp ? this.config.maxColorTemp : 375
|
this.config.maxColorTemp = this.config.maxColorTemp ? this.config.maxColorTemp : 385
|
||||||
this.config.colorTempScale = this.config.colorTempScale ? this.config.colorTempScale : this.guess.colorTempScale
|
this.config.colorTempScale = this.config.colorTempScale ? this.config.colorTempScale : this.guess.colorTempScale
|
||||||
this.config.dpsColor = this.config.dpsColor ? this.config.dpsColor : this.guess.dpsColor
|
this.config.dpsColor = this.config.dpsColor ? this.config.dpsColor : this.guess.dpsColor
|
||||||
this.config.colorType = this.config.colorType ? this.config.colorType : this.guess.colorType
|
this.config.colorType = this.config.colorType ? this.config.colorType : this.guess.colorType
|
||||||
@@ -29,7 +34,7 @@ class RGBTWLight extends TuyaDevice {
|
|||||||
key: this.config.dpsPower,
|
key: this.config.dpsPower,
|
||||||
type: 'bool'
|
type: 'bool'
|
||||||
},
|
},
|
||||||
white_value_state: {
|
white_brightness_state: {
|
||||||
key: this.config.dpsWhiteValue,
|
key: this.config.dpsWhiteValue,
|
||||||
type: 'int',
|
type: 'int',
|
||||||
min: 1,
|
min: 1,
|
||||||
@@ -43,7 +48,7 @@ class RGBTWLight extends TuyaDevice {
|
|||||||
type: this.config.colorType,
|
type: this.config.colorType,
|
||||||
components: 'h,s'
|
components: 'h,s'
|
||||||
},
|
},
|
||||||
brightness_state: {
|
color_brightness_state: {
|
||||||
key: this.config.dpsColor,
|
key: this.config.dpsColor,
|
||||||
type: this.config.colorType,
|
type: this.config.colorType,
|
||||||
components: 'b'
|
components: 'b'
|
||||||
@@ -91,13 +96,13 @@ class RGBTWLight extends TuyaDevice {
|
|||||||
name: (this.config.name) ? this.config.name : this.config.id,
|
name: (this.config.name) ? this.config.name : this.config.id,
|
||||||
state_topic: this.baseTopic+'state',
|
state_topic: this.baseTopic+'state',
|
||||||
command_topic: this.baseTopic+'command',
|
command_topic: this.baseTopic+'command',
|
||||||
brightness_state_topic: this.baseTopic+'brightness_state',
|
brightness_state_topic: this.baseTopic+'color_brightness_state',
|
||||||
brightness_command_topic: this.baseTopic+'brightness_command',
|
brightness_command_topic: this.baseTopic+'color_brightness_command',
|
||||||
brightness_scale: 100,
|
brightness_scale: 100,
|
||||||
hs_state_topic: this.baseTopic+'hs_state',
|
hs_state_topic: this.baseTopic+'hs_state',
|
||||||
hs_command_topic: this.baseTopic+'hs_command',
|
hs_command_topic: this.baseTopic+'hs_command',
|
||||||
white_value_state_topic: this.baseTopic+'white_value_state',
|
white_value_state_topic: this.baseTopic+'white_brightness_state',
|
||||||
white_value_command_topic: this.baseTopic+'white_value_command',
|
white_value_command_topic: this.baseTopic+'white_brightness_command',
|
||||||
white_value_scale: 100,
|
white_value_scale: 100,
|
||||||
unique_id: this.config.id,
|
unique_id: this.config.id,
|
||||||
device: this.deviceData
|
device: this.deviceData
|
||||||
@@ -119,14 +124,18 @@ class RGBTWLight extends TuyaDevice {
|
|||||||
this.guess = new Object()
|
this.guess = new Object()
|
||||||
debug('Attempting to detect light capabilites and DPS values...')
|
debug('Attempting to detect light capabilites and DPS values...')
|
||||||
debug('Querying DPS 2 for white/color mode setting...')
|
debug('Querying DPS 2 for white/color mode setting...')
|
||||||
let mode = await this.device.get({"dps": 2})
|
|
||||||
if (mode && (mode === 'white' || mode === 'colour' || mode.toString().includes('scene'))) {
|
// Check if DPS 2 contains typical values for RGBTW light
|
||||||
|
const mode2 = await this.device.get({"dps": 2})
|
||||||
|
const mode21 = await this.device.get({"dps": 21})
|
||||||
|
if (mode2 && (mode2 === 'white' || mode2 === 'colour' || mode2.toString().includes('scene'))) {
|
||||||
debug('Detected probably Tuya color bulb at DPS 1-5, checking more details...')
|
debug('Detected probably Tuya color bulb at DPS 1-5, checking more details...')
|
||||||
this.guess = {'dpsPower': 1, 'dpsMode': 2, 'dpsWhiteValue': 3, 'whiteValueScale': 255, 'dpsColorTemp': 4, 'colorTempScale': 255, 'dpsColor': 5}
|
this.guess = {'dpsPower': 1, 'dpsMode': 2, 'dpsWhiteValue': 3, 'whiteValueScale': 255, 'dpsColorTemp': 4, 'colorTempScale': 255, 'dpsColor': 5}
|
||||||
} else {
|
} else if (mode21 && (mode21 === 'white' || mode21 === 'colour' || mode21.toString().includes('scene'))) {
|
||||||
debug('Detected likely Tuya color bulb at DPS 20-24, checking more details...')
|
debug('Detected likely Tuya color bulb at DPS 20-24, checking more details...')
|
||||||
this.guess = {'dpsPower': 20, 'dpsMode': 21, 'dpsWhiteValue': 22, 'whiteValueScale': 1000, 'dpsColorTemp': 23, 'colorTempScale': 1000, 'dpsColor': 24}
|
this.guess = {'dpsPower': 20, 'dpsMode': 21, 'dpsWhiteValue': 22, 'whiteValueScale': 1000, 'dpsColorTemp': 23, 'colorTempScale': 1000, 'dpsColor': 24}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.guess.dpsPower) {
|
if (this.guess.dpsPower) {
|
||||||
debug('Attempting to detect if bulb supports color temperature...')
|
debug('Attempting to detect if bulb supports color temperature...')
|
||||||
const colorTemp = await this.device.get({"dps": this.guess.dpsColorTemp})
|
const colorTemp = await this.device.get({"dps": this.guess.dpsColorTemp})
|
||||||
@@ -140,8 +149,6 @@ class RGBTWLight extends TuyaDevice {
|
|||||||
const color = await this.device.get({"dps": this.guess.dpsColor})
|
const color = await this.device.get({"dps": this.guess.dpsColor})
|
||||||
this.guess.colorType = (color && color.length === 12) ? 'hsb' : 'hsbhex'
|
this.guess.colorType = (color && color.length === 12) ? 'hsb' : 'hsbhex'
|
||||||
debug ('Detected Tuya color format '+this.guess.colorType.toUpperCase())
|
debug ('Detected Tuya color format '+this.guess.colorType.toUpperCase())
|
||||||
} else {
|
|
||||||
debug('No Tuya color bulb detected, if this device is definitely a Tuya bulb please manually specify settings.')
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,6 +106,25 @@ class TuyaDevice {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get and update state of all dps properties for device
|
||||||
|
async getStates() {
|
||||||
|
// Suppress topic updates while syncing state
|
||||||
|
this.connected = false
|
||||||
|
for (let topic in this.deviceTopics) {
|
||||||
|
const key = this.deviceTopics[topic].key
|
||||||
|
try {
|
||||||
|
const result = await this.device.get({"dps": key})
|
||||||
|
this.state.dps[key].val = result
|
||||||
|
this.state.dps[key].updated = true
|
||||||
|
} catch {
|
||||||
|
debugError('Could not get value for device DPS key '+key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.connected = true
|
||||||
|
// Force topic update now that all states are fully in sync
|
||||||
|
this.publishTopics()
|
||||||
|
}
|
||||||
|
|
||||||
// Update cached DPS states on data updates
|
// Update cached DPS states on data updates
|
||||||
updateState(data) {
|
updateState(data) {
|
||||||
if (typeof data.dps != 'undefined') {
|
if (typeof data.dps != 'undefined') {
|
||||||
@@ -188,7 +207,7 @@ class TuyaDevice {
|
|||||||
break;
|
break;
|
||||||
case 'int':
|
case 'int':
|
||||||
case 'float':
|
case 'float':
|
||||||
state = this.parseStateNumber(value, deviceTopic)
|
state = this.parseNumberState(value, deviceTopic)
|
||||||
break;
|
break;
|
||||||
case 'hsb':
|
case 'hsb':
|
||||||
case 'hsbhex':
|
case 'hsbhex':
|
||||||
@@ -207,7 +226,7 @@ class TuyaDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse the received state numeric value based on deviceTopic rules
|
// Parse the received state numeric value based on deviceTopic rules
|
||||||
parseStateNumber(value, deviceTopic) {
|
parseNumberState(value, deviceTopic) {
|
||||||
// Check if it's a number and it's not outside of defined range
|
// Check if it's a number and it's not outside of defined range
|
||||||
if (isNaN(value)) {
|
if (isNaN(value)) {
|
||||||
return ''
|
return ''
|
||||||
@@ -216,18 +235,10 @@ class TuyaDevice {
|
|||||||
// Perform any required math transforms before returing command value
|
// Perform any required math transforms before returing command value
|
||||||
switch (deviceTopic.type) {
|
switch (deviceTopic.type) {
|
||||||
case 'int':
|
case 'int':
|
||||||
if (deviceTopic.stateMath) {
|
value = (deviceTopic.stateMath) ? parseInt(Math.round(evaluate(value+deviceTopic.stateMath))) : value = parseInt(value)
|
||||||
value = parseInt(Math.round(evaluate(value+deviceTopic.stateMath)))
|
|
||||||
} else {
|
|
||||||
value = parseInt(value)
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'float':
|
case 'float':
|
||||||
if (deviceTopic.stateMath) {
|
value = (deviceTopic.stateMath) ? parseFloat(evaluate(value+deviceTopic.stateMath)) : value = parseFloat(value)
|
||||||
value = parseFloat(evaluate(value+deviceTopic.stateMath))
|
|
||||||
} else {
|
|
||||||
value = parseFloat(value)
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,69 +247,49 @@ class TuyaDevice {
|
|||||||
|
|
||||||
// Process MQTT commands for all command topics at device level
|
// Process MQTT commands for all command topics at device level
|
||||||
processCommand(message, commandTopic) {
|
processCommand(message, commandTopic) {
|
||||||
const command = this.getCommandFromMessage(message)
|
let command
|
||||||
|
if (utils.isJsonString(message)) {
|
||||||
|
debugCommand('Received MQTT command message is a JSON string')
|
||||||
|
command = JSON.parse(message);
|
||||||
|
} else {
|
||||||
|
debugCommand('Received MQTT command message is a text string')
|
||||||
|
command = message.toLowerCase()
|
||||||
|
}
|
||||||
|
|
||||||
if (commandTopic === 'command' && command === 'get-states') {
|
if (commandTopic === 'command' && command === 'get-states') {
|
||||||
// Handle "get-states" command to update device state
|
// Handle "get-states" command to update device state
|
||||||
debugCommand('Received command: ', command)
|
debugCommand('Received command: ', command)
|
||||||
this.getStates()
|
this.getStates()
|
||||||
} else {
|
} else {
|
||||||
// Call device specific command topic handler
|
// Call device specific command topic handler
|
||||||
this.processDeviceCommand(message, commandTopic)
|
this.processDeviceCommand(command, commandTopic)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process MQTT commands for all command topics at device level
|
// Process MQTT commands for all command topics at device level
|
||||||
processDeviceCommand(message, commandTopic) {
|
processDeviceCommand(command, commandTopic) {
|
||||||
// Determine state topic from command topic to find proper template
|
// Determine state topic from command topic to find proper template
|
||||||
const stateTopic = commandTopic.replace('command', 'state')
|
const stateTopic = commandTopic.replace('command', 'state')
|
||||||
const deviceTopic = this.deviceTopics.hasOwnProperty(stateTopic) ? this.deviceTopics[stateTopic] : ''
|
const deviceTopic = this.deviceTopics.hasOwnProperty(stateTopic) ? this.deviceTopics[stateTopic] : ''
|
||||||
|
|
||||||
if (deviceTopic) {
|
if (deviceTopic) {
|
||||||
debugCommand('Device '+this.options.id+' received command topic: '+commandTopic+', message: '+message)
|
debugCommand('Device '+this.options.id+' received command topic: '+commandTopic+', message: '+command)
|
||||||
const command = this.getCommandFromMessage(message)
|
|
||||||
let commandResult = this.sendTuyaCommand(command, deviceTopic)
|
let commandResult = this.sendTuyaCommand(command, deviceTopic)
|
||||||
if (!commandResult) {
|
if (!commandResult) {
|
||||||
debugCommand('Command topic '+this.baseTopic+commandTopic+' received invalid value: '+command)
|
debugCommand('Command topic '+this.baseTopic+commandTopic+' received invalid value: '+command)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
debugCommand('Invalid command topic '+this.baseTopic+commandTopic+' for device: '+this.config.name)
|
debugCommand('Invalid command topic '+this.baseTopic+commandTopic+' for device id: '+this.config.id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts message to TuyAPI JSON commands
|
|
||||||
getCommandFromMessage(message) {
|
|
||||||
let command
|
|
||||||
|
|
||||||
if (message != '1' && message != '0' && utils.isJsonString(message)) {
|
|
||||||
debugCommand('MQTT message is JSON')
|
|
||||||
command = JSON.parse(message);
|
|
||||||
} else {
|
|
||||||
switch(message.toLowerCase()) {
|
|
||||||
case 'on':
|
|
||||||
case 'off':
|
|
||||||
case '0':
|
|
||||||
case '1':
|
|
||||||
case 'true':
|
|
||||||
case 'false':
|
|
||||||
// convert simple messages (on, off, 1, 0) to TuyAPI commands
|
|
||||||
command = {
|
|
||||||
set: (message.toLowerCase() === 'on' || message === '1' || message === 'true' || message === 1) ? true : false
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
command = message.toLowerCase()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return command
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process Tuya JSON commands via DPS command topic
|
// Process Tuya JSON commands via DPS command topic
|
||||||
processDpsCommand(message) {
|
processDpsCommand(message) {
|
||||||
if (utils.isJsonString(message)) {
|
if (utils.isJsonString(message)) {
|
||||||
const tuyaCommand = this.getCommandFromMessage(message)
|
const command = JSON.parse(message)
|
||||||
debugCommand('Received command: '+tuyaCommand)
|
debugCommand('Parsed Tuya JSON command: '+JSON.stringify(command))
|
||||||
this.set(tuyaCommand)
|
this.set(command)
|
||||||
} else {
|
} else {
|
||||||
debugCommand('DPS command topic requires Tuya style JSON value')
|
debugCommand('DPS command topic requires Tuya style JSON value')
|
||||||
}
|
}
|
||||||
@@ -311,11 +302,11 @@ class TuyaDevice {
|
|||||||
} else {
|
} else {
|
||||||
const dpsMessage = this.parseDpsMessage(message)
|
const dpsMessage = this.parseDpsMessage(message)
|
||||||
debugCommand('Received command for DPS'+dpsKey+': ', message)
|
debugCommand('Received command for DPS'+dpsKey+': ', message)
|
||||||
const tuyaCommand = {
|
const command = {
|
||||||
dps: dpsKey,
|
dps: dpsKey,
|
||||||
set: dpsMessage
|
set: dpsMessage
|
||||||
}
|
}
|
||||||
this.set(tuyaCommand)
|
this.set(command)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -332,27 +323,9 @@ class TuyaDevice {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get and update state of all dps properties for device
|
|
||||||
async getStates() {
|
|
||||||
// Suppress topic updates while syncing state
|
|
||||||
this.connected = false
|
|
||||||
for (let topic in this.deviceTopics) {
|
|
||||||
const key = this.deviceTopics[topic].key
|
|
||||||
try {
|
|
||||||
const result = await this.device.get({"dps": key})
|
|
||||||
this.state.dps[key].val = result
|
|
||||||
this.state.dps[key].updated = true
|
|
||||||
} catch {
|
|
||||||
debugError('Could not get value for device DPS key '+key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.connected = true
|
|
||||||
// Force topic update now that all states are fully in sync
|
|
||||||
this.publishTopics()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set state based on command topic
|
// Set state based on command topic
|
||||||
sendTuyaCommand(command, deviceTopic) {
|
sendTuyaCommand(message, deviceTopic) {
|
||||||
|
let command = message.toLowerCase()
|
||||||
const tuyaCommand = new Object()
|
const tuyaCommand = new Object()
|
||||||
tuyaCommand.dps = deviceTopic.key
|
tuyaCommand.dps = deviceTopic.key
|
||||||
switch (deviceTopic.type) {
|
switch (deviceTopic.type) {
|
||||||
@@ -360,6 +333,7 @@ class TuyaDevice {
|
|||||||
if (command === 'toggle') {
|
if (command === 'toggle') {
|
||||||
tuyaCommand.set = !this.state.dps[tuyaCommand.dps].val
|
tuyaCommand.set = !this.state.dps[tuyaCommand.dps].val
|
||||||
} else {
|
} else {
|
||||||
|
command = this.parseBoolCommand(command)
|
||||||
if (typeof command.set === 'boolean') {
|
if (typeof command.set === 'boolean') {
|
||||||
tuyaCommand.set = command.set
|
tuyaCommand.set = command.set
|
||||||
} else {
|
} else {
|
||||||
@@ -369,16 +343,19 @@ class TuyaDevice {
|
|||||||
break;
|
break;
|
||||||
case 'int':
|
case 'int':
|
||||||
case 'float':
|
case 'float':
|
||||||
tuyaCommand.set = this.parseCommandNumber(command, deviceTopic)
|
tuyaCommand.set = this.parseNumberCommand(command, deviceTopic)
|
||||||
break;
|
break;
|
||||||
case 'hsb':
|
case 'hsb':
|
||||||
this.updateSetColorState(command, deviceTopic.components)
|
this.updateSetColorState(command, deviceTopic.components)
|
||||||
tuyaCommand.set = this.getTuyaHsbColor()
|
tuyaCommand.set = this.parseTuyaHsbColor()
|
||||||
break;
|
break;
|
||||||
case 'hsbhex':
|
case 'hsbhex':
|
||||||
this.updateSetColorState(command, deviceTopic.components)
|
this.updateSetColorState(command, deviceTopic.components)
|
||||||
tuyaCommand.set = this.getTuyaHsbHexColor()
|
tuyaCommand.set = this.parseTuyaHsbHexColor()
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
// If type is not one of the above just use the raw string as is
|
||||||
|
tuyaCommand.set = message
|
||||||
}
|
}
|
||||||
if (tuyaCommand.set === '!!!INVALID!!!') {
|
if (tuyaCommand.set === '!!!INVALID!!!') {
|
||||||
return false
|
return false
|
||||||
@@ -392,17 +369,39 @@ class TuyaDevice {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert simple bool commands to true/flase
|
||||||
|
parseBoolCommand(command) {
|
||||||
|
switch(command) {
|
||||||
|
case 'on':
|
||||||
|
case 'off':
|
||||||
|
case '0':
|
||||||
|
case '1':
|
||||||
|
case 'true':
|
||||||
|
case 'false':
|
||||||
|
return {
|
||||||
|
set: (command === 'on' || command === '1' || command === 'true' || command === 1) ? true : false
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return command
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Validate/transform set interger values
|
// Validate/transform set interger values
|
||||||
parseCommandNumber(command, deviceTopic) {
|
parseNumberCommand(command, deviceTopic) {
|
||||||
let value = undefined
|
let value = undefined
|
||||||
const invalid = '!!!INVALID!!!'
|
const invalid = '!!!INVALID!!!'
|
||||||
|
|
||||||
// Check if it's a number and it's not outside of defined range
|
// Check if it's a number and it's not outside of defined range
|
||||||
if (isNaN(command)) {
|
if (isNaN(command)) {
|
||||||
return invalid
|
return invalid
|
||||||
} else if ((deviceTopic.hasOwnProperty('min') && command < deviceTopic.min) ||
|
} else if (deviceTopic.hasOwnProperty('min') && command < deviceTopic.min) {
|
||||||
(deviceTopic.hasOwnProperty('max') && command > deviceTopic.max)) {
|
debugError('Received command value "'+command+'" that is less than the configured minimum value')
|
||||||
return invalid
|
debugError('Overriding command with minimum value '+deviceTopic.min)
|
||||||
|
command = deviceTopic.min
|
||||||
|
} else if (deviceTopic.hasOwnProperty('max') && command > deviceTopic.max) {
|
||||||
|
debugError('Received command value "'+command+'" that is greater than the configured maximum value')
|
||||||
|
debugError('Overriding command with maximum value: '+deviceTopic.max)
|
||||||
|
command = deviceTopic.max
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform any required math transforms before returing command value
|
// Perform any required math transforms before returing command value
|
||||||
@@ -445,7 +444,7 @@ 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.hasOwnProperty('setColor')) {
|
||||||
this.state.setColor = {
|
this.state.setColor = {
|
||||||
'h': this.state.color.h,
|
'h': this.state.color.h,
|
||||||
's': this.state.color.s,
|
's': this.state.color.s,
|
||||||
@@ -467,7 +466,7 @@ class TuyaDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns Tuya HSB format value from current setColor HSB value
|
// Returns Tuya HSB format value from current setColor HSB value
|
||||||
getTuyaHsbColor() {
|
parseTuyaHsbColor() {
|
||||||
// Convert new HSB color to Tuya style HSB format
|
// Convert new HSB color to Tuya style HSB format
|
||||||
let {h, s, b} = this.state.setColor
|
let {h, s, b} = this.state.setColor
|
||||||
const hexColor = h.toString(16).padStart(4, '0') + (10 * s).toString(16).padStart(4, '0') + (10 * b).toString(16).padStart(4, '0')
|
const hexColor = h.toString(16).padStart(4, '0') + (10 * s).toString(16).padStart(4, '0') + (10 * b).toString(16).padStart(4, '0')
|
||||||
@@ -475,7 +474,7 @@ class TuyaDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns Tuya HSBHEX format value from current setColor HSB value
|
// Returns Tuya HSBHEX format value from current setColor HSB value
|
||||||
getTuyaHsbHexColor() {
|
parseTuyaHsbHexColor() {
|
||||||
let {h, s, b} = this.state.setColor
|
let {h, s, b} = this.state.setColor
|
||||||
const hsb = h.toString(16).padStart(4, '0') + Math.round(2.55 * s).toString(16).padStart(2, '0') + Math.round(2.55 * b).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(2.55 * b).toString(16).padStart(2, '0');
|
||||||
h /= 60;
|
h /= 60;
|
||||||
@@ -518,8 +517,6 @@ class TuyaDevice {
|
|||||||
// If setting white level or color temperature, light should be in white mode
|
// If setting white level or color temperature, light should be in white mode
|
||||||
targetMode = 'white'
|
targetMode = 'white'
|
||||||
} else if (topic.key === this.config.dpsColor) {
|
} else if (topic.key === this.config.dpsColor) {
|
||||||
// Short sleep for cases where mulitple updates occur quickly
|
|
||||||
await msSleep(100)
|
|
||||||
if (this.state.setColor.s < 10) {
|
if (this.state.setColor.s < 10) {
|
||||||
// If saturation is < 10 then white mode
|
// If saturation is < 10 then white mode
|
||||||
targetMode = 'white'
|
targetMode = 'white'
|
||||||
|
|||||||
Reference in New Issue
Block a user