-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve element editor and migrate heading-entity editor (#22034)
* Extract load config element * Improve error by using ha-alert * Create hui-hase-editor * Migrate heading entity form to its own editor * Rename editor * Rename * Rename * Move heading entity to its own component * Fix default action for heading entity
- Loading branch information
Showing
19 changed files
with
532 additions
and
575 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/panels/lovelace/cards/heading/hui-heading-entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { CSSResultGroup, LitElement, css, html, nothing } from "lit"; | ||
import { customElement, property } from "lit/decorators"; | ||
import { ifDefined } from "lit/directives/if-defined"; | ||
import memoizeOne from "memoize-one"; | ||
import "../../../../components/ha-card"; | ||
import "../../../../components/ha-icon"; | ||
import "../../../../components/ha-icon-next"; | ||
import "../../../../components/ha-state-icon"; | ||
import { ActionHandlerEvent } from "../../../../data/lovelace/action_handler"; | ||
import "../../../../state-display/state-display"; | ||
import { HomeAssistant } from "../../../../types"; | ||
import { actionHandler } from "../../common/directives/action-handler-directive"; | ||
import { handleAction } from "../../common/handle-action"; | ||
import { hasAction } from "../../common/has-action"; | ||
import type { HeadingEntityConfig } from "../types"; | ||
|
||
@customElement("hui-heading-entity") | ||
export class HuiHeadingEntity extends LitElement { | ||
@property({ attribute: false }) public hass!: HomeAssistant; | ||
|
||
@property({ attribute: false }) public config!: HeadingEntityConfig | string; | ||
|
||
private _handleAction(ev: ActionHandlerEvent) { | ||
const config: HeadingEntityConfig = { | ||
tap_action: { | ||
action: "none", | ||
}, | ||
...this._config(this.config), | ||
}; | ||
handleAction(this, this.hass!, config, ev.detail.action!); | ||
} | ||
|
||
private _config = memoizeOne( | ||
(configOrString: HeadingEntityConfig | string): HeadingEntityConfig => { | ||
const config = | ||
typeof configOrString === "string" | ||
? { entity: configOrString } | ||
: configOrString; | ||
|
||
return { | ||
tap_action: { | ||
action: "none", | ||
}, | ||
...config, | ||
}; | ||
} | ||
); | ||
|
||
protected render() { | ||
const config = this._config(this.config); | ||
|
||
const stateObj = this.hass!.states[config.entity]; | ||
|
||
if (!stateObj) { | ||
return nothing; | ||
} | ||
|
||
const actionable = hasAction(config.tap_action); | ||
|
||
return html` | ||
<div | ||
class="entity" | ||
@action=${this._handleAction} | ||
.actionHandler=${actionHandler()} | ||
role=${ifDefined(actionable ? "button" : undefined)} | ||
tabindex=${ifDefined(actionable ? "0" : undefined)} | ||
> | ||
<ha-state-icon | ||
.hass=${this.hass} | ||
.icon=${config.icon} | ||
.stateObj=${stateObj} | ||
></ha-state-icon> | ||
<state-display | ||
.hass=${this.hass} | ||
.stateObj=${stateObj} | ||
.content=${config.content || "state"} | ||
></state-display> | ||
</div> | ||
`; | ||
} | ||
|
||
static get styles(): CSSResultGroup { | ||
return css` | ||
[role="button"] { | ||
cursor: pointer; | ||
} | ||
.entity { | ||
display: flex; | ||
flex-direction: row; | ||
white-space: nowrap; | ||
align-items: center; | ||
gap: 3px; | ||
color: var(--secondary-text-color); | ||
font-family: Roboto; | ||
font-size: 14px; | ||
font-style: normal; | ||
font-weight: 500; | ||
line-height: 20px; /* 142.857% */ | ||
letter-spacing: 0.1px; | ||
--mdc-icon-size: 14px; | ||
} | ||
.entity ha-state-icon { | ||
--ha-icon-display: block; | ||
} | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-heading-entity": HuiHeadingEntity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.