Skip to content

Commit

Permalink
Case insensitive nature mode check for Issue #88
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-craig committed Jun 18, 2022
1 parent 7a78ee6 commit e21f1cf
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/xiaomi-fan-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,15 @@ export class FanXiaomiCard extends LitElement {
}
}

private getPresetMode() {
/**
* @returns The fan's preset (nature/normal) mode as a lowercase string.
*/
private getPresetMode(): "nature" | "normal" | undefined {
const attrs = this.hass.states[this.config.entity].attributes;
if (this.config.platform === "default") {
return attrs["preset_mode"];
return attrs["preset_mode"]?.toLowerCase();
}
return attrs["mode"];
return attrs["mode"]?.toLowerCase();
}

private setLed(on: boolean) {
Expand Down Expand Up @@ -401,7 +404,11 @@ export class FanXiaomiCard extends LitElement {
});
}

if (attributes.preset_mode && attributes.preset_modes && attributes.preset_modes.includes("Nature")) {
if (
attributes.preset_mode &&
attributes.preset_modes &&
attributes.preset_modes.some((m) => m.toLowerCase() === "nature")
) {
this.supportedAttributes.naturalSpeed = true;
}
}
Expand Down Expand Up @@ -636,7 +643,7 @@ export class FanXiaomiCard extends LitElement {
</div>`}
${this.supportedAttributes.naturalSpeed
? html`<div
class="op var-natural ${preset_mode === "Nature" ? "active" : ""}"
class="op var-natural ${preset_mode === "nature" ? "active" : ""}"
@click=${this.toggleNatureMode}
>
<button>
Expand Down Expand Up @@ -764,7 +771,7 @@ export class FanXiaomiCard extends LitElement {
}

private toggleNatureMode() {
const currentlyEnabled = this.getPresetMode() === "Nature";
const currentlyEnabled = this.getPresetMode() === "nature";
this.setPresetMode(currentlyEnabled ? "Normal" : "Nature");
}

Expand Down

0 comments on commit e21f1cf

Please sign in to comment.