-
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.
Add style and preset modes options to climate preset tile feature (#1…
- Loading branch information
Showing
8 changed files
with
404 additions
and
175 deletions.
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
133 changes: 133 additions & 0 deletions
133
src/panels/lovelace/editor/config-elements/hui-climate-preset-modes-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,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; | ||
} | ||
} |
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.