Resolves #183 Service call support for glance card

This commit is contained in:
Yegor Vialov 2018-11-16 23:35:08 +02:00
parent 82c9bd26d1
commit ab67b557ca
3 changed files with 71 additions and 16 deletions

View File

@ -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;

View File

@ -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;
}
} }
} }

View File

@ -327,9 +327,11 @@ 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 (entityId != null) {
message = '{"id": $_currentMessageId, "type": "call_service", "domain": "$domain", "service": "$service", "service_data": {"entity_id": "$entityId"';
if (additionalParams != null) { if (additionalParams != null) {
additionalParams.forEach((name, value){ additionalParams.forEach((name, value) {
if ((value is double) || (value is int) || (value is List)) { if ((value is double) || (value is int) || (value is List)) {
message += ', "$name" : $value'; message += ', "$name" : $value';
} else { } else {
@ -338,6 +340,27 @@ class HomeAssistant {
}); });
} }
message += '}}'; 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 += '}';
}
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}
) )
); );
} }