Skip to content
Merged
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
31 changes: 0 additions & 31 deletions src/cards/ha-badges-card.js

This file was deleted.

45 changes: 45 additions & 0 deletions src/cards/ha-badges-card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { TemplateResult, html } from "lit-html";
import { customElement, LitElement, property } from "lit-element";
import { HassEntity } from "home-assistant-js-websocket";

import "../components/entity/ha-state-label-badge";

import { HomeAssistant } from "../types";
import { fireEvent } from "../common/dom/fire_event";

@customElement("ha-badges-card")
class HaBadgesCard extends LitElement {
@property() public hass?: HomeAssistant;
@property() public states?: HassEntity[];

protected render(): TemplateResult | void {
if (!this.hass || !this.states) {
return html``;
}

return html`
${this.states.map(
(state) => html`
<ha-state-label-badge
.hass=${this.hass}
.state=${state}
@click=${this._handleClick}
></ha-state-label-badge>
`
)}
`;
}

private _handleClick(ev: Event) {
const entityId = ((ev.target as any).state as HassEntity).entity_id;
fireEvent(this, "hass-more-info", {
entityId,
});
}
}

declare global {
interface HTMLElementTagNameMap {
"ha-badges-card": HaBadgesCard;
}
}
11 changes: 0 additions & 11 deletions src/components/entity/ha-state-label-badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {

import { HassEntity } from "home-assistant-js-websocket";
import { classMap } from "lit-html/directives/class-map";
import { fireEvent } from "../../common/dom/fire_event";
import { HomeAssistant } from "../../types";

import { computeStateDomain } from "../../common/entity/compute_state_domain";
Expand Down Expand Up @@ -90,16 +89,6 @@ export class HaStateLabelBadge extends LitElement {
`;
}

protected firstUpdated(changedProperties: PropertyValues): void {
super.firstUpdated(changedProperties);
this.addEventListener("click", (ev) => {
ev.stopPropagation();
if (this.state) {
fireEvent(this, "hass-more-info", { entityId: this.state.entity_id });
}
});
}

protected updated(changedProperties: PropertyValues): void {
super.updated(changedProperties);

Expand Down
21 changes: 21 additions & 0 deletions src/panels/lovelace/badges/hui-state-label-badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import { LovelaceBadge } from "../types";
import { HomeAssistant } from "../../../types";
import { computeStateName } from "../../../common/entity/compute_state_name";
import { StateLabelBadgeConfig } from "./types";
import { longPress } from "../common/directives/long-press-directive";
import { hasDoubleClick } from "../common/has-double-click";
import { handleClick } from "../common/handle-click";

@customElement("hui-state-label-badge")
export class HuiStateLabelBadge extends LitElement implements LovelaceBadge {
Expand Down Expand Up @@ -41,9 +44,27 @@ export class HuiStateLabelBadge extends LitElement implements LovelaceBadge {
: ""}
.icon=${this._config.icon}
.image=${this._config.image}
@ha-click=${this._handleClick}
@ha-hold=${this._handleHold}
@ha-dblclick=${this._handleDblClick}
.longPress=${longPress({
hasDoubleClick: hasDoubleClick(this._config!.double_tap_action),
})}
></ha-state-label-badge>
`;
}

private _handleClick() {
handleClick(this, this.hass!, this._config!, false, false);
}

private _handleHold() {
handleClick(this, this.hass!, this._config!, true, false);
}

private _handleDblClick() {
handleClick(this, this.hass!, this._config!, false, true);
}
}

declare global {
Expand Down
5 changes: 4 additions & 1 deletion src/panels/lovelace/badges/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LovelaceBadgeConfig } from "../../../data/lovelace";
import { LovelaceBadgeConfig, ActionConfig } from "../../../data/lovelace";
import { EntityFilterEntityConfig } from "../entity-rows/types";

export interface EntityFilterBadgeConfig extends LovelaceBadgeConfig {
Expand All @@ -16,4 +16,7 @@ export interface StateLabelBadgeConfig extends LovelaceBadgeConfig {
name?: string;
icon?: string;
image?: string;
tap_action?: ActionConfig;
hold_action?: ActionConfig;
double_tap_action?: ActionConfig;
}
21 changes: 21 additions & 0 deletions src/panels/lovelace/elements/hui-state-badge-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { computeStateName } from "../../../common/entity/compute_state_name";
import { LovelaceElement, StateBadgeElementConfig } from "./types";
import { HomeAssistant } from "../../../types";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import { longPress } from "../common/directives/long-press-directive";
import { hasDoubleClick } from "../common/has-double-click";
import { handleClick } from "../common/handle-click";

@customElement("hui-state-badge-element")
export class HuiStateBadgeElement extends LitElement
Expand Down Expand Up @@ -61,9 +64,27 @@ export class HuiStateBadgeElement extends LitElement
: this._config.title === null
? ""
: this._config.title}"
@ha-click=${this._handleClick}
@ha-hold=${this._handleHold}
@ha-dblclick=${this._handleDblClick}
.longPress=${longPress({
hasDoubleClick: hasDoubleClick(this._config!.double_tap_action),
})}
></ha-state-label-badge>
`;
}

private _handleClick() {
handleClick(this, this.hass!, this._config!, false, false);
}

private _handleHold() {
handleClick(this, this.hass!, this._config!, true, false);
}

private _handleDblClick() {
handleClick(this, this.hass!, this._config!, false, true);
}
}

declare global {
Expand Down
1 change: 0 additions & 1 deletion src/panels/lovelace/elements/hui-state-label-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
PropertyValues,
} from "lit-element";

import "../../../components/entity/ha-state-label-badge";
import "../components/hui-warning-element";

import { computeStateDisplay } from "../../../common/entity/compute_state_display";
Expand Down
3 changes: 3 additions & 0 deletions src/panels/lovelace/elements/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export interface ServiceButtonElementConfig extends LovelaceElementConfig {
export interface StateBadgeElementConfig extends LovelaceElementConfig {
entity: string;
title?: string;
tap_action?: ActionConfig;
hold_action?: ActionConfig;
double_tap_action?: ActionConfig;
}

export interface StateIconElementConfig extends LovelaceElementConfig {
Expand Down