Skip to content

Commit

Permalink
Add logic in base card
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Mar 5, 2024
1 parent 9e12106 commit 3e2155c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
12 changes: 3 additions & 9 deletions src/cards/entity-card/entity-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ registerCustomCard({
});

@customElement(ENTITY_CARD_NAME)
export class EntityCard extends MushroomBaseCard implements LovelaceCard {
export class EntityCard extends MushroomBaseCard<EntityCardConfig> implements LovelaceCard {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import("./entity-card-editor");
return document.createElement(ENTITY_CARD_EDITOR_NAME) as LovelaceCardEditor;
Expand All @@ -50,22 +50,16 @@ export class EntityCard extends MushroomBaseCard implements LovelaceCard {
};
}

@state() private _config?: EntityCardConfig;

getCardSize(): number | Promise<number> {
return 1;
}

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

private _handleAction(ev: ActionHandlerEvent) {
Expand Down
31 changes: 7 additions & 24 deletions src/cards/light-card/light-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ registerCustomCard({
});

@customElement(LIGHT_CARD_NAME)
export class LightCard extends MushroomBaseCard implements LovelaceCard {
export class LightCard extends MushroomBaseCard<LightCardConfig> implements LovelaceCard {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import("./light-card-editor");
return document.createElement(LIGHT_CARD_EDITOR_NAME) as LovelaceCardEditor;
Expand All @@ -74,14 +74,10 @@ export class LightCard extends MushroomBaseCard implements LovelaceCard {
};
}

@state() private _config?: LightCardConfig;

@state() private _activeControl?: LightCardControl;

@state() private brightness?: number;

@state() private _inGrid = false;

private get _controls(): LightCardControl[] {
if (!this._config || !this.hass || !this._config.entity) return [];

Expand All @@ -102,43 +98,30 @@ export class LightCard extends MushroomBaseCard implements LovelaceCard {
return controls;
}

public getCardSize(): number | Promise<number> {
return 1;
}

public getGridSize(): [number, number] {
this._inGrid = true;
let column = 2;
let row = 1;
if (!this._config) return [column, row];

const size = super.getGridSize();
if (!this._config) return size;
const appearance = computeAppearance(this._config);
if (appearance.layout === "vertical") {
row += 1;
}
if (appearance.layout === "horizontal") {
column = 4;
}
if (
this._controls.length &&
!this._config?.collapsible_controls &&
appearance.layout !== "horizontal"
) {
row += 1;
size[1] += 1;
}
return [column, row];
return size;
}

setConfig(config: LightCardConfig): void {
this._config = {
super.setConfig({
tap_action: {
action: "toggle",
},
hold_action: {
action: "more-info",
},
...config,
};
});
this.updateActiveControls();
this.updateBrightness();
}
Expand Down
32 changes: 31 additions & 1 deletion src/utils/base-card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HassEntity } from "home-assistant-js-websocket";
import { html, nothing, TemplateResult } from "lit";
import { property, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { computeRTL, computeStateDisplay, HomeAssistant, isActive, isAvailable } from "../ha";
import setupCustomlocalize from "../localize";
Expand All @@ -21,7 +22,36 @@ export function computeDarkMode(hass?: HomeAssistant): boolean {
if (!hass) return false;
return (hass.themes as any).darkMode as boolean;
}
export class MushroomBaseCard extends MushroomBaseElement {
export class MushroomBaseCard<T extends BaseConfig = BaseConfig> extends MushroomBaseElement {
@state() protected _config?: T;

@property({ attribute: "in-grid", reflect: true, type: Boolean })
protected _inGrid = false;

public getCardSize(): number | Promise<number> {
return 1;
}

setConfig(config: T): void {
this._config = config;
}

public getGridSize(): [number, number] {
this._inGrid = true;
let column = 2;
let row = 1;
if (!this._config) return [column, row];

const appearance = computeAppearance(this._config);
if (appearance.layout === "vertical") {
row += 1;
}
if (appearance.layout === "horizontal") {
column = 4;
}
return [column, row];
}

protected renderPicture(picture: string): TemplateResult {
return html`
<mushroom-shape-avatar
Expand Down
4 changes: 2 additions & 2 deletions src/utils/card-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export const cardStyle = css`
ha-card.fill-container {
height: 100%;
}
ha-card.in-grid {
:host([in-grid]) ha-card {
height: 100%;
}
ha-card.in-grid mushroom-card {
:host([in-grid]) ha-card mushroom-card {
height: 100%;
}
.actions {
Expand Down

0 comments on commit 3e2155c

Please sign in to comment.