Resolves #205, Resolves #417 Condotional cards support

This commit is contained in:
estevez-dev 2019-08-26 17:03:28 +03:00
parent fbbb96409d
commit 37155901ef
3 changed files with 37 additions and 25 deletions

View File

@ -296,31 +296,25 @@ class HomeAssistant {
List<HACard> result = []; List<HACard> result = [];
rawCards.forEach((rawCard){ rawCards.forEach((rawCard){
try { try {
bool isThereCardOptionsInside = rawCard["card"] != null; //bool isThereCardOptionsInside = rawCard["card"] != null;
var rawCardInfo = rawCard["card"] ?? rawCard;
HACard card = HACard( HACard card = HACard(
id: "card", id: "card",
name: isThereCardOptionsInside ? rawCard["card"]["title"] ?? name: rawCardInfo["title"] ?? rawCardInfo["name"],
rawCard["card"]["name"] : rawCard["title"] ?? rawCard["name"], type: rawCardInfo['type'],
type: isThereCardOptionsInside columnsCount: rawCardInfo['columns'] ?? 4,
? rawCard["card"]['type'] showName: rawCardInfo['show_name'] ?? true,
: rawCard['type'], showState: rawCardInfo['show_state'] ?? true,
columnsCount: isThereCardOptionsInside showEmpty: rawCardInfo['show_empty'] ?? true,
? rawCard["card"]['columns'] ?? 4 stateFilter: rawCardInfo['state_filter'] ?? [],
: rawCard['columns'] ?? 4, states: rawCardInfo['states'],
showName: isThereCardOptionsInside ? rawCard["card"]['show_name'] ?? conditions: rawCard['conditions'] ?? [],
true : rawCard['show_name'] ?? true, content: rawCardInfo['content']
showState: isThereCardOptionsInside
? rawCard["card"]['show_state'] ?? true
: rawCard['show_state'] ?? true,
showEmpty: rawCard['show_empty'] ?? true,
stateFilter: rawCard['state_filter'] ?? [],
states: rawCard['states'],
content: rawCard['content']
); );
if (rawCard["cards"] != null) { if (rawCardInfo["cards"] != null) {
card.childCards = _createLovelaceCards(rawCard["cards"]); card.childCards = _createLovelaceCards(rawCardInfo["cards"]);
} }
rawCard["entities"]?.forEach((rawEntity) { rawCardInfo["entities"]?.forEach((rawEntity) {
if (rawEntity is String) { if (rawEntity is String) {
if (entities.isExist(rawEntity)) { if (entities.isExist(rawEntity)) {
card.entities.add(EntityWrapper(entity: entities.get(rawEntity))); card.entities.add(EntityWrapper(entity: entities.get(rawEntity)));
@ -383,15 +377,15 @@ class HomeAssistant {
} }
} }
}); });
if (rawCard["entity"] != null) { if (rawCardInfo["entity"] != null) {
var en = rawCard["entity"]; var en = rawCardInfo["entity"];
if (en is String) { if (en is String) {
if (entities.isExist(en)) { if (entities.isExist(en)) {
Entity e = entities.get(en); Entity e = entities.get(en);
card.linkedEntityWrapper = EntityWrapper( card.linkedEntityWrapper = EntityWrapper(
entity: e, entity: e,
icon: rawCard["icon"], icon: rawCardInfo["icon"],
displayName: rawCard["name"], displayName: rawCardInfo["name"],
uiAction: EntityUIAction(rawEntityData: rawCard) uiAction: EntityUIAction(rawEntityData: rawCard)
); );
} else { } else {

View File

@ -13,6 +13,7 @@ class HACard {
int columnsCount; int columnsCount;
List stateFilter; List stateFilter;
List states; List states;
List conditions;
String content; String content;
HACard({ HACard({
@ -26,6 +27,7 @@ class HACard {
this.showEmpty: true, this.showEmpty: true,
this.content, this.content,
this.states, this.states,
this.conditions,
@required this.type @required this.type
}); });

View File

@ -24,6 +24,22 @@ class CardWidget extends StatelessWidget {
} }
} }
if (card.conditions.isNotEmpty) {
bool showCardByConditions = false;
for (var condition in card.conditions) {
Entity conditionEntity = HomeAssistant().entities.get(condition['entity']);
if (conditionEntity != null &&
(condition['state'] != null && conditionEntity.state == condition['state']) ||
(condition['state_not'] != null && conditionEntity.state != condition['state_not'])
) {
showCardByConditions = true;
}
}
if (!showCardByConditions) {
return Container(width: 0.0, height: 0.0,);
}
}
switch (card.type) { switch (card.type) {
case CardType.entities: { case CardType.entities: {