Skip to content

Commit

Permalink
Add horizontal swing to climate (#22043)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST authored Nov 27, 2024
1 parent f9118a4 commit 988fa3e
Show file tree
Hide file tree
Showing 11 changed files with 489 additions and 6 deletions.
16 changes: 10 additions & 6 deletions gallery/src/pages/lovelace/thermostat-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ const ENTITIES = [
friendly_name: "Sensibo purifier",
fan_modes: ["low", "high"],
fan_mode: "low",
swing_modes: ["on", "off", "both", "vertical", "horizontal"],
swing_mode: "vertical",
supported_features: 41,
swing_modes: ["both", "rangefull", "off"],
swing_mode: "rangefull",
swing_horizontal_modes: ["both", "rangefull", "off"],
swing_horizontal_mode: "both",
supported_features: 553,
}),
getEntity("climate", "unavailable", "unavailable", {
supported_features: 43,
Expand Down Expand Up @@ -188,11 +190,13 @@ const CONFIGS = [
- type: climate-swing-modes
style: icons
swing_modes:
- 'on'
- 'both'
- 'rangefull'
- 'off'
swing_horizontal_modes:
- 'both'
- 'vertical'
- 'horizontal'
- 'rangefull'
- 'off'
`,
},
{
Expand Down
3 changes: 3 additions & 0 deletions gallery/src/pages/lovelace/tile-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,19 @@ const ENTITIES = [
fan_modes: ["on_low", "on_high", "auto_low", "auto_high", "off"],
preset_modes: ["home", "eco", "away"],
swing_modes: ["auto", "1", "2", "3", "off"],
switch_horizontal_modes: ["auto", "4", "5", "6", "off"],
current_temperature: 23,
target_temp_high: 24,
target_temp_low: 21,
fan_mode: "auto_low",
preset_mode: "home",
swing_mode: "auto",
swing_horizontal_mode: "off",
supported_features:
ClimateEntityFeature.TURN_ON +
ClimateEntityFeature.TURN_OFF +
ClimateEntityFeature.SWING_MODE +
ClimateEntityFeature.SWING_HORIZONTAL_MODE +
ClimateEntityFeature.PRESET_MODE +
ClimateEntityFeature.FAN_MODE +
ClimateEntityFeature.TARGET_TEMPERATURE_RANGE,
Expand Down
3 changes: 3 additions & 0 deletions src/data/climate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export type ClimateEntity = HassEntityBase & {
preset_modes?: string[];
swing_mode?: string;
swing_modes?: string[];
swing_horizontal_mode?: string;
swing_horizontal_modes?: string[];
aux_heat?: "on" | "off";
};
};
Expand All @@ -75,6 +77,7 @@ export const enum ClimateEntityFeature {
AUX_HEAT = 64,
TURN_OFF = 128,
TURN_ON = 256,
SWING_HORIZONTAL_MODE = 512,
}

const hvacModeOrdering = HVAC_MODES.reduce(
Expand Down
67 changes: 67 additions & 0 deletions src/dialogs/more-info/controls/more-info-climate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class MoreInfoClimate extends LitElement {
stateObj,
ClimateEntityFeature.SWING_MODE
);
const supportSwingHorizontalMode = supportsFeature(
stateObj,
ClimateEntityFeature.SWING_HORIZONTAL_MODE
);

const currentTemperature = this.stateObj.attributes.current_temperature;
const currentHumidity = this.stateObj.attributes.current_humidity;
Expand Down Expand Up @@ -344,6 +348,59 @@ class MoreInfoClimate extends LitElement {
</ha-control-select-menu>
`
: nothing}
${supportSwingHorizontalMode &&
stateObj.attributes.swing_horizontal_modes
? html`
<ha-control-select-menu
.label=${this.hass.formatEntityAttributeName(
stateObj,
"swing_horizontal_mode"
)}
.value=${stateObj.attributes.swing_horizontal_mode}
.disabled=${this.stateObj.state === UNAVAILABLE}
fixedMenuPosition
naturalMenuWidth
@selected=${this._handleSwingHorizontalmodeChanged}
@closed=${stopPropagation}
>
${stateObj.attributes.swing_horizontal_mode
? html`
<ha-attribute-icon
slot="icon"
.hass=${this.hass}
.stateObj=${stateObj}
attribute="swing_horizontal_mode"
.attributeValue=${stateObj.attributes
.swing_horizontal_mode}
></ha-attribute-icon>
`
: html`
<ha-svg-icon
slot="icon"
.path=${mdiArrowOscillating}
></ha-svg-icon>
`}
${stateObj.attributes.swing_horizontal_modes!.map(
(mode) => html`
<ha-list-item .value=${mode} graphic="icon">
<ha-attribute-icon
slot="graphic"
.hass=${this.hass}
.stateObj=${stateObj}
attribute="swing_horizontal_mode"
.attributeValue=${mode}
></ha-attribute-icon>
${this.hass.formatEntityAttributeValue(
stateObj,
"swing_horizontal_mode",
mode
)}
</ha-list-item>
`
)}
</ha-control-select-menu>
`
: nothing}
</ha-more-info-control-select-container>
`;
}
Expand Down Expand Up @@ -380,6 +437,16 @@ class MoreInfoClimate extends LitElement {
);
}

private _handleSwingHorizontalmodeChanged(ev) {
const newVal = ev.target.value;
this._callServiceHelper(
this.stateObj!.attributes.swing_horizontal_mode,
newVal,
"set_swing_horizontal_mode",
{ swing_horizontal_mode: newVal }
);
}

private _handlePresetmodeChanged(ev) {
const newVal = ev.target.value || null;
if (newVal) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import { mdiArrowOscillating } from "@mdi/js";
import type { HassEntity } from "home-assistant-js-websocket";
import type { PropertyValues, TemplateResult } from "lit";
import { html, LitElement } from "lit";
import { customElement, property, query, state } from "lit/decorators";
import { stopPropagation } from "../../../common/dom/stop_propagation";
import { computeDomain } from "../../../common/entity/compute_domain";
import { supportsFeature } from "../../../common/entity/supports-feature";
import "../../../components/ha-attribute-icon";
import "../../../components/ha-control-select";
import type { ControlSelectOption } from "../../../components/ha-control-select";
import "../../../components/ha-control-select-menu";
import type { HaControlSelectMenu } from "../../../components/ha-control-select-menu";
import type { ClimateEntity } from "../../../data/climate";
import { ClimateEntityFeature } from "../../../data/climate";
import { UNAVAILABLE } from "../../../data/entity";
import type { HomeAssistant } from "../../../types";
import type { LovelaceCardFeature, LovelaceCardFeatureEditor } from "../types";
import { cardFeatureStyles } from "./common/card-feature-styles";
import { filterModes } from "./common/filter-modes";
import type { ClimateSwingHorizontalModesCardFeatureConfig } from "./types";

export const supportsClimateSwingHorizontalModesCardFeature = (
stateObj: HassEntity
) => {
const domain = computeDomain(stateObj.entity_id);
return (
domain === "climate" &&
supportsFeature(stateObj, ClimateEntityFeature.SWING_HORIZONTAL_MODE)
);
};

@customElement("hui-climate-swing-horizontal-modes-card-feature")
class HuiClimateSwingHorizontalModesCardFeature
extends LitElement
implements LovelaceCardFeature
{
@property({ attribute: false }) public hass?: HomeAssistant;

@property({ attribute: false }) public stateObj?: ClimateEntity;

@state() private _config?: ClimateSwingHorizontalModesCardFeatureConfig;

@state() _currentSwingHorizontalMode?: string;

@query("ha-control-select-menu", true)
private _haSelect?: HaControlSelectMenu;

static getStubConfig(): ClimateSwingHorizontalModesCardFeatureConfig {
return {
type: "climate-swing-horizontal-modes",
style: "dropdown",
};
}

public static async getConfigElement(): Promise<LovelaceCardFeatureEditor> {
await import(
"../editor/config-elements/hui-climate-swing-horizontal-modes-card-feature-editor"
);
return document.createElement(
"hui-climate-swing-horizontal-modes-card-feature-editor"
);
}

public setConfig(config: ClimateSwingHorizontalModesCardFeatureConfig): 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._currentSwingHorizontalMode =
this.stateObj.attributes.swing_horizontal_mode;
}
}

protected updated(changedProps: PropertyValues) {
super.updated(changedProps);
if (this._haSelect && changedProps.has("hass")) {
const oldHass = changedProps.get("hass") as HomeAssistant | undefined;
if (
this.hass &&
this.hass.formatEntityAttributeValue !==
oldHass?.formatEntityAttributeValue
) {
this._haSelect.layoutOptions();
}
}
}

private async _valueChanged(ev: CustomEvent) {
const swingHorizontalMode =
(ev.detail as any).value ?? ((ev.target as any).value as string);

const oldSwingHorizontalMode =
this.stateObj!.attributes.swing_horizontal_mode;

if (swingHorizontalMode === oldSwingHorizontalMode) return;

this._currentSwingHorizontalMode = swingHorizontalMode;

try {
await this._setMode(swingHorizontalMode);
} catch (err) {
this._currentSwingHorizontalMode = oldSwingHorizontalMode;
}
}

private async _setMode(mode: string) {
await this.hass!.callService("climate", "set_swing_horizontal_mode", {
entity_id: this.stateObj!.entity_id,
swing_horizontal_mode: mode,
});
}

protected render(): TemplateResult | null {
if (
!this._config ||
!this.hass ||
!this.stateObj ||
!supportsClimateSwingHorizontalModesCardFeature(this.stateObj)
) {
return null;
}

const stateObj = this.stateObj;

const options = filterModes(
stateObj.attributes.swing_horizontal_modes,
this._config!.swing_horizontal_modes
).map<ControlSelectOption>((mode) => ({
value: mode,
label: this.hass!.formatEntityAttributeValue(
this.stateObj!,
"swing_horizontal_mode",
mode
),
icon: html`<ha-attribute-icon
slot="graphic"
.hass=${this.hass}
.stateObj=${stateObj}
attribute="swing_horizontal_mode"
.attributeValue=${mode}
></ha-attribute-icon>`,
}));

if (this._config.style === "icons") {
return html`
<ha-control-select
.options=${options}
.value=${this._currentSwingHorizontalMode}
@value-changed=${this._valueChanged}
hide-label
.ariaLabel=${this.hass!.formatEntityAttributeName(
stateObj,
"swing_horizontal_mode"
)}
.disabled=${this.stateObj!.state === UNAVAILABLE}
>
</ha-control-select>
`;
}

return html`
<ha-control-select-menu
show-arrow
hide-label
.label=${this.hass!.formatEntityAttributeName(
stateObj,
"swing_horizontal_mode"
)}
.value=${this._currentSwingHorizontalMode}
.disabled=${this.stateObj.state === UNAVAILABLE}
fixedMenuPosition
naturalMenuWidth
@selected=${this._valueChanged}
@closed=${stopPropagation}
>
${this._currentSwingHorizontalMode
? html`<ha-attribute-icon
slot="icon"
.hass=${this.hass}
.stateObj=${stateObj}
attribute="swing_horizontal_mode"
.attributeValue=${this._currentSwingHorizontalMode}
></ha-attribute-icon>`
: html` <ha-svg-icon
slot="icon"
.path=${mdiArrowOscillating}
></ha-svg-icon>`}
${options.map(
(option) => html`
<ha-list-item .value=${option.value} graphic="icon">
${option.icon}${option.label}
</ha-list-item>
`
)}
</ha-control-select-menu>
`;
}

static get styles() {
return cardFeatureStyles;
}
}

declare global {
interface HTMLElementTagNameMap {
"hui-climate-swing-horizontal-modes-card-feature": HuiClimateSwingHorizontalModesCardFeature;
}
}
7 changes: 7 additions & 0 deletions src/panels/lovelace/card-features/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ export interface ClimateSwingModesCardFeatureConfig {
swing_modes?: string[];
}

export interface ClimateSwingHorizontalModesCardFeatureConfig {
type: "climate-swing-horizontal-modes";
style?: "dropdown" | "icons";
swing_horizontal_modes?: string[];
}

export interface ClimateHvacModesCardFeatureConfig {
type: "climate-hvac-modes";
style?: "dropdown" | "icons";
Expand Down Expand Up @@ -139,6 +145,7 @@ export type LovelaceCardFeatureConfig =
| AlarmModesCardFeatureConfig
| ClimateFanModesCardFeatureConfig
| ClimateSwingModesCardFeatureConfig
| ClimateSwingHorizontalModesCardFeatureConfig
| ClimateHvacModesCardFeatureConfig
| ClimatePresetModesCardFeatureConfig
| CoverOpenCloseCardFeatureConfig
Expand Down
Loading

0 comments on commit 988fa3e

Please sign in to comment.