|
| 1 | +import { mdiArrowOscillating } from "@mdi/js"; |
| 2 | +import type { HassEntity } from "home-assistant-js-websocket"; |
| 3 | +import type { PropertyValues, TemplateResult } from "lit"; |
| 4 | +import { html, LitElement } from "lit"; |
| 5 | +import { customElement, property, query, state } from "lit/decorators"; |
| 6 | +import { stopPropagation } from "../../../common/dom/stop_propagation"; |
| 7 | +import { computeDomain } from "../../../common/entity/compute_domain"; |
| 8 | +import { supportsFeature } from "../../../common/entity/supports-feature"; |
| 9 | +import "../../../components/ha-attribute-icon"; |
| 10 | +import "../../../components/ha-control-select"; |
| 11 | +import type { ControlSelectOption } from "../../../components/ha-control-select"; |
| 12 | +import "../../../components/ha-control-select-menu"; |
| 13 | +import type { HaControlSelectMenu } from "../../../components/ha-control-select-menu"; |
| 14 | +import type { ClimateEntity } from "../../../data/climate"; |
| 15 | +import { ClimateEntityFeature } from "../../../data/climate"; |
| 16 | +import { UNAVAILABLE } from "../../../data/entity"; |
| 17 | +import type { HomeAssistant } from "../../../types"; |
| 18 | +import type { LovelaceCardFeature, LovelaceCardFeatureEditor } from "../types"; |
| 19 | +import { cardFeatureStyles } from "./common/card-feature-styles"; |
| 20 | +import { filterModes } from "./common/filter-modes"; |
| 21 | +import type { ClimateSwingHorizontalModesCardFeatureConfig } from "./types"; |
| 22 | + |
| 23 | +export const supportsClimateSwingHorizontalModesCardFeature = ( |
| 24 | + stateObj: HassEntity |
| 25 | +) => { |
| 26 | + const domain = computeDomain(stateObj.entity_id); |
| 27 | + return ( |
| 28 | + domain === "climate" && |
| 29 | + supportsFeature(stateObj, ClimateEntityFeature.SWING_HORIZONTAL_MODE) |
| 30 | + ); |
| 31 | +}; |
| 32 | + |
| 33 | +@customElement("hui-climate-swing-horizontal-modes-card-feature") |
| 34 | +class HuiClimateSwingHorizontalModesCardFeature |
| 35 | + extends LitElement |
| 36 | + implements LovelaceCardFeature |
| 37 | +{ |
| 38 | + @property({ attribute: false }) public hass?: HomeAssistant; |
| 39 | + |
| 40 | + @property({ attribute: false }) public stateObj?: ClimateEntity; |
| 41 | + |
| 42 | + @state() private _config?: ClimateSwingHorizontalModesCardFeatureConfig; |
| 43 | + |
| 44 | + @state() _currentSwingHorizontalMode?: string; |
| 45 | + |
| 46 | + @query("ha-control-select-menu", true) |
| 47 | + private _haSelect?: HaControlSelectMenu; |
| 48 | + |
| 49 | + static getStubConfig(): ClimateSwingHorizontalModesCardFeatureConfig { |
| 50 | + return { |
| 51 | + type: "climate-swing-horizontal-modes", |
| 52 | + style: "dropdown", |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + public static async getConfigElement(): Promise<LovelaceCardFeatureEditor> { |
| 57 | + await import( |
| 58 | + "../editor/config-elements/hui-climate-swing-horizontal-modes-card-feature-editor" |
| 59 | + ); |
| 60 | + return document.createElement( |
| 61 | + "hui-climate-swing-horizontal-modes-card-feature-editor" |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public setConfig(config: ClimateSwingHorizontalModesCardFeatureConfig): void { |
| 66 | + if (!config) { |
| 67 | + throw new Error("Invalid configuration"); |
| 68 | + } |
| 69 | + this._config = config; |
| 70 | + } |
| 71 | + |
| 72 | + protected willUpdate(changedProp: PropertyValues): void { |
| 73 | + super.willUpdate(changedProp); |
| 74 | + if (changedProp.has("stateObj") && this.stateObj) { |
| 75 | + this._currentSwingHorizontalMode = |
| 76 | + this.stateObj.attributes.swing_horizontal_mode; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + protected updated(changedProps: PropertyValues) { |
| 81 | + super.updated(changedProps); |
| 82 | + if (this._haSelect && changedProps.has("hass")) { |
| 83 | + const oldHass = changedProps.get("hass") as HomeAssistant | undefined; |
| 84 | + if ( |
| 85 | + this.hass && |
| 86 | + this.hass.formatEntityAttributeValue !== |
| 87 | + oldHass?.formatEntityAttributeValue |
| 88 | + ) { |
| 89 | + this._haSelect.layoutOptions(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private async _valueChanged(ev: CustomEvent) { |
| 95 | + const swingHorizontalMode = |
| 96 | + (ev.detail as any).value ?? ((ev.target as any).value as string); |
| 97 | + |
| 98 | + const oldSwingHorizontalMode = |
| 99 | + this.stateObj!.attributes.swing_horizontal_mode; |
| 100 | + |
| 101 | + if (swingHorizontalMode === oldSwingHorizontalMode) return; |
| 102 | + |
| 103 | + this._currentSwingHorizontalMode = swingHorizontalMode; |
| 104 | + |
| 105 | + try { |
| 106 | + await this._setMode(swingHorizontalMode); |
| 107 | + } catch (err) { |
| 108 | + this._currentSwingHorizontalMode = oldSwingHorizontalMode; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private async _setMode(mode: string) { |
| 113 | + await this.hass!.callService("climate", "set_swing_horizontal_mode", { |
| 114 | + entity_id: this.stateObj!.entity_id, |
| 115 | + swing_horizontal_mode: mode, |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + protected render(): TemplateResult | null { |
| 120 | + if ( |
| 121 | + !this._config || |
| 122 | + !this.hass || |
| 123 | + !this.stateObj || |
| 124 | + !supportsClimateSwingHorizontalModesCardFeature(this.stateObj) |
| 125 | + ) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + const stateObj = this.stateObj; |
| 130 | + |
| 131 | + const options = filterModes( |
| 132 | + stateObj.attributes.swing_horizontal_modes, |
| 133 | + this._config!.swing_horizontal_modes |
| 134 | + ).map<ControlSelectOption>((mode) => ({ |
| 135 | + value: mode, |
| 136 | + label: this.hass!.formatEntityAttributeValue( |
| 137 | + this.stateObj!, |
| 138 | + "swing_horizontal_mode", |
| 139 | + mode |
| 140 | + ), |
| 141 | + icon: html`<ha-attribute-icon |
| 142 | + slot="graphic" |
| 143 | + .hass=${this.hass} |
| 144 | + .stateObj=${stateObj} |
| 145 | + attribute="swing_horizontal_mode" |
| 146 | + .attributeValue=${mode} |
| 147 | + ></ha-attribute-icon>`, |
| 148 | + })); |
| 149 | + |
| 150 | + if (this._config.style === "icons") { |
| 151 | + return html` |
| 152 | + <ha-control-select |
| 153 | + .options=${options} |
| 154 | + .value=${this._currentSwingHorizontalMode} |
| 155 | + @value-changed=${this._valueChanged} |
| 156 | + hide-label |
| 157 | + .ariaLabel=${this.hass!.formatEntityAttributeName( |
| 158 | + stateObj, |
| 159 | + "swing_horizontal_mode" |
| 160 | + )} |
| 161 | + .disabled=${this.stateObj!.state === UNAVAILABLE} |
| 162 | + > |
| 163 | + </ha-control-select> |
| 164 | + `; |
| 165 | + } |
| 166 | + |
| 167 | + return html` |
| 168 | + <ha-control-select-menu |
| 169 | + show-arrow |
| 170 | + hide-label |
| 171 | + .label=${this.hass!.formatEntityAttributeName( |
| 172 | + stateObj, |
| 173 | + "swing_horizontal_mode" |
| 174 | + )} |
| 175 | + .value=${this._currentSwingHorizontalMode} |
| 176 | + .disabled=${this.stateObj.state === UNAVAILABLE} |
| 177 | + fixedMenuPosition |
| 178 | + naturalMenuWidth |
| 179 | + @selected=${this._valueChanged} |
| 180 | + @closed=${stopPropagation} |
| 181 | + > |
| 182 | + ${this._currentSwingHorizontalMode |
| 183 | + ? html`<ha-attribute-icon |
| 184 | + slot="icon" |
| 185 | + .hass=${this.hass} |
| 186 | + .stateObj=${stateObj} |
| 187 | + attribute="swing_horizontal_mode" |
| 188 | + .attributeValue=${this._currentSwingHorizontalMode} |
| 189 | + ></ha-attribute-icon>` |
| 190 | + : html` <ha-svg-icon |
| 191 | + slot="icon" |
| 192 | + .path=${mdiArrowOscillating} |
| 193 | + ></ha-svg-icon>`} |
| 194 | + ${options.map( |
| 195 | + (option) => html` |
| 196 | + <ha-list-item .value=${option.value} graphic="icon"> |
| 197 | + ${option.icon}${option.label} |
| 198 | + </ha-list-item> |
| 199 | + ` |
| 200 | + )} |
| 201 | + </ha-control-select-menu> |
| 202 | + `; |
| 203 | + } |
| 204 | + |
| 205 | + static get styles() { |
| 206 | + return cardFeatureStyles; |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +declare global { |
| 211 | + interface HTMLElementTagNameMap { |
| 212 | + "hui-climate-swing-horizontal-modes-card-feature": HuiClimateSwingHorizontalModesCardFeature; |
| 213 | + } |
| 214 | +} |
0 commit comments