Add vibration (#3588)

* Add vibration

I don't have a device that supports vibrate, and can't find a list of patterns, maybe someone can make some nice patterns?

* listen event

* Mixin

* move logic to mixin
This commit is contained in:
Bram Kragten 2019-09-09 14:08:52 +02:00 committed by GitHub
parent 2d8d6119bd
commit 11f917d5f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

28
src/state/haptic-mixin.ts Normal file
View File

@ -0,0 +1,28 @@
import { Constructor, LitElement, PropertyValues } from "lit-element";
import { HassBaseEl } from "./hass-base-mixin";
import { HapticType } from "../data/haptics";
const hapticPatterns = {
success: [50, 50, 50],
warning: [100, 50, 100],
failure: [200, 100, 200],
light: [50],
medium: [100],
heavy: [200],
selection: [20],
};
const handleHaptic = (hapticType: HapticType) => {
navigator.vibrate(hapticPatterns[hapticType]);
};
export const hapticMixin = (superClass: Constructor<LitElement & HassBaseEl>) =>
class extends superClass {
protected firstUpdated(changedProps: PropertyValues) {
super.firstUpdated(changedProps);
if (navigator.vibrate) {
window.addEventListener("haptic", (e) => handleHaptic(e.detail));
}
}
};

View File

@ -9,6 +9,7 @@ import { dialogManagerMixin } from "./dialog-manager-mixin";
import { connectionMixin } from "./connection-mixin";
import NotificationMixin from "./notification-mixin";
import DisconnectToastMixin from "./disconnect-toast-mixin";
import { hapticMixin } from "./haptic-mixin";
import { urlSyncMixin } from "./url-sync-mixin";
import { LitElement } from "lit-element";
@ -27,4 +28,5 @@ export class HassElement extends ext(HassBaseMixin(LitElement), [
dialogManagerMixin,
urlSyncMixin,
ZHADialogMixin,
hapticMixin,
]) {}