Skip to content

Commit

Permalink
Initial Haptic support
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider committed Apr 24, 2019
1 parent d5269cc commit 24906ca
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
12 changes: 12 additions & 0 deletions dist/button-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -2472,6 +2472,13 @@ const toggleEntity = (hass, entityId) => {
return turnOnOffEntity(hass, entityId, turnOn);
};

/**
* Utility function that enables haptic feedback
*/
const forwardHaptic = (el, hapticType) => {
fireEvent(el, "haptic", hapticType);
};

const handleClick = (node, hass, config, hold) => {
let actionConfig;
if (hold && config.hold_action) {
Expand All @@ -2490,19 +2497,23 @@ const handleClick = (node, hass, config, hold) => {
fireEvent(node, "hass-more-info", {
entityId: config.entity ? config.entity : config.camera_image
});
if (actionConfig.haptic) forwardHaptic(node, actionConfig.haptic);
}
break;
case "navigate":
if (actionConfig.navigation_path) {
navigate(node, actionConfig.navigation_path);
if (actionConfig.haptic) forwardHaptic(node, actionConfig.haptic);
}
break;
case 'url':
actionConfig.url && window.open(actionConfig.url);
if (actionConfig.haptic) forwardHaptic(node, actionConfig.haptic);
break;
case "toggle":
if (config.entity) {
toggleEntity(hass, config.entity);
if (actionConfig.haptic) forwardHaptic(node, actionConfig.haptic);
}
break;
case "call-service":
Expand All @@ -2512,6 +2523,7 @@ const handleClick = (node, hass, config, hold) => {
}
const [domain, service] = actionConfig.service.split(".", 2);
hass.callService(domain, service, actionConfig.service_data);
if (actionConfig.haptic) forwardHaptic(node, actionConfig.haptic);
}
}
};
Expand Down
6 changes: 6 additions & 0 deletions src/handle-click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HomeAssistant, ActionConfig } from "./types";
import { fireEvent } from "./fire_event";
import { navigate } from "./navigate";
import { toggleEntity } from "./toggle-entity";
import { forwardHaptic } from "./haptic";

export const handleClick = (
node: HTMLElement,
Expand Down Expand Up @@ -34,19 +35,23 @@ export const handleClick = (
fireEvent(node, "hass-more-info", {
entityId: config.entity ? config.entity : config.camera_image!,
});
if (actionConfig.haptic) forwardHaptic(node, actionConfig.haptic);
}
break;
case "navigate":
if (actionConfig.navigation_path) {
navigate(node, actionConfig.navigation_path);
if (actionConfig.haptic) forwardHaptic(node, actionConfig.haptic);
}
break;
case 'url':
actionConfig.url && window.open(actionConfig.url);
if (actionConfig.haptic) forwardHaptic(node, actionConfig.haptic);
break;
case "toggle":
if (config.entity) {
toggleEntity(hass, config.entity!);
if (actionConfig.haptic) forwardHaptic(node, actionConfig.haptic);
}
break;
case "call-service": {
Expand All @@ -55,6 +60,7 @@ export const handleClick = (
}
const [domain, service] = actionConfig.service.split(".", 2);
hass.callService(domain, service, actionConfig.service_data);
if (actionConfig.haptic) forwardHaptic(node, actionConfig.haptic);
}
}
};
29 changes: 29 additions & 0 deletions src/haptic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

/**
* Utility function that enables haptic feedback
*/

import { fireEvent } from "./fire_event";

// Allowed types are from iOS HIG.
// https://developer.apple.com/design/human-interface-guidelines/ios/user-interaction/feedback/#haptics
// Implementors on platforms other than iOS should attempt to match the patterns (shown in HIG) as closely as possible.
export type HapticType =
| "success"
| "warning"
| "failure"
| "light"
| "medium"
| "heavy"
| "selection";

declare global {
// for fire event
interface HASSDomEvents {
haptic: HapticType;
}
}

export const forwardHaptic = (el: HTMLElement, hapticType: HapticType) => {
fireEvent(el, "haptic", hapticType);
};
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MessageBase,
HassServices,
} from "home-assistant-js-websocket";
import { HapticType } from "./haptic";

export interface ButtonCardConfig {
type: string;
Expand Down Expand Up @@ -48,10 +49,12 @@ export interface CssStyleConfig {

export interface ToggleActionConfig {
action: "toggle";
haptic?: HapticType;
}

export interface CallServiceActionConfig {
action: "call-service";
haptic?: HapticType;
service: string;
service_data?: {
entity_id?: string | [string];
Expand All @@ -61,11 +64,13 @@ export interface CallServiceActionConfig {

export interface NavigateActionConfig {
action: "navigate";
haptic?: HapticType;
navigation_path: string;
}

export interface MoreInfoActionConfig {
action: "more-info";
haptic?: HapticType;
}

export interface NoActionConfig {
Expand All @@ -74,6 +79,7 @@ export interface NoActionConfig {

export interface UrlActionConfig {
action: "url";
haptic?: HapticType;
url: string;
}

Expand Down

0 comments on commit 24906ca

Please sign in to comment.