From 0cecce17400abcab1c90724af53ab4410dbc7c07 Mon Sep 17 00:00:00 2001 From: Will Ho Date: Sat, 21 May 2022 19:05:17 +0800 Subject: [PATCH 1/9] Remove speedList logic in checkFanFeatures --- fan-xiaomi.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/fan-xiaomi.js b/fan-xiaomi.js index 23c66af..2dbf6f4 100644 --- a/fan-xiaomi.js +++ b/fan-xiaomi.js @@ -303,15 +303,6 @@ class FanXiaomi extends HTMLElement { } checkFanFeatures(attributes) { - // TODO: Deprecate as fan.set_speed is deprecated - this.supportedAttributes.speedList = ['low', 'medium', 'high']; - if (attributes.speed_list) { - this.supportedAttributes.speedList = attributes.speed_list.filter(s => { - const speed = s.toLowerCase(); - return speed !== "nature" && speed !== "normal" && speed !== "off"; - }); - } - if (attributes.preset_mode && attributes.preset_modes && attributes.preset_modes.includes("Nature")) { this.supportedAttributes.naturalSpeed = true; } From 19696739f2eaa146c03203ea860596e4a24b60ef Mon Sep 17 00:00:00 2001 From: Will Ho Date: Sat, 21 May 2022 19:34:30 +0800 Subject: [PATCH 2/9] Remove use_standard_speeds config --- fan-xiaomi.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/fan-xiaomi.js b/fan-xiaomi.js index 2dbf6f4..01ca6fa 100644 --- a/fan-xiaomi.js +++ b/fan-xiaomi.js @@ -40,7 +40,6 @@ const defaultConfig = { platform: OptionsPlatform[0], entity: "fan.fan", disable_animation: false, - use_standard_speeds: false, force_sleep_mode_support: false, hide_led_button: false } @@ -1174,10 +1173,6 @@ class ContentCardEditor extends LitElement { ...defaultConfig, ...config, }; - - if (this.config.platform === 'default') { - this.config.use_standard_speeds = true; - } } static get properties() { @@ -1240,16 +1235,6 @@ class ContentCardEditor extends LitElement {
- - - -
-
Date: Sat, 21 May 2022 19:35:08 +0800 Subject: [PATCH 3/9] Implement setSpeedPercentage, setSpeedLevel, getSpeedLevel Also remove deprecated getSpeed and setSpeed --- fan-xiaomi.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/fan-xiaomi.js b/fan-xiaomi.js index 01ca6fa..4968f3c 100644 --- a/fan-xiaomi.js +++ b/fan-xiaomi.js @@ -182,21 +182,27 @@ class FanXiaomi extends HTMLElement { return hass.states[this.config.entity].attributes['oscillating']; } - setSpeed(hass, value) { - hass.callService('fan', 'set_speed', { + setSpeedPercentage(hass, value) { + hass.callService('fan', 'set_percentage', { entity_id: this.config.entity, speed: value }); } - getSpeed(hass) { - return hass.states[this.config.entity].attributes['speed']; - } - getSpeedPercentage(hass) { return hass.states[this.config.entity].attributes['percentage']; } + setSpeedLevel(hass, value) { + let speedPercentage = Math.round(value / this.supportedAttributes.speedLevels) * 100 + this.setSpeedPercentage(hass, speedPercentage) + } + + getSpeedLevel(hass) { + let speedPercentage = Number(hass.states[this.config.entity].attributes['speed']) + return Math.round(speedPercentage / 100 * this.supportedAttributes.speedLevels) + } + setPresetMode(hass, value) { if (this.config.platform === 'default') { hass.callService('fan', 'set_preset_mode', { From 0c6bb60ec8675986467a1dd75859ff44049bb954 Mon Sep 17 00:00:00 2001 From: Will Ho Date: Sat, 21 May 2022 19:35:29 +0800 Subject: [PATCH 4/9] Remove trick to support any fan --- fan-xiaomi.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/fan-xiaomi.js b/fan-xiaomi.js index 4968f3c..14bda83 100644 --- a/fan-xiaomi.js +++ b/fan-xiaomi.js @@ -419,10 +419,6 @@ class FanXiaomi extends HTMLElement { this.supportedAttributes.sleepMode = true; } - //trick to support of 'any' fan - if (this.config.use_standard_speeds) { - this.supportedAttributes.speedList = ['low', 'medium', 'high'] - } if (this.config.force_sleep_mode_support) { this.supportedAttributes.sleepMode = true; } From d3b7316dad9b869e48ec5332aa100437c640783d Mon Sep 17 00:00:00 2001 From: Will Ho Date: Sat, 21 May 2022 19:35:58 +0800 Subject: [PATCH 5/9] Implement speedLevel parameter --- fan-xiaomi.js | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/fan-xiaomi.js b/fan-xiaomi.js index 14bda83..4ced9da 100644 --- a/fan-xiaomi.js +++ b/fan-xiaomi.js @@ -714,7 +714,7 @@ class FanXiaomi extends HTMLElement { oscillating: this.getOscillation(hass), delay_off_countdown: this.getTimer(hass), angle: this.getAngle(hass), - speed: this.getSpeed(hass), + speed_level: this.getSpeedLevel(hass), preset_mode: this.getPresetMode(hass), model: this.getModel(hass), led: this.getLed(hass), @@ -937,7 +937,7 @@ LED // Define UI Parameters setUI(fanboxa, {title, speed_percentage, state, child_lock, oscillating, - delay_off_countdown, angle, speed, preset_mode, model, led, + delay_off_countdown, angle, speedLevel, preset_mode, model, led, temperature, humidity, power_supply }) { fanboxa.querySelector('.var-title').textContent = title @@ -1047,22 +1047,6 @@ LED activeElement.classList.remove('active') } - let speedRegexpMatch - let speedLevel - let speed_percentage_int = Number(speed_percentage) - if (this.config.use_standard_speeds || this.config.platform === 'default') { - let speedCount = this.supportedAttributes.speedList.length - speedLevel = Math.round(speed_percentage_int / 100 * speedCount) - } else { - let speedRegexp = /Level (\d)/g - speedRegexpMatch = speedRegexp.exec(speed) - if (speedRegexpMatch && speedRegexpMatch.length > 0) { - speedLevel = speedRegexpMatch[1] - } - if (speedLevel === undefined) { - speedLevel = 1 - } - } iconSpan.innerHTML = `` activeElement = fanboxa.querySelector('.fanbox .blades') activeElement.className = `blades level${speedLevel}` From 82b8e76979f336490712e506534c1f90a0696978 Mon Sep 17 00:00:00 2001 From: Will Ho Date: Sat, 21 May 2022 19:36:27 +0800 Subject: [PATCH 6/9] Implement logic to increment speedLevel --- fan-xiaomi.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/fan-xiaomi.js b/fan-xiaomi.js index 4ced9da..502e0c6 100644 --- a/fan-xiaomi.js +++ b/fan-xiaomi.js @@ -491,23 +491,17 @@ class FanXiaomi extends HTMLElement { //let iconSpan = u.querySelector('.icon-waper') let icon = u.querySelector('.icon-waper > ha-icon').getAttribute('icon') let newSpeedLevel - let newSpeed let maskSpeedLevel = /mdi:numeric-(\d)-box-outline/g let speedLevelMatch = maskSpeedLevel.exec(icon) let speedLevel = parseInt(speedLevelMatch ? speedLevelMatch[1] : 1) - if (this.config.use_standard_speeds || this.config.platform === 'default') { - newSpeedLevel = this.supportedAttributes.speedList[(speedLevel < - this.supportedAttributes.speedList.length ? speedLevel: 0)] - newSpeed = newSpeedLevel - } else { - newSpeedLevel = (speedLevel < this.supportedAttributes.speedLevels ? speedLevel+1: 1) - newSpeed = `Level ${newSpeedLevel}` + newSpeedLevel = speedLevel + 1 + if (newSpeedLevel > this.supportedAttributes.speedLevels) { + newSpeedLevel = 1 } - - this.log(`Set speed to: ${newSpeed}`) - this.setSpeed(hass, newSpeed); + this.log(`Set speed to: ${newSpeedLevel}`) + this.setSpeedLevel(hass, newSpeedLevel); } } From 3755f96daa59c22dd60a2a1dc4a5d9e6a3a2504f Mon Sep 17 00:00:00 2001 From: Will Ho Date: Sat, 21 May 2022 19:46:39 +0800 Subject: [PATCH 7/9] Fix set_percentage service call --- fan-xiaomi.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fan-xiaomi.js b/fan-xiaomi.js index 502e0c6..a4a1694 100644 --- a/fan-xiaomi.js +++ b/fan-xiaomi.js @@ -185,7 +185,7 @@ class FanXiaomi extends HTMLElement { setSpeedPercentage(hass, value) { hass.callService('fan', 'set_percentage', { entity_id: this.config.entity, - speed: value + percentage: value }); } From cb6a4650bfdb0ba8450c509809379beda574a964 Mon Sep 17 00:00:00 2001 From: Will Ho Date: Sat, 21 May 2022 19:46:58 +0800 Subject: [PATCH 8/9] Fix speed_level argument reference --- fan-xiaomi.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fan-xiaomi.js b/fan-xiaomi.js index a4a1694..b77afae 100644 --- a/fan-xiaomi.js +++ b/fan-xiaomi.js @@ -931,7 +931,7 @@ LED // Define UI Parameters setUI(fanboxa, {title, speed_percentage, state, child_lock, oscillating, - delay_off_countdown, angle, speedLevel, preset_mode, model, led, + delay_off_countdown, angle, speed_level, preset_mode, model, led, temperature, humidity, power_supply }) { fanboxa.querySelector('.var-title').textContent = title @@ -1041,9 +1041,9 @@ LED activeElement.classList.remove('active') } - iconSpan.innerHTML = `` + iconSpan.innerHTML = `` activeElement = fanboxa.querySelector('.fanbox .blades') - activeElement.className = `blades level${speedLevel}` + activeElement.className = `blades level${speed_level}` // Natural mode activeElement = fanboxa.querySelector('.var-natural') @@ -1133,7 +1133,7 @@ LED // Add to logs log() { - //console.log(...arguments) + // console.log(...arguments) } warn() { // console.log(...arguments) From 1837024ec817889ea46bcc946b812c59ebb17c08 Mon Sep 17 00:00:00 2001 From: Will Ho Date: Sat, 21 May 2022 19:47:27 +0800 Subject: [PATCH 9/9] Fix getSpeedLevel setSpeedLevel logic --- fan-xiaomi.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fan-xiaomi.js b/fan-xiaomi.js index b77afae..437d03e 100644 --- a/fan-xiaomi.js +++ b/fan-xiaomi.js @@ -194,12 +194,12 @@ class FanXiaomi extends HTMLElement { } setSpeedLevel(hass, value) { - let speedPercentage = Math.round(value / this.supportedAttributes.speedLevels) * 100 + let speedPercentage = value / this.supportedAttributes.speedLevels * 100 this.setSpeedPercentage(hass, speedPercentage) } getSpeedLevel(hass) { - let speedPercentage = Number(hass.states[this.config.entity].attributes['speed']) + let speedPercentage = this.getSpeedPercentage(hass) return Math.round(speedPercentage / 100 * this.supportedAttributes.speedLevels) }