Skip to content

Commit

Permalink
Add grid support to fan card
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Mar 5, 2024
1 parent 802365e commit 737219f
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/cards/fan-card/fan-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ registerCustomCard({
});

@customElement(FAN_CARD_NAME)
export class FanCard extends MushroomBaseCard implements LovelaceCard {
export class FanCard extends MushroomBaseCard<FanCardConfig> implements LovelaceCard {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import("./fan-card-editor");
return document.createElement(FAN_CARD_EDITOR_NAME) as LovelaceCardEditor;
Expand All @@ -56,22 +56,23 @@ export class FanCard extends MushroomBaseCard implements LovelaceCard {
};
}

@state() private _config?: FanCardConfig;

getCardSize(): number | Promise<number> {
return 1;
protected get hasControls(): boolean {
return (
Boolean(this._config?.show_percentage_control) ||
Boolean(this._config?.show_oscillate_control)
);
}

setConfig(config: FanCardConfig): void {
this._config = {
super.setConfig({
tap_action: {
action: "toggle",
},
hold_action: {
action: "more-info",
},
...config,
};
});
this.updatePercentage();
}

Expand All @@ -87,12 +88,8 @@ export class FanCard extends MushroomBaseCard implements LovelaceCard {

updatePercentage() {
this.percentage = undefined;
if (!this._config || !this.hass || !this._config.entity) return;

const entityId = this._config.entity;
const stateObj = this.hass.states[entityId] as HassEntity | undefined;

if (!stateObj) return;
const stateObj = this._stateObj;
if (!this._config || !this.hass || !stateObj) return;
this.percentage = getPercentage(stateObj);
}

Expand All @@ -111,8 +108,7 @@ export class FanCard extends MushroomBaseCard implements LovelaceCard {
return nothing;
}

const entityId = this._config.entity;
const stateObj = this.hass.states[entityId] as HassEntity | undefined;
const stateObj = this._stateObj;

if (!stateObj) {
return this.renderNotFound(this._config);
Expand Down

0 comments on commit 737219f

Please sign in to comment.