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/view.class.dart

223 lines
8.1 KiB
Dart
Raw Normal View History

2019-09-07 18:23:04 +03:00
part of 'main.dart';
2018-10-27 17:28:47 +03:00
class HAView {
List<HACard> cards = [];
List<Entity> badges = [];
Entity linkedEntity;
2020-03-22 01:11:00 +02:00
String name;
String id;
String iconName;
final int count;
2020-03-22 01:11:00 +02:00
bool isPanel;
2018-10-27 17:28:47 +03:00
2020-03-22 01:11:00 +02:00
HAView({@required this.count, @required rawData}) {
id = "${rawData['id']}";
name = rawData['title'];
iconName = rawData['icon'];
isPanel = rawData['panel'] ?? false;
if (rawData['badges'] != null && rawData['badges'] is List) {
rawData['badges'].forEach((entity) {
if (entity is String) {
if (HomeAssistant().entities.isExist(entity)) {
Entity e = HomeAssistant().entities.get(entity);
badges.add(e);
}
} else {
String eId = '${entity['entity']}';
if (HomeAssistant().entities.isExist(eId)) {
Entity e = HomeAssistant().entities.get(eId);
badges.add(e);
}
}
});
}
2020-04-25 17:15:19 +03:00
cards.addAll(_createLovelaceCards(rawData["cards"] ?? [], 1));
2018-10-27 17:28:47 +03:00
}
2020-04-25 17:15:19 +03:00
List<HACard> _createLovelaceCards(List rawCards, int depth) {
2020-03-22 01:11:00 +02:00
List<HACard> result = [];
rawCards.forEach((rawCard){
try {
//bool isThereCardOptionsInside = rawCard["card"] != null;
var rawCardInfo = rawCard["card"] ?? rawCard;
2018-11-11 19:51:02 +02:00
HACard card = HACard(
2020-03-22 01:11:00 +02:00
id: "card",
name: rawCardInfo["title"] ?? rawCardInfo["name"],
type: rawCardInfo['type'] ?? CardType.ENTITIES,
icon: rawCardInfo['icon'],
2020-03-22 01:11:00 +02:00
columnsCount: rawCardInfo['columns'] ?? 4,
showName: (rawCardInfo['show_name'] ?? rawCard['show_name']) ?? true,
2020-04-13 16:05:59 +03:00
showHeaderToggle: (rawCardInfo['show_header_toggle'] ?? rawCard['show_header_toggle']) ?? false,
2020-03-22 01:11:00 +02:00
showState: (rawCardInfo['show_state'] ?? rawCard['show_state']) ?? true,
showEmpty: (rawCardInfo['show_empty'] ?? rawCard['show_empty']) ?? true,
stateFilter: (rawCard['state_filter'] ?? rawCardInfo['state_filter']) ?? [],
states: rawCardInfo['states'],
conditions: rawCard['conditions'] ?? [],
content: rawCardInfo['content'],
min: rawCardInfo['min'] ?? 0,
max: rawCardInfo['max'] ?? 100,
unit: rawCardInfo['unit'],
2020-04-25 17:15:19 +03:00
depth: depth,
2020-03-22 01:11:00 +02:00
severity: rawCardInfo['severity']
2018-11-11 19:51:02 +02:00
);
2020-03-22 01:11:00 +02:00
if (rawCardInfo["cards"] != null) {
2020-04-25 17:15:19 +03:00
card.childCards = _createLovelaceCards(rawCardInfo["cards"], depth + 1);
2020-03-22 01:11:00 +02:00
}
var rawEntities = rawCard["entities"] ?? rawCardInfo["entities"];
var rawSingleEntity = rawCard["entity"] ?? rawCardInfo["entity"];
if (rawEntities != null) {
rawEntities.forEach((rawEntity) {
if (rawEntity is String) {
if (HomeAssistant().entities.isExist(rawEntity)) {
card.entities.add(EntityWrapper(entity: HomeAssistant().entities.get(rawEntity)));
} else {
card.entities.add(EntityWrapper(entity: Entity.missed(rawEntity)));
}
2020-03-22 01:11:00 +02:00
} else {
if (rawEntity["type"] == "divider") {
card.entities.add(EntityWrapper(entity: Entity.divider()));
} else if (rawEntity["type"] == "section") {
card.entities.add(EntityWrapper(entity: Entity.section(rawEntity["label"] ?? "")));
} else if (rawEntity["type"] == "call-service") {
Map uiActionData = {
"tap_action": {
"action": EntityUIAction.callService,
"service": rawEntity["service"],
"service_data": rawEntity["service_data"]
},
"hold_action": EntityUIAction.none
};
card.entities.add(EntityWrapper(
2020-03-22 01:11:00 +02:00
entity: Entity.callService(
icon: rawEntity["icon"],
name: rawEntity["name"],
service: rawEntity["service"],
actionName: rawEntity["action_name"]
),
uiAction: EntityUIAction(rawEntityData: uiActionData)
)
);
} else if (rawEntity["type"] == "weblink") {
Map uiActionData = {
"tap_action": {
"action": EntityUIAction.navigate,
"service": rawEntity["url"]
},
"hold_action": EntityUIAction.none
};
card.entities.add(EntityWrapper(
entity: Entity.weblink(
icon: rawEntity["icon"],
name: rawEntity["name"],
url: rawEntity["url"]
),
uiAction: EntityUIAction(rawEntityData: uiActionData)
)
);
} else if (HomeAssistant().entities.isExist(rawEntity["entity"])) {
Entity e = HomeAssistant().entities.get(rawEntity["entity"]);
card.entities.add(
EntityWrapper(
entity: e,
overrideName: rawEntity["name"],
overrideIcon: rawEntity["icon"],
stateFilter: rawEntity['state_filter'] ?? [],
uiAction: EntityUIAction(rawEntityData: rawEntity)
)
);
} else {
card.entities.add(EntityWrapper(entity: Entity.missed(rawEntity["entity"])));
}
2020-03-22 01:11:00 +02:00
}
});
} else if (rawSingleEntity != null) {
2020-03-22 01:11:00 +02:00
var en = rawSingleEntity;
if (en is String) {
if (HomeAssistant().entities.isExist(en)) {
Entity e = HomeAssistant().entities.get(en);
card.linkedEntityWrapper = EntityWrapper(
entity: e,
overrideIcon: rawCardInfo["icon"],
overrideName: rawCardInfo["name"],
uiAction: EntityUIAction(rawEntityData: rawCard)
);
} else {
card.linkedEntityWrapper = EntityWrapper(entity: Entity.missed(en));
}
} else {
if (HomeAssistant().entities.isExist(en["entity"])) {
Entity e = HomeAssistant().entities.get(en["entity"]);
card.linkedEntityWrapper = EntityWrapper(
entity: e,
overrideIcon: en["icon"],
overrideName: en["name"],
stateFilter: en['state_filter'] ?? [],
uiAction: EntityUIAction(rawEntityData: rawCard)
);
} else {
card.linkedEntityWrapper = EntityWrapper(entity: Entity.missed(en["entity"]));
}
}
} else {
card.linkedEntityWrapper = EntityWrapper(
entity: Entity.ghost(
card.name,
card.icon,
),
uiAction: EntityUIAction(
rawEntityData: rawCardInfo
)
);
2020-03-22 01:11:00 +02:00
}
result.add(card);
} catch (e) {
Logger.e("There was an error parsing card: ${e.toString()}");
2018-10-27 17:28:47 +03:00
}
});
2020-03-22 01:11:00 +02:00
return result;
2018-10-27 17:28:47 +03:00
}
2018-11-04 22:19:45 +02:00
Widget buildTab() {
if (linkedEntity == null) {
if (iconName != null && iconName.isNotEmpty) {
2018-11-04 22:19:45 +02:00
return
Tab(
icon:
Icon(
MaterialDesignIcons.getIconDataFromIconName(
2018-11-04 22:19:45 +02:00
iconName ?? "mdi:home-assistant"),
size: 24.0,
)
);
} else {
return
Tab(
2019-09-18 21:14:21 +03:00
text: "${name?.toUpperCase() ?? "UNNAMED VIEW"}",
2018-11-04 22:19:45 +02:00
);
}
} else {
if (linkedEntity.icon != null && linkedEntity.icon.length > 0) {
return Tab(
icon: Icon(
MaterialDesignIcons.getIconDataFromIconName(
2018-11-04 22:19:45 +02:00
linkedEntity.icon),
size: 24.0,
)
);
} else {
return Tab(
2019-09-18 21:14:21 +03:00
text: "${linkedEntity.displayName?.toUpperCase()}",
2018-11-04 22:19:45 +02:00
);
}
}
}
2018-10-27 17:28:47 +03:00
Widget build(BuildContext context) {
return ViewWidget(
view: this,
);
}
}