From 7fe44459e71010f66caba9f8a7550fb4a0ce6268 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Fri, 14 Dec 2018 14:28:23 +0200 Subject: [PATCH] Resolves #252, Resolves #249 --- lib/entity_class/const.dart | 40 +++++++++++++++- lib/entity_class/entity_wrapper.class.dart | 55 +++++++++++----------- lib/home_assistant.class.dart | 39 ++------------- 3 files changed, 70 insertions(+), 64 deletions(-) diff --git a/lib/entity_class/const.dart b/lib/entity_class/const.dart index 52a0920..6047dc3 100644 --- a/lib/entity_class/const.dart +++ b/lib/entity_class/const.dart @@ -30,11 +30,49 @@ class EntityState { static const problem = 'problem'; } -class EntityTapAction { +class EntityUIAction { static const moreInfo = 'more-info'; static const toggle = 'toggle'; static const callService = 'call-service'; + static const navigate = 'navigate'; static const none = 'none'; + + String tapAction = EntityUIAction.moreInfo; + String tapNavigationPath; + String tapService; + Map tapServiceData; + String holdAction = EntityUIAction.none; + String holdNavigationPath; + String holdService; + Map holdServiceData; + + EntityUIAction({rawEntityData}) { + if (rawEntityData != null) { + if (rawEntityData["tap_action"] != null) { + if (rawEntityData["tap_action"] is String) { + tapAction = rawEntityData["tap_action"]; + } else { + tapAction = + rawEntityData["tap_action"]["action"] ?? EntityUIAction.moreInfo; + tapNavigationPath = rawEntityData["tap_action"]["navigation_path"]; + tapService = rawEntityData["tap_action"]["service"]; + tapServiceData = rawEntityData["tap_action"]["service_data"]; + } + } + if (rawEntityData["hold_action"] != null) { + if (rawEntityData["hold_action"] is String) { + holdAction = rawEntityData["hold_action"]; + } else { + holdAction = + rawEntityData["hold_action"]["action"] ?? EntityUIAction.none; + holdNavigationPath = rawEntityData["hold_action"]["navigation_path"]; + holdService = rawEntityData["hold_action"]["service"]; + holdServiceData = rawEntityData["hold_action"]["service_data"]; + } + } + } + } + } class CardType { diff --git a/lib/entity_class/entity_wrapper.class.dart b/lib/entity_class/entity_wrapper.class.dart index 896bdca..4ea0953 100644 --- a/lib/entity_class/entity_wrapper.class.dart +++ b/lib/entity_class/entity_wrapper.class.dart @@ -4,12 +4,7 @@ class EntityWrapper { String displayName; String icon; - String tapAction; - String holdAction; - String tapActionService; - Map tapActionServiceData; - String holdActionService; - Map holdActionServiceData; + EntityUIAction uiAction; Entity entity; @@ -17,59 +12,65 @@ class EntityWrapper { this.entity, String icon, String displayName, - this.tapAction: EntityTapAction.moreInfo, - this.holdAction: EntityTapAction.none, - this.tapActionService, - this.tapActionServiceData, - this.holdActionService, - this.holdActionServiceData + this.uiAction }) { this.icon = icon ?? entity.icon; this.displayName = displayName ?? entity.displayName; } void handleTap() { - TheLogger.debug(tapAction); - switch (tapAction) { - case EntityTapAction.toggle: { + switch (uiAction.tapAction) { + case EntityUIAction.toggle: { eventBus.fire( ServiceCallEvent("homeassistant", "toggle", entity.entityId, null)); break; } - case EntityTapAction.callService: { - eventBus.fire( - ServiceCallEvent(tapActionService.split(".")[0], tapActionService.split(".")[1], null, tapActionServiceData)); + case EntityUIAction.callService: { + if (uiAction.tapService != null) { + eventBus.fire( + ServiceCallEvent(uiAction.tapService.split(".")[0], + uiAction.tapService.split(".")[1], null, + uiAction.tapServiceData)); + } break; } - case EntityTapAction.none: { + case EntityUIAction.none: { + break; + } + + case EntityUIAction.moreInfo: { + eventBus.fire( + new ShowEntityPageEvent(entity)); break; } default: { - eventBus.fire( - new ShowEntityPageEvent(entity)); break; } } } void handleHold() { - switch (holdAction) { - case EntityTapAction.toggle: { + switch (uiAction.holdAction) { + case EntityUIAction.toggle: { eventBus.fire( ServiceCallEvent("homeassistant", "toggle", entity.entityId, null)); break; } - case EntityTapAction.callService: { - eventBus.fire( - ServiceCallEvent(tapActionService.split(".")[0], tapActionService.split(".")[1], null, tapActionServiceData)); + case EntityUIAction.callService: { + if (uiAction.holdService != null) { + eventBus.fire( + ServiceCallEvent(uiAction.holdService.split(".")[0], + uiAction.holdService.split(".")[1], null, + uiAction.holdServiceData)); + } break; } - case EntityTapAction.moreInfo: { + case EntityUIAction.moreInfo: { eventBus.fire( new ShowEntityPageEvent(entity)); break; diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index dbf1d04..e55102e 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -450,28 +450,12 @@ class HomeAssistant { } else { if (entities.isExist(rawEntity["entity"])) { Entity e = entities.get(rawEntity["entity"]); - String tapAction = EntityTapAction.moreInfo; - String holdAction = EntityTapAction.none; - if (card.type == CardType.glance || card.type == CardType.entityButton) { - if (rawEntity["tap_action"] != null) { - if (rawEntity["tap_action"] is String) { - tapAction = rawEntity["tap_action"]; - holdAction = rawEntity["hold_action"]; - } else { - tapAction = rawEntity["tap_action"]["action"]; - holdAction = rawEntity["hold_action"]["action"]; - } - } - } card.entities.add( EntityWrapper( entity: e, displayName: rawEntity["name"], icon: rawEntity["icon"], - tapAction: tapAction, - holdAction: holdAction, - //tapActionService: rawEntity["service"], - //tapActionServiceData: rawEntity["service_data"] ?? {"entity_id": e.entityId} + uiAction: EntityUIAction(rawEntityData: rawEntity) ) ); } @@ -479,26 +463,12 @@ class HomeAssistant { }); if (rawCard["entity"] != null) { var en = rawCard["entity"]; - String tapAction = EntityTapAction.moreInfo; - String holdAction = EntityTapAction.none; - if (rawCard["tap_action"] != null) { - if (rawCard["tap_action"] is String) { - tapAction = rawCard["tap_action"]; - holdAction = rawCard["hold_action"]; - } else { - tapAction = rawCard["tap_action"]["action"]; - holdAction = rawCard["hold_action"]["action"]; - } - } if (en is String) { if (entities.isExist(en)) { Entity e = entities.get(en); card.linkedEntityWrapper = EntityWrapper( entity: e, - tapAction: tapAction, - holdAction: holdAction, - //tapActionService: rawCard["service"], - //tapActionServiceData: rawCard["service_data"] ?? {"entity_id": e.entityId} + uiAction: EntityUIAction(rawEntityData: rawCard) ); } } else { @@ -508,10 +478,7 @@ class HomeAssistant { entity: e, icon: en["icon"], displayName: en["name"], - tapAction: tapAction, - holdAction: holdAction, - tapActionService: rawCard["service"], - tapActionServiceData: rawCard["service_data"] ?? {"entity_id": e.entityId} + uiAction: EntityUIAction(rawEntityData: rawCard) ); } }