Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/fan auto control #170

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/cards/fan.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All the options are available in the lovelace editor but you can use `yaml` if y
| `hide_state` | boolean | `false` | Hide the entity state |
| `icon_animation` | boolean | `false` | Animate the icon when fan is `on` |
| `show_percentage_control` | boolean | `false` | Show a slider to control speed |
| `show_auto_control` | boolean | `false` | Show a button to set auto speed |
| `show_oscillate_control` | boolean | `false` | Show a button to control oscillation |
| `tap_action` | action | `toggle` | Home assistant action to perform on tap |
| `hold_action` | action | `more-info` | Home assistant action to perform on hold |
Expand Down
61 changes: 61 additions & 0 deletions src/cards/fan-card/controls/fan-auto-control.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { HomeAssistant } from "custom-card-helpers";
import { HassEntity } from "home-assistant-js-websocket";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import "../../../shared/slider";
import { isActive } from "../../../utils/entity";
import { isAuto, getPresetModes} from "../utils";

@customElement("mushroom-fan-auto-control")
export class FanPercentageControl extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) public entity!: HassEntity;

private _onTap(e: MouseEvent): void {
e.stopPropagation();
const auto = isAuto(this.entity);

if (!auto) {
const validAutoModes = ["auto", "smart", "woosh", "eco", "breeze"];
getPresetModes(this.entity).every(presetMode => {
if (validAutoModes.includes(presetMode.toLowerCase())) {
this.hass.callService("fan", "set_preset_mode", {
entity_id: this.entity.entity_id,
preset_mode: presetMode,
});
return false;
} else {
return true;
}
});
}
};

protected render(): TemplateResult {
const auto = isAuto(this.entity);
const active = isActive(this.entity);

return html`
<mushroom-button
class=${classMap({ active: auto})}
.icon=${"mdi:fan-auto"}
@click=${this._onTap}
.disabled=${!active}
/>
`;
}

static get styles(): CSSResultGroup {
return css`
:host {
display: flex;
}
mushroom-button.active {
--icon-color: rgb(var(--rgb-white));
--bg-color: rgb(var(--rgb-state-fan));
}
`;
}
}
2 changes: 2 additions & 0 deletions src/cards/fan-card/fan-card-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface FanCardConfig extends LovelaceCardConfig {
hide_state?: boolean;
icon_animation?: boolean;
show_percentage_control?: boolean;
show_auto_control?: boolean;
show_oscillate_control?: boolean;
tap_action?: ActionConfig;
hold_action?: ActionConfig;
Expand All @@ -28,6 +29,7 @@ export const fanCardConfigStruct = assign(
layout: optional(layoutStruct),
hide_state: optional(boolean()),
show_percentage_control: optional(boolean()),
show_auto_control: optional(boolean()),
show_oscillate_control: optional(boolean()),
tap_action: optional(actionConfigStruct),
hold_action: optional(actionConfigStruct),
Expand Down
10 changes: 10 additions & 0 deletions src/cards/fan-card/fan-card-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ export class FanCardEditor extends LitElement implements LovelaceCardEditor {
@change=${this._valueChanged}
></ha-switch>
</ha-formfield>
<ha-formfield
.label=${customLocalize("editor.card.fan.show_auto_control")}
.dir=${dir}
>
<ha-switch
.checked=${!!this._config.show_auto_control}
.configValue=${"show_auto_control"}
@change=${this._valueChanged}
></ha-switch>
</ha-formfield>
<ha-formfield
.label=${customLocalize("editor.card.fan.show_oscillate_control")}
.dir=${dir}
Expand Down
11 changes: 10 additions & 1 deletion src/cards/fan-card/fan-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { isActive, isAvailable } from "../../utils/entity";
import { stateIcon } from "../../utils/icons/state-icon";
import { getLayoutFromConfig } from "../../utils/layout";
import { FAN_CARD_EDITOR_NAME, FAN_CARD_NAME, FAN_ENTITY_DOMAINS } from "./const";
import "./controls/fan-auto-control";
import "./controls/fan-oscillate-control";
import "./controls/fan-percentage-control";
import { FanCardConfig } from "./fan-card-config";
Expand Down Expand Up @@ -146,7 +147,7 @@ export class FanCard extends LitElement implements LovelaceCard {
.secondary=${!hideState && stateValue}
></mushroom-state-info>
</mushroom-state-item>
${this._config.show_percentage_control || this._config.show_oscillate_control
${this._config.show_percentage_control || this._config.show_auto_control || this._config.show_oscillate_control
? html`
<div class="actions">
${this._config.show_percentage_control
Expand All @@ -157,6 +158,14 @@ export class FanCard extends LitElement implements LovelaceCard {
></mushroom-fan-percentage-control>
`
: null}
${this._config.show_auto_control
? html`
<mushroom-fan-auto-control
.hass=${this.hass}
.entity=${entity}
></mushroom-fan-auto-control>
`
: null}
${this._config.show_oscillate_control
? html`
<mushroom-fan-oscillate-control
Expand Down
9 changes: 9 additions & 0 deletions src/cards/fan-card/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ export function getPercentage(entity: HassEntity) {
: undefined;
}

export function getPresetModes(entity: HassEntity) {
return entity.attributes.preset_modes;
}

export function isAuto(entity: HassEntity) {
const validAutoModes = ["auto", "smart", "woosh", "eco", "breeze"];
return entity.attributes.preset_mode != null ? Boolean(validAutoModes.includes(entity.attributes.preset_mode.toLowerCase())) : false;
}

export function isOscillating(entity: HassEntity) {
return entity.attributes.oscillating != null ? Boolean(entity.attributes.oscillating) : false;
}
1 change: 1 addition & 0 deletions src/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"fan": {
"animate_icon_active": "Icon animieren, wenn aktiv?",
"show_percentage_control": "Prozentuale Kontrolle?",
"show_auto_control": "Automatische Drehzahlregelung?",
"show_oscillate_control": "Oszillationssteuerung?"
},
"cover": {
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"fan": {
"animate_icon_active": "Animate icon when active?",
"show_percentage_control": "Percentage control?",
"show_auto_control": "Auto Speed control?",
"show_oscillate_control": "Oscillate control?"
},
"cover": {
Expand Down
1 change: 1 addition & 0 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"fan": {
"animate_icon_active": "Animation de l'icône ?",
"show_percentage_control": "Contrôle de la vitesse ?",
"show_auto_control": "Contrôle automatique de la vitesse ?",
"show_oscillate_control": "Contrôle de l'oscillation ?"
},
"cover": {
Expand Down
1 change: 1 addition & 0 deletions src/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"fan": {
"animate_icon_active": "Animar ícone quando ativo?",
"show_percentage_control": "Mostrar controle de porcentagem?",
"show_auto_control": "Mostrar controle de automático de velocidade?",
"show_oscillate_control": "Mostrar controle de oscilação?"
},
"cover": {
Expand Down
1 change: 1 addition & 0 deletions src/translations/sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"fan": {
"animate_icon_active": "Animera ikonen när fläkten är på?",
"show_percentage_control": "Procentuell kontroll?",
"show_auto_control": "Kontroll för automatisk hastighet?",
"show_oscillate_control": "Kontroll för oscillera?"
},
"cover": {
Expand Down
1 change: 1 addition & 0 deletions src/translations/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"fan": {
"animate_icon_active": "激活时使用动态图标?",
"show_percentage_control": "百分比控制?",
"show_auto_control": "自动调速?",
"show_oscillate_control": "摆动控制?"
},
"cover": {
Expand Down