From db03e271f5c5a82416cc16ff855200046de82735 Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Tue, 12 Nov 2024 19:21:45 +0200 Subject: [PATCH] ZWaveJS: Color Switch support fot the expert UI (#22722) * ZWaveJS: Color Switch support fot the expert UI * remove debug code * fix stopTransition options * fix options format --- ...wave_js-capability-control-color-switch.ts | 84 +++++++++++++++++++ ...js-capability-control-multilevel-switch.ts | 17 +++- .../zwave_js/zwave_js-node-installer.ts | 2 + src/translations/en.json | 3 + 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/panels/config/integrations/integration-panels/zwave_js/capability-controls/zwave_js-capability-control-color-switch.ts diff --git a/src/panels/config/integrations/integration-panels/zwave_js/capability-controls/zwave_js-capability-control-color-switch.ts b/src/panels/config/integrations/integration-panels/zwave_js/capability-controls/zwave_js-capability-control-color-switch.ts new file mode 100644 index 0000000000..d7facea8ac --- /dev/null +++ b/src/panels/config/integrations/integration-panels/zwave_js/capability-controls/zwave_js-capability-control-color-switch.ts @@ -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`${this._error}`; + } + if (!this._color_components) { + return html``; + } + return this._color_components.map( + (color) => + html`
+ ${this.hass.localize( + "ui.panel.config.zwave_js.node_installer.capability_controls.color_switch.color_component" + )}: + ${color} +
+ ` + ); + } + + 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, control: string) => + control === "startLevelChange" + ? { + ...opts, + colorComponent: color, + } + : color; + } +} + +declare global { + interface HTMLElementTagNameMap { + "zwave_js-capability-control-color_switch": ZWaveJSCapabilityColorSwitch; + } +} diff --git a/src/panels/config/integrations/integration-panels/zwave_js/capability-controls/zwave_js-capability-control-multilevel-switch.ts b/src/panels/config/integrations/integration-panels/zwave_js/capability-controls/zwave_js-capability-control-multilevel-switch.ts index cf7e957926..9a06049121 100644 --- a/src/panels/config/integrations/integration-panels/zwave_js/capability-controls/zwave_js-capability-control-multilevel-switch.ts +++ b/src/panels/config/integrations/integration-panels/zwave_js/capability-controls/zwave_js-capability-control-multilevel-switch.ts @@ -28,6 +28,11 @@ class ZWaveJSCapabilityMultiLevelSwitch extends LitElement { @property({ type: Number }) public version!: number; + @property({ attribute: false }) public transform_options?: ( + opts: Record, + control: string + ) => unknown; + @state() private _error?: string; protected render() { @@ -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( @@ -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) { diff --git a/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-node-installer.ts b/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-node-installer.ts index 352c7e38a2..03596963cb 100644 --- a/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-node-installer.ts +++ b/src/panels/config/integrations/integration-panels/zwave_js/zwave_js-node-installer.ts @@ -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") diff --git a/src/translations/en.json b/src/translations/en.json index 26c602f546..68eb675f05 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -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" } } }