Handle entity taps and holds in one place
This commit is contained in:
@ -34,4 +34,5 @@ class EntityTapAction {
|
||||
static const moreInfo = 'more-info';
|
||||
static const toggle = 'toggle';
|
||||
static const callService = 'call-service';
|
||||
static const none = 'none';
|
||||
}
|
@ -6,8 +6,10 @@ class EntityWrapper {
|
||||
String icon;
|
||||
String tapAction;
|
||||
String holdAction;
|
||||
String actionService;
|
||||
Map<String, dynamic> actionServiceData;
|
||||
String tapActionService;
|
||||
Map<String, dynamic> tapActionServiceData;
|
||||
String holdActionService;
|
||||
Map<String, dynamic> holdActionServiceData;
|
||||
Entity entity;
|
||||
|
||||
|
||||
@ -16,12 +18,66 @@ class EntityWrapper {
|
||||
String icon,
|
||||
String displayName,
|
||||
this.tapAction: EntityTapAction.moreInfo,
|
||||
this.holdAction,
|
||||
this.actionService,
|
||||
this.actionServiceData
|
||||
this.holdAction: EntityTapAction.none,
|
||||
this.tapActionService,
|
||||
this.tapActionServiceData,
|
||||
this.holdActionService,
|
||||
this.holdActionServiceData
|
||||
}) {
|
||||
this.icon = icon ?? entity.icon;
|
||||
this.displayName = displayName ?? entity.displayName;
|
||||
}
|
||||
|
||||
void handleTap() {
|
||||
switch (tapAction) {
|
||||
case EntityTapAction.toggle: {
|
||||
eventBus.fire(
|
||||
ServiceCallEvent("homeassistant", "toggle", entity.entityId, null));
|
||||
break;
|
||||
}
|
||||
|
||||
case EntityTapAction.callService: {
|
||||
eventBus.fire(
|
||||
ServiceCallEvent(tapActionService.split(".")[0], tapActionService.split(".")[1], null, tapActionServiceData));
|
||||
break;
|
||||
}
|
||||
|
||||
case EntityTapAction.none: {
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
eventBus.fire(
|
||||
new ShowEntityPageEvent(entity));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void handleHold() {
|
||||
switch (holdAction) {
|
||||
case EntityTapAction.toggle: {
|
||||
eventBus.fire(
|
||||
ServiceCallEvent("homeassistant", "toggle", entity.entityId, null));
|
||||
break;
|
||||
}
|
||||
|
||||
case EntityTapAction.callService: {
|
||||
eventBus.fire(
|
||||
ServiceCallEvent(tapActionService.split(".")[0], tapActionService.split(".")[1], null, tapActionServiceData));
|
||||
break;
|
||||
}
|
||||
|
||||
case EntityTapAction.moreInfo: {
|
||||
eventBus.fire(
|
||||
new ShowEntityPageEvent(entity));
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user