Skip to content

Commit

Permalink
Add climate fan mode feature to thermostat & tile cards (#19094)
Browse files Browse the repository at this point in the history
* climate: Add fan_only exemple

* climate: add fan_mode feature to thermostat & tile cards

* review: update dropdown icon
  • Loading branch information
Quentame authored Dec 21, 2023
1 parent 53839ab commit 521c0b5
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 0 deletions.
29 changes: 29 additions & 0 deletions gallery/src/pages/lovelace/thermostat-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ const ENTITIES = [
friendly_name: "Nest",
supported_features: 43,
}),
getEntity("climate", "sensibo", "fan_only", {
current_temperature: null,
temperature: null,
min_temp: 0,
max_temp: 1,
target_temp_step: 1,
hvac_modes: ["fan_only", "off"],
friendly_name: "Sensibo purifier",
fan_modes: ["low", "high"],
fan_mode: "low",
supported_features: 9,
}),
getEntity("climate", "unavailable", "unavailable", {
supported_features: 43,
}),
Expand All @@ -57,6 +69,23 @@ const CONFIGS = [
entity: climate.nest
`,
},
{
heading: "Fan only example",
config: `
- type: thermostat
entity: climate.sensibo
features:
- type: climate-hvac-modes
hvac_modes:
- fan_only
- 'off'
- type: climate-fan-modes
style: icons
fan_modes:
- low
- high
`,
},
{
heading: "Unavailable",
config: `
Expand Down
15 changes: 15 additions & 0 deletions gallery/src/pages/more-info/climate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ const ENTITIES = [
max_temp: 30,
supported_features: ClimateEntityFeature.TARGET_TEMPERATURE,
}),
getEntity("climate", "fan", "fan_only", {
friendly_name: "Basic fan",
hvac_modes: ["fan_only", "off"],
hvac_mode: "fan_only",
fan_modes: ["low", "high"],
fan_mode: "low",
current_temperature: null,
temperature: null,
min_temp: 0,
max_temp: 1,
target_temp_step: 1,
supported_features:
// eslint-disable-next-line no-bitwise
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE,
}),
getEntity("climate", "hvac", "auto", {
friendly_name: "Basic hvac",
hvac_modes: ["auto", "off"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
import { mdiFan } from "@mdi/js";
import { HassEntity } from "home-assistant-js-websocket";
import { css, html, LitElement, PropertyValues, TemplateResult } 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-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 {
ClimateEntity,
ClimateEntityFeature,
computeFanModeIcon,
} from "../../../data/climate";
import { UNAVAILABLE } from "../../../data/entity";
import { HomeAssistant } from "../../../types";
import { LovelaceCardFeature, LovelaceCardFeatureEditor } from "../types";
import { ClimateFanModesCardFeatureConfig } from "./types";

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

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

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

@state() private _config?: ClimateFanModesCardFeatureConfig;

@state() _currentFanMode?: string;

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

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

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

public setConfig(config: ClimateFanModesCardFeatureConfig): 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._currentFanMode = this.stateObj.attributes.fan_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 fanMode =
(ev.detail as any).value ?? ((ev.target as any).value as string);

const oldFanMode = this.stateObj!.attributes.fan_mode;

if (fanMode === oldFanMode) return;

this._currentFanMode = fanMode;

try {
await this._setMode(fanMode);
} catch (err) {
this._currentFanMode = oldFanMode;
}
}

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

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

const stateObj = this.stateObj;

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

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

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

return html`
<div class="container">
<ha-control-select-menu
show-arrow
hide-label
.label=${this.hass!.formatEntityAttributeName(stateObj, "fan_mode")}
.value=${this._currentFanMode}
.disabled=${this.stateObj.state === UNAVAILABLE}
fixedMenuPosition
naturalMenuWidth
@selected=${this._valueChanged}
@closed=${stopPropagation}
>
<ha-svg-icon slot="icon" .path=${mdiFan}></ha-svg-icon>
${options.map(
(option) => html`
<ha-list-item .value=${option.value} graphic="icon">
<ha-svg-icon slot="graphic" .path=${option.path}></ha-svg-icon>
${option.label}
</ha-list-item>
`
)}
</ha-control-select-menu>
</div>
`;
}

static get styles() {
return css`
ha-control-select-menu {
box-sizing: border-box;
--control-select-menu-height: 40px;
--control-select-menu-border-radius: 10px;
line-height: 1.2;
display: block;
width: 100%;
}
ha-control-select {
--control-select-color: var(--feature-color);
--control-select-padding: 0;
--control-select-thickness: 40px;
--control-select-border-radius: 10px;
--control-select-button-border-radius: 10px;
}
.container {
padding: 0 12px 12px 12px;
width: auto;
}
`;
}
}

declare global {
interface HTMLElementTagNameMap {
"hui-climate-fan-modes-card-feature": HuiClimateFanModesCardFeature;
}
}
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 @@ -35,6 +35,12 @@ export interface AlarmModesCardFeatureConfig {
modes?: AlarmMode[];
}

export interface ClimateFanModesCardFeatureConfig {
type: "climate-fan-modes";
style?: "dropdown" | "icons";
fan_modes?: string[];
}

export interface ClimateHvacModesCardFeatureConfig {
type: "climate-hvac-modes";
style?: "dropdown" | "icons";
Expand Down Expand Up @@ -105,6 +111,7 @@ export interface LawnMowerCommandsCardFeatureConfig {

export type LovelaceCardFeatureConfig =
| AlarmModesCardFeatureConfig
| ClimateFanModesCardFeatureConfig
| ClimateHvacModesCardFeatureConfig
| ClimatePresetModesCardFeatureConfig
| CoverOpenCloseCardFeatureConfig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "../card-features/hui-alarm-modes-card-feature";
import "../card-features/hui-climate-fan-modes-card-feature";
import "../card-features/hui-climate-hvac-modes-card-feature";
import "../card-features/hui-climate-preset-modes-card-feature";
import "../card-features/hui-cover-open-close-card-feature";
Expand All @@ -25,6 +26,7 @@ import {

const TYPES: Set<LovelaceCardFeatureConfig["type"]> = new Set([
"alarm-modes",
"climate-fan-modes",
"climate-hvac-modes",
"climate-preset-modes",
"cover-open-close",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { sortableStyles } from "../../../../resources/ha-sortable-style";
import type { SortableInstance } from "../../../../resources/sortable";
import { HomeAssistant } from "../../../../types";
import { supportsAlarmModesCardFeature } from "../../card-features/hui-alarm-modes-card-feature";
import { supportsClimateFanModesCardFeature } from "../../card-features/hui-climate-fan-modes-card-feature";
import { supportsClimateHvacModesCardFeature } from "../../card-features/hui-climate-hvac-modes-card-feature";
import { supportsClimatePresetModesCardFeature } from "../../card-features/hui-climate-preset-modes-card-feature";
import { supportsCoverOpenCloseCardFeature } from "../../card-features/hui-cover-open-close-card-feature";
Expand All @@ -48,6 +49,7 @@ type SupportsFeature = (stateObj: HassEntity) => boolean;

const UI_FEATURE_TYPES = [
"alarm-modes",
"climate-fan-modes",
"climate-hvac-modes",
"climate-preset-modes",
"cover-open-close",
Expand Down Expand Up @@ -86,6 +88,7 @@ const SUPPORTS_FEATURE_TYPES: Record<
SupportsFeature | undefined
> = {
"alarm-modes": supportsAlarmModesCardFeature,
"climate-fan-modes": supportsClimateFanModesCardFeature,
"climate-hvac-modes": supportsClimateHvacModesCardFeature,
"climate-preset-modes": supportsClimatePresetModesCardFeature,
"cover-open-close": supportsCoverOpenCloseCardFeature,
Expand Down
Loading

0 comments on commit 521c0b5

Please sign in to comment.