Skip to content

Commit

Permalink
Add haptic feedback
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
bokub committed Sep 12, 2024
1 parent abc0783 commit 50e91d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions card.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {});
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/unit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -79,16 +84,19 @@ test('Clicking the icons call the right function', (t) => {
],
});
clickOnCircle(1);
t.is(vibrations, 1);
t.deepEqual(called, {
domain: 'light',
service: 'turn_on',
payload: { entity_id: 'light.example', hs_color: [180, 50], brightness: 200 },
});

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',
Expand Down

0 comments on commit 50e91d9

Please sign in to comment.