Skip to content

Commit

Permalink
Add option to select preset list
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Sep 21, 2023
1 parent f1dc50f commit 8402587
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { HassEntity } from "home-assistant-js-websocket";
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 { FormatEntityAttributeValueFunc } from "../../../../common/translations/entity-state";
import { LocalizeFunc } from "../../../../common/translations/localize";
import "../../../../components/ha-form/ha-form";
import type {
Expand Down Expand Up @@ -31,7 +33,11 @@ export class HuiClimatePresetModesTileFeatureEditor
}

private _schema = memoizeOne(
(localize: LocalizeFunc) =>
(
localize: LocalizeFunc,
formatEntityAttributeValue: FormatEntityAttributeValueFunc,
stateObj?: HassEntity
) =>
[
{
name: "style",
Expand All @@ -48,6 +54,24 @@ export class HuiClimatePresetModesTileFeatureEditor
},
},
},
{
name: "preset_modes",
selector: {
select: {
multiple: true,
mode: "list",
options:
stateObj?.attributes.preset_modes?.map((mode) => ({
value: mode,
label: formatEntityAttributeValue(
stateObj,
"preset_mode",
mode
),
})) || [],
},
},
},
] as const satisfies readonly HaFormSchema[]
);

Expand All @@ -56,12 +80,21 @@ export class HuiClimatePresetModesTileFeatureEditor
return nothing;
}

const stateObj = this.context?.entity_id
? this.hass.states[this.context?.entity_id]
: undefined;

const data: ClimatePresetModesTileFeatureConfig = {
style: "dropdown",
preset_modes: [],
...this._config,
};

const schema = this._schema(this.hass.localize);
const schema = this._schema(
this.hass.localize,
this.hass.formatEntityAttributeValue,
stateObj
);

return html`
<ha-form
Expand All @@ -83,13 +116,12 @@ export class HuiClimatePresetModesTileFeatureEditor
) => {
switch (schema.name) {
case "style":
case "preset_modes":
return this.hass!.localize(
`ui.panel.lovelace.editor.card.tile.features.types.climate-preset-modes.${schema.name}`
);
default:
return this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
);
return "";
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { HomeAssistant } from "../../../../types";
import { getTileFeatureElementClass } from "../../create-element/create-tile-feature-element";
import { supportsAlarmModesTileFeature } from "../../tile-features/hui-alarm-modes-tile-feature";
import { supportsClimateHvacModesTileFeature } from "../../tile-features/hui-climate-hvac-modes-tile-feature";
import { supportsClimatePresetsTileFeature } from "../../tile-features/hui-climate-presets-tile-feature";
import { supportsCoverOpenCloseTileFeature } from "../../tile-features/hui-cover-open-close-tile-feature";
import { supportsCoverPositionTileFeature } from "../../tile-features/hui-cover-position-tile-feature";
import { supportsCoverTiltPositionTileFeature } from "../../tile-features/hui-cover-tilt-position-tile-feature";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ class HuiClimatePresetModeTileFeature
@query("ha-control-select-menu", true)
private _haSelect?: HaControlSelectMenu;

static getStubConfig(): ClimatePresetModesTileFeatureConfig {
static getStubConfig(
_,
stateObj?: HassEntity
): ClimatePresetModesTileFeatureConfig {
return {
type: "climate-preset-modes",
style: "dropdown",
preset_modes: stateObj?.attributes.preset_modes || [],
};
}

Expand Down Expand Up @@ -124,15 +129,17 @@ class HuiClimatePresetModeTileFeature

const modes = stateObj.attributes.preset_modes || [];

const options = modes.map<ControlSelectOption>((mode) => ({
value: mode,
label: this.hass!.formatEntityAttributeValue(
this.stateObj!,
"preset_mode",
mode
),
path: computePresetModeIcon(mode),
}));
const options = modes
.filter((mode) => (this._config!.preset_modes || []).includes(mode))
.map<ControlSelectOption>((mode) => ({
value: mode,
label: this.hass!.formatEntityAttributeValue(
this.stateObj!,
"preset_mode",
mode
),
path: computePresetModeIcon(mode),
}));

if (this._config.style === "icons") {
return html`
Expand Down
1 change: 1 addition & 0 deletions src/panels/lovelace/tile-features/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface ClimateHvacModesTileFeatureConfig {
export interface ClimatePresetModesTileFeatureConfig {
type: "climate-preset-modes";
style?: "dropdown" | "icons";
preset_modes?: string[];
}

export interface SelectOptionsTileFeatureConfig {
Expand Down
3 changes: 2 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5044,7 +5044,8 @@
"style_list": {
"dropdown": "Dropdown",
"icons": "Icons"
}
},
"preset_modes": "Preset modes"
},
"target-temperature": {
"label": "Target temperature"
Expand Down

0 comments on commit 8402587

Please sign in to comment.