-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa0419e
commit b538270
Showing
6 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
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
90 changes: 90 additions & 0 deletions
90
src/panels/lovelace/editor/config-elements/hui-number-tile-feature-editor.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,90 @@ | ||
import { html, LitElement, nothing } from "lit"; | ||
import { customElement, property, state } from "lit/decorators"; | ||
import memoizeOne from "memoize-one"; | ||
import { fireEvent } from "../../../../common/dom/fire_event"; | ||
import "../../../../components/ha-form/ha-form"; | ||
import type { SchemaUnion } from "../../../../components/ha-form/types"; | ||
import type { HomeAssistant } from "../../../../types"; | ||
import { | ||
NumberTileFeatureConfig, | ||
LovelaceTileFeatureContext, | ||
} from "../../tile-features/types"; | ||
import type { LovelaceTileFeatureEditor } from "../../types"; | ||
import { LocalizeFunc } from "../../../../common/translations/localize"; | ||
|
||
@customElement("hui-number-tile-feature-editor") | ||
export class HuiNumberTileFeatureEditor | ||
extends LitElement | ||
implements LovelaceTileFeatureEditor | ||
{ | ||
@property({ attribute: false }) public hass?: HomeAssistant; | ||
|
||
@property({ attribute: false }) public context?: LovelaceTileFeatureContext; | ||
|
||
@state() private _config?: NumberTileFeatureConfig; | ||
|
||
public setConfig(config: NumberTileFeatureConfig): void { | ||
this._config = config; | ||
} | ||
|
||
private _schema = memoizeOne( | ||
(localize: LocalizeFunc) => | ||
[ | ||
{ | ||
name: "style", | ||
selector: { | ||
select: { | ||
multiple: false, | ||
mode: "list", | ||
options: ["slider", "buttons"].map((mode) => ({ | ||
value: mode, | ||
label: localize( | ||
`ui.panel.lovelace.editor.card.tile.features.types.number.style_list.${mode}` | ||
), | ||
})), | ||
}, | ||
}, | ||
}, | ||
] as const | ||
); | ||
|
||
protected render() { | ||
if (!this.hass || !this._config) { | ||
return nothing; | ||
} | ||
|
||
const data: NumberTileFeatureConfig = { | ||
style: "buttons", | ||
...this._config, | ||
}; | ||
|
||
const schema = this._schema(this.hass.localize); | ||
|
||
return html` | ||
<ha-form | ||
.hass=${this.hass} | ||
.data=${data} | ||
.schema=${schema} | ||
.computeLabel=${this._computeLabelCallback} | ||
@value-changed=${this._valueChanged} | ||
></ha-form> | ||
`; | ||
} | ||
|
||
private _valueChanged(ev: CustomEvent): void { | ||
fireEvent(this, "config-changed", { config: ev.detail.value }); | ||
} | ||
|
||
private _computeLabelCallback = ( | ||
schema: SchemaUnion<ReturnType<typeof this._schema>> | ||
) => | ||
this.hass!.localize( | ||
`ui.panel.lovelace.editor.card.tile.features.types.number.${schema.name}` | ||
); | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-number-tile-feature-editor": HuiNumberTileFeatureEditor; | ||
} | ||
} |
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
121 changes: 121 additions & 0 deletions
121
src/panels/lovelace/tile-features/hui-number-tile-feature.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,121 @@ | ||
import { HassEntity } from "home-assistant-js-websocket"; | ||
import { css, html, LitElement, nothing, PropertyValues } from "lit"; | ||
import { customElement, property, state } from "lit/decorators"; | ||
import { computeDomain } from "../../../common/entity/compute_domain"; | ||
import { isUnavailableState } from "../../../data/entity"; | ||
import { HomeAssistant } from "../../../types"; | ||
import { LovelaceTileFeature, LovelaceTileFeatureEditor } from "../types"; | ||
import { NumberTileFeatureConfig } from "./types"; | ||
import "../../../components/ha-control-button"; | ||
import "../../../components/ha-control-button-group"; | ||
import "../../../components/ha-control-number-buttons"; | ||
import "../../../components/ha-control-slider"; | ||
import "../../../components/ha-icon"; | ||
|
||
export const supportsNumberTileFeature = (stateObj: HassEntity) => { | ||
const domain = computeDomain(stateObj.entity_id); | ||
return domain === "input_number"; | ||
}; | ||
|
||
@customElement("hui-number-tile-feature") | ||
class HuiNumberTileFeature extends LitElement implements LovelaceTileFeature { | ||
@property({ attribute: false }) public hass?: HomeAssistant; | ||
|
||
@property({ attribute: false }) public stateObj?: HassEntity; | ||
|
||
@state() private _config?: NumberTileFeatureConfig; | ||
|
||
@state() _currentState?: string; | ||
|
||
static getStubConfig(): NumberTileFeatureConfig { | ||
return { | ||
type: "number", | ||
style: "buttons", | ||
}; | ||
} | ||
|
||
public static async getConfigElement(): Promise<LovelaceTileFeatureEditor> { | ||
await import("../editor/config-elements/hui-number-tile-feature-editor"); | ||
return document.createElement("hui-number-tile-feature-editor"); | ||
} | ||
|
||
public setConfig(config: NumberTileFeatureConfig): void { | ||
if (!config) { | ||
throw new Error("Invalid configuration"); | ||
} | ||
this._config = config; | ||
} | ||
|
||
protected willUpdate(changedProp: PropertyValues): void { | ||
super.willUpdate(changedProp); | ||
if (changedProp.has("stateObj") && this.stateObj) { | ||
this._currentState = this.stateObj.state; | ||
} | ||
} | ||
|
||
private async _setValue(ev: CustomEvent) { | ||
const stateObj = this.stateObj!; | ||
|
||
await this.hass!.callService("input_number", "set_value", { | ||
entity_id: stateObj.entity_id, | ||
value: ev.detail.value, | ||
}); | ||
} | ||
|
||
protected render() { | ||
if ( | ||
!this._config || | ||
!this.hass || | ||
!this.stateObj || | ||
!supportsNumberTileFeature(this.stateObj) | ||
) { | ||
return nothing; | ||
} | ||
|
||
const stateObj = this.stateObj; | ||
|
||
return html` | ||
<div class="container"> | ||
${this._config.style === "buttons" | ||
? html`<ha-control-number-buttons | ||
value=${stateObj.state} | ||
min=${stateObj.attributes.min} | ||
max=${stateObj.attributes.max} | ||
step=${stateObj.attributes.step} | ||
@value-changed=${this._setValue} | ||
.disabled=${isUnavailableState(stateObj.state)} | ||
></ha-control-number-buttons>` | ||
: html`<ha-control-slider | ||
value=${stateObj.state} | ||
min=${stateObj.attributes.min} | ||
max=${stateObj.attributes.max} | ||
step=${stateObj.attributes.step} | ||
@value-changed=${this._setValue} | ||
.disabled=${isUnavailableState(stateObj.state)} | ||
>test | ||
</ha-control-slider>`} | ||
</div> | ||
`; | ||
} | ||
|
||
static get styles() { | ||
return css` | ||
ha-control-number-buttons { | ||
width: auto; | ||
} | ||
ha-control-slider { | ||
--control-slider-color: var(--tile-color); | ||
} | ||
.container { | ||
padding: 0 12px 12px 12px; | ||
width: auto; | ||
} | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-number-tile-feature": HuiNumberTileFeature; | ||
} | ||
} |
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