Resolves #183 Service call support for glance card
This commit is contained in:
		| @@ -6,6 +6,8 @@ class EntityWrapper { | |||||||
|   String icon; |   String icon; | ||||||
|   String tapAction; |   String tapAction; | ||||||
|   String holdAction; |   String holdAction; | ||||||
|  |   String actionService; | ||||||
|  |   Map<String, dynamic> actionServiceData; | ||||||
|   Entity entity; |   Entity entity; | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -14,7 +16,9 @@ class EntityWrapper { | |||||||
|     String icon, |     String icon, | ||||||
|     String displayName, |     String displayName, | ||||||
|     this.tapAction: EntityTapAction.moreInfo, |     this.tapAction: EntityTapAction.moreInfo, | ||||||
|     this.holdAction |     this.holdAction, | ||||||
|  |     this.actionService, | ||||||
|  |     this.actionServiceData | ||||||
|   }) { |   }) { | ||||||
|     this.icon = icon ?? entity.icon; |     this.icon = icon ?? entity.icon; | ||||||
|     this.displayName = displayName ?? entity.displayName; |     this.displayName = displayName ?? entity.displayName; | ||||||
|   | |||||||
| @@ -20,19 +20,44 @@ class EntityIcon extends StatelessWidget { | |||||||
|             EntityColor.stateColor(entityModel.entityWrapper.entity.state) |             EntityColor.stateColor(entityModel.entityWrapper.entity.state) | ||||||
|         ), |         ), | ||||||
|       ), |       ), | ||||||
|       onTap: () { |       onLongPress: () { | ||||||
|         if (entityModel.handleTap) { |         if (entityModel.handleTap) { | ||||||
|           switch (entityModel.entityWrapper.tapAction) { |           switch (entityModel.entityWrapper.holdAction) { | ||||||
|             case EntityTapAction.moreInfo: { |             case EntityTapAction.toggle: { | ||||||
|  |               eventBus.fire( | ||||||
|  |                   ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null)); | ||||||
|  |               break; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             default: { | ||||||
|               eventBus.fire( |               eventBus.fire( | ||||||
|                   new ShowEntityPageEvent(entityModel.entityWrapper.entity)); |                   new ShowEntityPageEvent(entityModel.entityWrapper.entity)); | ||||||
|               break; |               break; | ||||||
|             } |             } | ||||||
|  |           } | ||||||
|  |  | ||||||
|  |         } | ||||||
|  |       }, | ||||||
|  |       onTap: () { | ||||||
|  |         if (entityModel.handleTap) { | ||||||
|  |           switch (entityModel.entityWrapper.tapAction) { | ||||||
|             case EntityTapAction.toggle: { |             case EntityTapAction.toggle: { | ||||||
|               eventBus.fire( |               eventBus.fire( | ||||||
|                   ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null)); |                   ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null)); | ||||||
|               break; |               break; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  |             case EntityTapAction.callService: { | ||||||
|  |               eventBus.fire( | ||||||
|  |                   ServiceCallEvent(entityModel.entityWrapper.actionService.split(".")[0], entityModel.entityWrapper.actionService.split(".")[1], null, entityModel.entityWrapper.actionServiceData)); | ||||||
|  |               break; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             default: { | ||||||
|  |               eventBus.fire( | ||||||
|  |                   new ShowEntityPageEvent(entityModel.entityWrapper.entity)); | ||||||
|  |               break; | ||||||
|  |             } | ||||||
|           } |           } | ||||||
|  |  | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -327,17 +327,40 @@ class HomeAssistant { | |||||||
|  |  | ||||||
|   Future callService(String domain, String service, String entityId, Map<String, dynamic> additionalParams) { |   Future callService(String domain, String service, String entityId, Map<String, dynamic> additionalParams) { | ||||||
|     _incrementMessageId(); |     _incrementMessageId(); | ||||||
|     String message = '{"id": $_currentMessageId, "type": "call_service", "domain": "$domain", "service": "$service", "service_data": {"entity_id": "$entityId"'; |     String message = ""; | ||||||
|     if (additionalParams != null) { |     if (entityId != null) { | ||||||
|       additionalParams.forEach((name, value){ |       message = '{"id": $_currentMessageId, "type": "call_service", "domain": "$domain", "service": "$service", "service_data": {"entity_id": "$entityId"'; | ||||||
|         if ((value is double) || (value is int) || (value is List)) { |       if (additionalParams != null) { | ||||||
|           message += ', "$name" : $value'; |         additionalParams.forEach((name, value) { | ||||||
|         } else { |           if ((value is double) || (value is int) || (value is List)) { | ||||||
|           message += ', "$name" : "$value"'; |             message += ', "$name" : $value'; | ||||||
|         } |           } else { | ||||||
|       }); |             message += ', "$name" : "$value"'; | ||||||
|  |           } | ||||||
|  |         }); | ||||||
|  |       } | ||||||
|  |       message += '}}'; | ||||||
|  |     } else { | ||||||
|  |       message = '{"id": $_currentMessageId, "type": "call_service", "domain": "$domain", "service": "$service"'; | ||||||
|  |       if (additionalParams != null && additionalParams.isNotEmpty) { | ||||||
|  |         message += ', "service_data": {'; | ||||||
|  |         bool first = true; | ||||||
|  |         additionalParams.forEach((name, value) { | ||||||
|  |           if (!first) { | ||||||
|  |             message += ', '; | ||||||
|  |           } | ||||||
|  |           if ((value is double) || (value is int) || (value is List)) { | ||||||
|  |             message += '"$name" : $value'; | ||||||
|  |           } else { | ||||||
|  |             message += '"$name" : "$value"'; | ||||||
|  |           } | ||||||
|  |           first = false; | ||||||
|  |         }); | ||||||
|  |  | ||||||
|  |         message += '}'; | ||||||
|  |       } | ||||||
|  |       message += '}'; | ||||||
|     } |     } | ||||||
|     message += '}}'; |  | ||||||
|     return _sendMessageRaw(message, true); |     return _sendMessageRaw(message, true); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -423,13 +446,16 @@ class HomeAssistant { | |||||||
|             } |             } | ||||||
|           } else { |           } else { | ||||||
|             if (entities.isExist(rawEntity["entity"])) { |             if (entities.isExist(rawEntity["entity"])) { | ||||||
|  |               Entity e = entities.get(rawEntity["entity"]); | ||||||
|               card.entities.add( |               card.entities.add( | ||||||
|                   EntityWrapper( |                   EntityWrapper( | ||||||
|                     entity: entities.get(rawEntity["entity"]), |                     entity: e, | ||||||
|                     displayName: rawEntity["name"], |                     displayName: rawEntity["name"], | ||||||
|                     icon: rawEntity["icon"], |                     icon: rawEntity["icon"], | ||||||
|                     tapAction: rawEntity["tap_action"] ?? EntityTapAction.moreInfo, |                     tapAction: rawEntity["tap_action"] ?? EntityTapAction.moreInfo, | ||||||
|                     holdAction: rawEntity["hold_action"] ?? EntityTapAction.moreInfo |                     holdAction: rawEntity["hold_action"] ?? EntityTapAction.moreInfo, | ||||||
|  |                     actionService: rawEntity["service"], | ||||||
|  |                     actionServiceData: rawEntity["service_data"] ?? {"entity_id": e.entityId} | ||||||
|                   ) |                   ) | ||||||
|               ); |               ); | ||||||
|             } |             } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user