Skip to content

Commit

Permalink
Add style and preset modes options to climate preset tile feature (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored Sep 21, 2023
1 parent e7960bf commit 90d01e4
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 175 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "../tile-features/hui-alarm-modes-tile-feature";
import "../tile-features/hui-climate-hvac-modes-tile-feature";
import "../tile-features/hui-climate-presets-tile-feature";
import "../tile-features/hui-climate-preset-modes-tile-feature";
import "../tile-features/hui-cover-open-close-tile-feature";
import "../tile-features/hui-cover-position-tile-feature";
import "../tile-features/hui-cover-tilt-position-tile-feature";
Expand All @@ -22,7 +22,7 @@ import {
const TYPES: Set<LovelaceTileFeatureConfig["type"]> = new Set([
"alarm-modes",
"climate-hvac-modes",
"climate-presets",
"climate-preset-modes",
"cover-open-close",
"cover-position",
"cover-tilt-position",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
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 {
HaFormSchema,
SchemaUnion,
} from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import {
ClimatePresetModesTileFeatureConfig,
LovelaceTileFeatureContext,
} from "../../tile-features/types";
import type { LovelaceTileFeatureEditor } from "../../types";

@customElement("hui-climate-preset-modes-tile-feature-editor")
export class HuiClimatePresetModesTileFeatureEditor
extends LitElement
implements LovelaceTileFeatureEditor
{
@property({ attribute: false }) public hass?: HomeAssistant;

@property({ attribute: false }) public context?: LovelaceTileFeatureContext;

@state() private _config?: ClimatePresetModesTileFeatureConfig;

public setConfig(config: ClimatePresetModesTileFeatureConfig): void {
this._config = config;
}

private _schema = memoizeOne(
(
localize: LocalizeFunc,
formatEntityAttributeValue: FormatEntityAttributeValueFunc,
stateObj?: HassEntity
) =>
[
{
name: "style",
selector: {
select: {
multiple: false,
mode: "list",
options: ["dropdown", "icons"].map((mode) => ({
value: mode,
label: localize(
`ui.panel.lovelace.editor.card.tile.features.types.climate-preset-modes.style_list.${mode}`
),
})),
},
},
},
{
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[]
);

protected render() {
if (!this.hass || !this._config) {
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,
this.hass.formatEntityAttributeValue,
stateObj
);

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>>
) => {
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 "";
}
};
}

declare global {
interface HTMLElementTagNameMap {
"hui-climate-preset-modes-tile-feature-editor": HuiClimatePresetModesTileFeatureEditor;
}
}
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 All @@ -41,14 +40,15 @@ import { supportsTargetTemperatureTileFeature } from "../../tile-features/hui-ta
import { supportsVacuumCommandTileFeature } from "../../tile-features/hui-vacuum-commands-tile-feature";
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";

type FeatureType = LovelaceTileFeatureConfig["type"];
type SupportsFeature = (stateObj: HassEntity) => boolean;

const FEATURE_TYPES: FeatureType[] = [
const UI_FEATURE_TYPES = [
"alarm-modes",
"climate-hvac-modes",
"climate-presets",
"climate-preset-modes",
"cover-open-close",
"cover-position",
"cover-tilt-position",
Expand All @@ -61,35 +61,39 @@ const FEATURE_TYPES: FeatureType[] = [
"target-temperature",
"vacuum-commands",
"water-heater-operation-modes",
];
] as const satisfies readonly FeatureType[];

const EDITABLES_FEATURE_TYPES = new Set<FeatureType>([
type UiFeatureTypes = (typeof UI_FEATURE_TYPES)[number];

const EDITABLES_FEATURE_TYPES = new Set<UiFeatureTypes>([
"vacuum-commands",
"alarm-modes",
"climate-hvac-modes",
"water-heater-operation-modes",
"lawn-mower-commands",
"climate-preset-modes",
]);

const SUPPORTS_FEATURE_TYPES: Record<FeatureType, SupportsFeature | undefined> =
{
"alarm-modes": supportsAlarmModesTileFeature,
"climate-hvac-modes": supportsClimateHvacModesTileFeature,
"climate-presets": supportsClimatePresetsTileFeature,
"cover-open-close": supportsCoverOpenCloseTileFeature,
"cover-position": supportsCoverPositionTileFeature,
"cover-tilt-position": supportsCoverTiltPositionTileFeature,
"cover-tilt": supportsCoverTiltTileFeature,
"fan-speed": supportsFanSpeedTileFeature,
"lawn-mower-commands": supportsLawnMowerCommandTileFeature,
"light-brightness": supportsLightBrightnessTileFeature,
"light-color-temp": supportsLightColorTempTileFeature,
"target-temperature": supportsTargetTemperatureTileFeature,
"vacuum-commands": supportsVacuumCommandTileFeature,
"water-heater-operation-modes":
supportsWaterHeaterOperationModesTileFeature,
"select-options": supportsSelectOptionTileFeature,
};
const SUPPORTS_FEATURE_TYPES: Record<
UiFeatureTypes,
SupportsFeature | undefined
> = {
"alarm-modes": supportsAlarmModesTileFeature,
"climate-hvac-modes": supportsClimateHvacModesTileFeature,
"climate-preset-modes": supportsClimatePresetModesTileFeature,
"cover-open-close": supportsCoverOpenCloseTileFeature,
"cover-position": supportsCoverPositionTileFeature,
"cover-tilt-position": supportsCoverTiltPositionTileFeature,
"cover-tilt": supportsCoverTiltTileFeature,
"fan-speed": supportsFanSpeedTileFeature,
"lawn-mower-commands": supportsLawnMowerCommandTileFeature,
"light-brightness": supportsLightBrightnessTileFeature,
"light-color-temp": supportsLightColorTempTileFeature,
"target-temperature": supportsTargetTemperatureTileFeature,
"vacuum-commands": supportsVacuumCommandTileFeature,
"water-heater-operation-modes": supportsWaterHeaterOperationModesTileFeature,
"select-options": supportsSelectOptionTileFeature,
};

const CUSTOM_FEATURE_ENTRIES: Record<
string,
Expand Down Expand Up @@ -181,7 +185,7 @@ export class HuiTileCardFeaturesEditor extends LitElement {
}

private _getSupportedFeaturesType() {
const featuresTypes = FEATURE_TYPES as string[];
const featuresTypes = UI_FEATURE_TYPES as readonly string[];
const customFeaturesTypes = customTileFeatures.map(
(feature) => `${CUSTOM_TYPE_PREFIX}${feature.type}`
);
Expand Down
Loading

0 comments on commit 90d01e4

Please sign in to comment.