Skip to content

Commit

Permalink
Merge pull request #86 from ikaruswill/deprecate-set-speed
Browse files Browse the repository at this point in the history
Deprecate set_speed
  • Loading branch information
ikaruswill authored May 21, 2022
2 parents 7569d2c + 1837024 commit 52a89c1
Showing 1 changed file with 23 additions and 67 deletions.
90 changes: 23 additions & 67 deletions fan-xiaomi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -183,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
percentage: 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 = value / this.supportedAttributes.speedLevels * 100
this.setSpeedPercentage(hass, speedPercentage)
}

getSpeedLevel(hass) {
let speedPercentage = this.getSpeedPercentage(hass)
return Math.round(speedPercentage / 100 * this.supportedAttributes.speedLevels)
}

setPresetMode(hass, value) {
if (this.config.platform === 'default') {
hass.callService('fan', 'set_preset_mode', {
Expand Down Expand Up @@ -303,15 +308,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;
}
Expand Down Expand Up @@ -423,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;
}
Expand Down Expand Up @@ -499,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);
}
}

Expand Down Expand Up @@ -722,7 +708,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),
Expand Down Expand Up @@ -945,7 +931,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, speed_level, preset_mode, model, led,
temperature, humidity, power_supply
}) {
fanboxa.querySelector('.var-title').textContent = title
Expand Down Expand Up @@ -1055,25 +1041,9 @@ 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 = `<ha-icon icon="mdi:numeric-${speedLevel}-box-outline"></ha-icon>`
iconSpan.innerHTML = `<ha-icon icon="mdi:numeric-${speed_level}-box-outline"></ha-icon>`
activeElement = fanboxa.querySelector('.fanbox .blades')
activeElement.className = `blades level${speedLevel}`
activeElement.className = `blades level${speed_level}`

// Natural mode
activeElement = fanboxa.querySelector('.var-natural')
Expand Down Expand Up @@ -1163,7 +1133,7 @@ LED

// Add to logs
log() {
//console.log(...arguments)
// console.log(...arguments)
}
warn() {
// console.log(...arguments)
Expand All @@ -1183,10 +1153,6 @@ class ContentCardEditor extends LitElement {
...defaultConfig,
...config,
};

if (this.config.platform === 'default') {
this.config.use_standard_speeds = true;
}
}

static get properties() {
Expand Down Expand Up @@ -1249,16 +1215,6 @@ class ContentCardEditor extends LitElement {
</ha-formfield>
</div>
<div class="row">
<ha-formfield label="Use HA standard speeds (low/medium/high)">
<ha-switch
.disabled=${this.config.platform === 'default'}
.checked=${this.config.use_standard_speeds}
.configValue="${'use_standard_speeds'}"
@change=${this._valueChanged}
></ha-switch>
</ha-formfield>
</div>
<div class="row">
<ha-formfield label="Show sleep mode button">
<ha-switch
.checked=${this.config.force_sleep_mode_support}
Expand Down

1 comment on commit 52a89c1

@ian-craig
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This got overwritten by the boilerplate card refactor, so I've redone the equivalent change as 8b22cb7

@ikaruswill I think we're probably now in a decent place to release this, but will leave it to you to do any extra testing and kick off the release. We also now have a Release Action which I think should run when a release is created and automatically attach the built files to the release, but I don't know if there's any additional steps you usually run which we should add to this Action.

Please sign in to comment.