Skip to content

Commit

Permalink
Redo deprecate-set-speed after boilerplate refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-craig committed Jun 18, 2022
1 parent e21f1cf commit 8b22cb7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 74 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ type: "custom:fan-xiaomi"
platform: xiaomi_miio_airpurifier
```

| Card attribute | Default | Description |
| -------------------------- | --------- | ---------------------------------------------------------------------------------------------------- |
| `entity_id` | - | `entity_id` of your fan |
| `type` | - | Must be `custom:fan-xiaomi` |
| `name` | - | (Optional) Fan name shown on card title |
| `platform` | `default` | (Optional) If using Xiaomi Mi Smart Pedestal Fan integration, specify `xiaomi_miio_fan`. |
| `disable_animation` | `False` | (Optional) Disable fan image and animation |
| `use_standard_speeds` | `False` | (Optional) Use `low`/`medium`/`high` speeds, if you have issues using this card with your fan model. |
| `force_sleep_mode_support` | `False` | (Optional) Enables Sleep button in UI which sets speed to 1% |
| `hide_led_button` | `False` | (Optional) Hide LED toggle for supported models |
| Card attribute | Default | Description |
| -------------------------- | --------- | ---------------------------------------------------------------------------------------- |
| `entity_id` | - | `entity_id` of your fan |
| `type` | - | Must be `custom:fan-xiaomi` |
| `name` | - | (Optional) Fan name shown on card title |
| `platform` | `default` | (Optional) If using Xiaomi Mi Smart Pedestal Fan integration, specify `xiaomi_miio_fan`. |
| `disable_animation` | `False` | (Optional) Disable fan image and animation |
| `force_sleep_mode_support` | `False` | (Optional) Enables Sleep button in UI which sets speed to 1% |
| `hide_led_button` | `False` | (Optional) Hide LED toggle for supported models |

## Preview

Expand Down
2 changes: 0 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ interface FanXiaomiCustomConfig {
platform: typeof platforms[number];
entity: string;
disable_animation: boolean;
use_standard_speeds: boolean;
force_sleep_mode_support: boolean;
hide_led_button: boolean;
}
Expand All @@ -19,7 +18,6 @@ export const defaultConfig: FanXiaomiCustomConfig = {
platform: platforms[0],
entity: "",
disable_animation: false,
use_standard_speeds: false,
force_sleep_mode_support: false,
hide_led_button: false,
};
8 changes: 0 additions & 8 deletions src/xiaomi-fan-card-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,6 @@ export class FanXiaomiCardEditor extends ScopedRegistryHost(LitElement) implemen
return html`<mwc-list-item .value=${entity}>${entity}</mwc-list-item>`;
})}
</mwc-select>
<mwc-formfield label="Use HA standard speeds (low/medium/high)">
<mwc-switch
.disabled=${config.platform === "default"}
.checked=${this.config.platform === "default" || this.config.use_standard_speeds}
.configValue=${"use_standard_speeds"}
@change=${this._valueChanged}
></mwc-switch>
</mwc-formfield>
<mwc-formfield label="Show sleep mode button">
<mwc-switch
.checked=${config.force_sleep_mode_support}
Expand Down
64 changes: 10 additions & 54 deletions src/xiaomi-fan-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type SupportedAttributes = {
sleepMode: boolean;
led: boolean;
speedIncreaseDecreaseButtons: boolean;
speedList: string[];
};

type DeviceEntities = {
Expand Down Expand Up @@ -149,7 +148,6 @@ export class FanXiaomiCard extends LitElement {
sleepMode: false,
led: false,
speedIncreaseDecreaseButtons: false,
speedList: [],
};

@state() private config!: FanXiaomiCardConfig;
Expand Down Expand Up @@ -255,32 +253,13 @@ export class FanXiaomiCard extends LitElement {
return this.hass.states[this.config.entity].attributes["oscillating"];
}

private getSpeed() {
return this.hass.states[this.config.entity].attributes["speed"];
}

private getSpeedPercentage(): number {
return Number(this.hass.states[this.config.entity].attributes["percentage"]);
}

private getSpeedLevel(): number {
let speedLevel = 0;

if (this.config.use_standard_speeds || this.config.platform === "default") {
const speedCount = this.supportedAttributes.speedList.length;
speedLevel = Math.ceil((this.getSpeedPercentage() / 100) * speedCount);
} else {
const speedRegexp = /Level (\d)/g;
const speedRegexpMatch = speedRegexp.exec(this.getSpeed());
if (speedRegexpMatch && speedRegexpMatch.length > 0) {
speedLevel = Number(speedRegexpMatch[1]);
}
if (speedLevel === NaN) {
speedLevel = 1;
}
}

return speedLevel;
const speedCount = this.supportedAttributes.speedLevels;
return Math.ceil((this.getSpeedPercentage() / 100) * speedCount);
}

private setPresetMode(value) {
Expand Down Expand Up @@ -395,15 +374,6 @@ export class FanXiaomiCard extends LitElement {
}

private 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 &&
Expand Down Expand Up @@ -477,9 +447,6 @@ export class FanXiaomiCard extends LitElement {
}

//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 @@ -559,7 +526,7 @@ export class FanXiaomiCard extends LitElement {
</div>`
: ""}
<div class="fanbox ${state.state === "on" ? "active" : ""} ${oscillating ? "oscillation" : ""}">
<div class="blades level${speedLevel}">
<div class="blades level${state.state === "on" ? Math.max(1, speedLevel) : 0}">
<div class="b1 ang1"></div>
<div class="b2 ang25"></div>
<div class="b3 ang49"></div>
Expand Down Expand Up @@ -711,26 +678,15 @@ export class FanXiaomiCard extends LitElement {
* the fan when animations/fanbox is disabled.
*/
private toggleSpeedLevel(): void {
const speedLevel = this.getSpeedLevel();
const currentLevel = this.getSpeedLevel();

let newSpeed: string | 0;
if (this.config.use_standard_speeds || this.config.platform === "default") {
newSpeed =
speedLevel >= this.supportedAttributes.speedList.length ? 0 : this.supportedAttributes.speedList[speedLevel];
} else {
newSpeed = speedLevel >= this.supportedAttributes.speedLevels ? 0 : `Level ${speedLevel + 1}`;
}
const newLevel = currentLevel >= this.supportedAttributes.speedLevels ? 0 : currentLevel + 1;
const newPercentage = (newLevel / this.supportedAttributes.speedLevels) * 100;

if (newSpeed === 0) {
this.hass.callService("fan", "turn_off", {
entity_id: this.config.entity,
});
} else {
this.hass.callService("fan", "set_speed", {
entity_id: this.config.entity,
speed: newSpeed,
});
}
this.hass.callService("fan", "set_percentage", {
entity_id: this.config.entity,
percentage: newPercentage,
});
}

private increaseSpeed() {
Expand Down

0 comments on commit 8b22cb7

Please sign in to comment.