Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add number tile feature #18562

Merged
merged 7 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import "../tile-features/hui-select-options-tile-feature";
import "../tile-features/hui-target-temperature-tile-feature";
import "../tile-features/hui-vacuum-commands-tile-feature";
import "../tile-features/hui-water-heater-operation-modes-tile-feature";
import "../tile-features/hui-number-tile-feature";
import { LovelaceTileFeatureConfig } from "../tile-features/types";
import {
createLovelaceElement,
Expand All @@ -35,6 +36,7 @@ const TYPES: Set<LovelaceTileFeatureConfig["type"]> = new Set([
"target-temperature",
"vacuum-commands",
"water-heater-operation-modes",
"number",
]);

export const createTileFeatureElement = (config: LovelaceTileFeatureConfig) =>
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { supportsVacuumCommandTileFeature } from "../../tile-features/hui-vacuum
import { supportsWaterHeaterOperationModesTileFeature } from "../../tile-features/hui-water-heater-operation-modes-tile-feature";
import { LovelaceTileFeatureConfig } from "../../tile-features/types";
import { supportsClimatePresetModesTileFeature } from "../../tile-features/hui-climate-preset-modes-tile-feature";
import { supportsNumberTileFeature } from "../../tile-features/hui-number-tile-feature";

type FeatureType = LovelaceTileFeatureConfig["type"];
type SupportsFeature = (stateObj: HassEntity) => boolean;
Expand All @@ -58,6 +59,7 @@ const UI_FEATURE_TYPES = [
"target-temperature",
"vacuum-commands",
"water-heater-operation-modes",
"number",
] as const satisfies readonly FeatureType[];

type UiFeatureTypes = (typeof UI_FEATURE_TYPES)[number];
Expand All @@ -69,6 +71,7 @@ const EDITABLES_FEATURE_TYPES = new Set<UiFeatureTypes>([
"water-heater-operation-modes",
"lawn-mower-commands",
"climate-preset-modes",
"number",
]);

const SUPPORTS_FEATURE_TYPES: Record<
Expand All @@ -90,6 +93,7 @@ const SUPPORTS_FEATURE_TYPES: Record<
"vacuum-commands": supportsVacuumCommandTileFeature,
"water-heater-operation-modes": supportsWaterHeaterOperationModesTileFeature,
"select-options": supportsSelectOptionTileFeature,
number: supportsNumberTileFeature,
bramkragten marked this conversation as resolved.
Show resolved Hide resolved
};

const CUSTOM_FEATURE_ENTRIES: Record<
Expand Down
121 changes: 121 additions & 0 deletions src/panels/lovelace/tile-features/hui-number-tile-feature.ts
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" || domain === "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", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that for number domain the service that should be called is number.set_value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I just added that domain so I hadn't tested it yet, good catch.

entity_id: stateObj.entity_id,
value: ev.detail.value,
});
piitaya marked this conversation as resolved.
Show resolved Hide resolved
}

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)}
>
</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;
}
}
8 changes: 7 additions & 1 deletion src/panels/lovelace/tile-features/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export interface SelectOptionsTileFeatureConfig {
type: "select-options";
}

export interface NumberTileFeatureConfig {
type: "number";
style?: "buttons" | "slider";
}

export interface TargetTemperatureTileFeatureConfig {
type: "target-temperature";
}
Expand Down Expand Up @@ -98,7 +103,8 @@ export type LovelaceTileFeatureConfig =
| VacuumCommandsTileFeatureConfig
| TargetTemperatureTileFeatureConfig
| WaterHeaterOperationModesTileFeatureConfig
| SelectOptionsTileFeatureConfig;
| SelectOptionsTileFeatureConfig
| NumberTileFeatureConfig;

export type LovelaceTileFeatureContext = {
entity_id?: string;
Expand Down
8 changes: 8 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5207,6 +5207,14 @@
"select-options": {
"label": "Select options"
},
"number": {
"label": "Number",
"style": "Style",
"style_list": {
"buttons": "Buttons",
"slider": "Slider"
}
},
"target-temperature": {
"label": "Target temperature"
},
Expand Down