From 50e91d9d078224b701f4315b23d4927d8b17bc27 Mon Sep 17 00:00:00 2001 From: Boris Kubiak Date: Thu, 12 Sep 2024 10:55:27 +0200 Subject: [PATCH] Add haptic feedback Fixes #29 --- card.js | 11 +++++++++++ test/unit-tests.js | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/card.js b/card.js index e0885a4..a460a13 100644 --- a/card.js +++ b/card.js @@ -119,6 +119,7 @@ class RGBLightCard extends HTMLElement { } applyColor(color) { + this.fireHapticFeedback(); if (color.type === 'action') { const [domain, action] = color.action.split('.'); this._hass.callService(domain, action, color.data || {}); @@ -149,6 +150,16 @@ class RGBLightCard extends HTMLElement { } } + fireHapticFeedback() { + const event = new Event('haptic', { + bubbles: true, + cancelable: false, + composed: true, + }); + event.detail = 'light'; + window.dispatchEvent(event); + } + // Transform a deprecated config into a more recent one static ensureBackwardCompatibility(originalConfig) { // Create a deep copy of the config diff --git a/test/unit-tests.js b/test/unit-tests.js index 2c885dd..c47b35c 100644 --- a/test/unit-tests.js +++ b/test/unit-tests.js @@ -62,6 +62,11 @@ test('Clicking the icons call the right function', (t) => { }, }; + let vibrations = 0; + window.dispatchEvent = (event) => { + if (event.type === 'haptic') vibrations++; + }; + card.setConfig({ entity: 'light.example', colors: [ @@ -79,6 +84,7 @@ test('Clicking the icons call the right function', (t) => { ], }); clickOnCircle(1); + t.is(vibrations, 1); t.deepEqual(called, { domain: 'light', service: 'turn_on', @@ -86,9 +92,11 @@ test('Clicking the icons call the right function', (t) => { }); clickOnCircle(2); + t.is(vibrations, 2); t.deepEqual(called, { domain: 'homeassistant', service: 'restart', payload: { force: true } }); clickOnCircle(3); + t.is(vibrations, 3); t.deepEqual(called, { domain: 'hue', service: 'hue_activate_scene',