This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/entities/entity_wrapper.class.dart

116 lines
3.0 KiB
Dart
Raw Normal View History

2018-11-15 19:08:47 +02:00
part of '../main.dart';
class EntityWrapper {
String displayName;
String icon;
2019-09-07 17:04:40 +03:00
String unitOfMeasurement;
2019-03-13 22:12:01 +02:00
String entityPicture;
2018-12-14 14:28:23 +02:00
EntityUIAction uiAction;
2018-11-15 19:08:47 +02:00
Entity entity;
EntityWrapper({
this.entity,
String icon,
String displayName,
2018-12-14 14:28:23 +02:00
this.uiAction
}) {
if (entity.statelessType == StatelessEntityType.NONE || entity.statelessType == StatelessEntityType.CALL_SERVICE || entity.statelessType == StatelessEntityType.WEBLINK) {
2019-03-12 23:35:33 +02:00
this.icon = icon ?? entity.icon;
2019-03-13 22:12:01 +02:00
if (icon == null) {
entityPicture = entity.entityPicture;
}
2019-03-12 23:35:33 +02:00
this.displayName = displayName ?? entity.displayName;
if (uiAction == null) {
uiAction = EntityUIAction();
}
2019-09-07 17:04:40 +03:00
unitOfMeasurement = entity.unitOfMeasurement;
2018-12-14 16:31:41 +02:00
}
2018-11-15 19:08:47 +02:00
}
void handleTap() {
2018-12-14 14:28:23 +02:00
switch (uiAction.tapAction) {
case EntityUIAction.toggle: {
2019-11-08 21:37:41 +02:00
ConnectionManager().callService(domain: "homeassistant", service: "toggle", entityId: entity.entityId);
break;
}
2018-12-14 14:28:23 +02:00
case EntityUIAction.callService: {
if (uiAction.tapService != null) {
2019-11-08 21:37:41 +02:00
ConnectionManager().callService(
domain: uiAction.tapService.split(".")[0],
service: uiAction.tapService.split(".")[1],
data: uiAction.tapServiceData
);
2018-12-14 14:28:23 +02:00
}
break;
}
2018-12-14 14:28:23 +02:00
case EntityUIAction.none: {
break;
}
2018-12-14 14:28:23 +02:00
case EntityUIAction.moreInfo: {
eventBus.fire(
2019-09-14 18:32:44 +03:00
new ShowEntityPageEvent(entity: entity));
break;
}
2018-12-14 14:28:23 +02:00
case EntityUIAction.navigate: {
2019-11-29 14:45:59 +02:00
if (uiAction.tapService != null && uiAction.tapService.startsWith("/")) {
//TODO handle local urls
Logger.w("Local urls is not supported yet");
} else {
Launcher.launchURL(uiAction.tapService);
}
break;
}
2018-12-14 14:28:23 +02:00
default: {
break;
}
}
}
void handleHold() {
2018-12-14 14:28:23 +02:00
switch (uiAction.holdAction) {
case EntityUIAction.toggle: {
2019-11-08 21:37:41 +02:00
ConnectionManager().callService(domain: "homeassistant", service: "toggle", entityId: entity.entityId);
break;
}
2018-12-14 14:28:23 +02:00
case EntityUIAction.callService: {
if (uiAction.holdService != null) {
2019-11-08 21:37:41 +02:00
ConnectionManager().callService(
domain: uiAction.holdService.split(".")[0],
service: uiAction.holdService.split(".")[1],
data: uiAction.holdServiceData
);
2018-12-14 14:28:23 +02:00
}
break;
}
2018-12-14 14:28:23 +02:00
case EntityUIAction.moreInfo: {
eventBus.fire(
2019-09-14 18:32:44 +03:00
new ShowEntityPageEvent(entity: entity));
break;
}
case EntityUIAction.navigate: {
2019-11-29 14:45:59 +02:00
if (uiAction.holdService != null && uiAction.holdService.startsWith("/")) {
//TODO handle local urls
Logger.w("Local urls is not supported yet");
} else {
Launcher.launchURL(uiAction.holdService);
}
break;
}
default: {
break;
}
}
}
2018-11-15 19:08:47 +02:00
}