Skip to content

Commit

Permalink
Add grid support to cover card
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Mar 5, 2024
1 parent 737219f commit 47190b7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
39 changes: 27 additions & 12 deletions src/cards/cover-card/cover-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ registerCustomCard({
});

@customElement(COVER_CARD_NAME)
export class CoverCard extends MushroomBaseCard implements LovelaceCard {
export class CoverCard
extends MushroomBaseCard<CoverCardConfig, CoverEntity>
implements LovelaceCard
{
public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import("./cover-card-editor");
return document.createElement(COVER_CARD_EDITOR_NAME) as LovelaceCardEditor;
Expand All @@ -66,12 +69,12 @@ export class CoverCard extends MushroomBaseCard implements LovelaceCard {
};
}

@state() private _config?: CoverCardConfig;
protected get hasControls(): boolean {
return this._controls.length > 0;
}

@state() private _activeControl?: CoverCardControl;

@state() private _controls: CoverCardControl[] = [];

get _nextControl(): CoverCardControl | undefined {
if (this._activeControl) {
return (
Expand All @@ -91,34 +94,46 @@ export class CoverCard extends MushroomBaseCard implements LovelaceCard {
}

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

private get _controls(): CoverCardControl[] {
if (!this._config || !this._stateObj) return [];
const controls: CoverCardControl[] = [];
if (this._config?.show_buttons_control) {
if (this._config.show_buttons_control) {
controls.push("buttons_control");
}
if (this._config?.show_position_control) {
if (this._config.show_position_control) {
controls.push("position_control");
}
if (this._config?.show_tilt_position_control) {
if (this._config.show_tilt_position_control) {
controls.push("tilt_position_control");
}
this._controls = controls;
this._activeControl = controls[0];
this.updatePosition();
return controls;
}

updateActiveControl() {
const isActiveControlSupported = this._activeControl
? this._controls.includes(this._activeControl)
: false;
this._activeControl = isActiveControlSupported ? this._activeControl : this._controls[0];
}

protected updated(changedProperties: PropertyValues) {
super.updated(changedProperties);
if (this.hass && changedProperties.has("hass")) {
this.updatePosition();
this.updateActiveControl();
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/cards/light-card/light-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ registerCustomCard({
});

@customElement(LIGHT_CARD_NAME)
export class LightCard extends MushroomBaseCard<LightCardConfig, LightEntity> implements LovelaceCard {
export class LightCard
extends MushroomBaseCard<LightCardConfig, LightEntity>
implements LovelaceCard
{
public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import("./light-card-editor");
return document.createElement(LIGHT_CARD_EDITOR_NAME) as LovelaceCardEditor;
Expand Down Expand Up @@ -106,7 +109,7 @@ export class LightCard extends MushroomBaseCard<LightCardConfig, LightEntity> im
},
...config,
});
this.updateActiveControls();
this.updateActiveControl();
this.updateBrightness();
}

Expand All @@ -118,7 +121,7 @@ export class LightCard extends MushroomBaseCard<LightCardConfig, LightEntity> im
protected updated(changedProperties: PropertyValues) {
super.updated(changedProperties);
if (this.hass && changedProperties.has("hass")) {
this.updateActiveControls();
this.updateActiveControl();
this.updateBrightness();
}
}
Expand All @@ -140,7 +143,7 @@ export class LightCard extends MushroomBaseCard<LightCardConfig, LightEntity> im
}
}

updateActiveControls() {
updateActiveControl() {
const isActiveControlSupported = this._activeControl
? this._controls.includes(this._activeControl)
: false;
Expand Down
6 changes: 3 additions & 3 deletions src/cards/media-player-card/media-player-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ export class MediaPlayerCard

setConfig(config: MediaPlayerCardConfig): void {
super.setConfig(config);
this.updateActiveControls();
this.updateActiveControl();
this.updateVolume();
}

protected updated(changedProperties: PropertyValues) {
super.updated(changedProperties);
if (this.hass && changedProperties.has("hass")) {
this.updateActiveControls();
this.updateActiveControl();
this.updateVolume();
}
}
Expand All @@ -139,7 +139,7 @@ export class MediaPlayerCard
}
}

updateActiveControls() {
updateActiveControl() {
const isActiveControlSupported = this._activeControl
? this._controls.includes(this._activeControl)
: false;
Expand Down

0 comments on commit 47190b7

Please sign in to comment.