Skip to content

Commit

Permalink
ZWaveJS: Color Switch support fot the expert UI (#22722)
Browse files Browse the repository at this point in the history
* ZWaveJS: Color Switch support fot the expert UI

* remove debug code

* fix stopTransition options

* fix options format
  • Loading branch information
MindFreeze authored Nov 12, 2024
1 parent 7c851d4 commit db03e27
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { LitElement, html } from "lit";
import { customElement, property, state } from "lit/decorators";
import type { DeviceRegistryEntry } from "../../../../../../data/device_registry";
import type { HomeAssistant } from "../../../../../../types";
import { invokeZWaveCCApi } from "../../../../../../data/zwave_js";
import "../../../../../../components/ha-alert";
import "../../../../../../components/ha-circular-progress";
import { extractApiErrorMessage } from "../../../../../../data/hassio/common";
import "./zwave_js-capability-control-multilevel-switch";

@customElement("zwave_js-capability-control-color_switch")
class ZWaveJSCapabilityColorSwitch extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) public device!: DeviceRegistryEntry;

@property({ type: Number }) public endpoint!: number;

@property({ type: Number }) public command_class!: number;

@property({ type: Number }) public version!: number;

@state() private _color_components?: number[];

@state() private _error?: string;

protected render() {
if (this._error) {
return html`<ha-alert alert-type="error">${this._error}</ha-alert>`;
}
if (!this._color_components) {
return html`<ha-circular-progress indeterminate></ha-circular-progress>`;
}
return this._color_components.map(
(color) =>
html` <h5>
${this.hass.localize(
"ui.panel.config.zwave_js.node_installer.capability_controls.color_switch.color_component"
)}:
${color}
</h5>
<zwave_js-capability-control-multilevel_switch
.hass=${this.hass}
.device=${this.device}
.endpoint=${this.endpoint}
.command_class=${this.command_class}
.version=${this.version}
.transform_options=${this._transformOptions(color)}
></zwave_js-capability-control-multilevel_switch>`
);
}

protected async firstUpdated() {
try {
this._color_components = (await invokeZWaveCCApi(
this.hass,
this.device.id,
this.command_class,
this.endpoint,
"getSupported",
[],
true
)) as number[];
} catch (error) {
this._error = extractApiErrorMessage(error);
}
}

private _transformOptions(color: number) {
return (opts: Record<string, any>, control: string) =>
control === "startLevelChange"
? {
...opts,
colorComponent: color,
}
: color;
}
}

declare global {
interface HTMLElementTagNameMap {
"zwave_js-capability-control-color_switch": ZWaveJSCapabilityColorSwitch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class ZWaveJSCapabilityMultiLevelSwitch extends LitElement {

@property({ type: Number }) public version!: number;

@property({ attribute: false }) public transform_options?: (
opts: Record<string, any>,
control: string
) => unknown;

@state() private _error?: string;

protected render() {
Expand Down Expand Up @@ -109,6 +114,12 @@ class ZWaveJSCapabilityMultiLevelSwitch extends LitElement {
(this.shadowRoot!.getElementById("start_level") as HaTextField).value
);

const options = {
direction,
ignoreStartLevel,
startLevel,
};

try {
button.actionSuccess();
await invokeZWaveCCApi(
Expand All @@ -117,7 +128,11 @@ class ZWaveJSCapabilityMultiLevelSwitch extends LitElement {
this.command_class,
this.endpoint,
control,
[{ direction, ignoreStartLevel, startLevel }],
[
this.transform_options
? this.transform_options(options, control)
: options,
],
true
);
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import type { HomeAssistant, Route } from "../../../../../types";
import "../../../ha-config-section";
import "./capability-controls/zwave_js-capability-control-multilevel-switch";
import "./capability-controls/zwave_js-capability-control-thermostat-setback";
import "./capability-controls/zwave_js-capability-control-color-switch";

const CAPABILITY_CONTROLS = {
38: "multilevel_switch",
71: "thermostat_setback",
51: "color_switch",
};

@customElement("zwave_js-node-installer")
Expand Down
3 changes: 3 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5263,6 +5263,9 @@
"start_transition": "Start transition",
"stop_transition": "Stop transition",
"control_failed": "Failed to control transition. {error}"
},
"color_switch": {
"color_component": "Color component"
}
}
}
Expand Down

0 comments on commit db03e27

Please sign in to comment.