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

123 lines
3.3 KiB
Dart
Raw Normal View History

2018-10-27 17:28:47 +03:00
part of '../main.dart';
class HACard {
2018-11-15 19:08:47 +02:00
List<EntityWrapper> entities = [];
2018-11-25 20:44:19 +02:00
List<HACard> childCards = [];
EntityWrapper linkedEntityWrapper;
2018-10-27 17:28:47 +03:00
String name;
String id;
String type;
2018-11-15 19:08:47 +02:00
bool showName;
bool showState;
bool showEmpty;
bool showHeaderToggle;
2018-11-14 19:52:17 +02:00
int columnsCount;
List stateFilter;
2019-01-29 15:00:15 +02:00
List states;
List conditions;
2019-01-23 23:34:45 +02:00
String content;
2019-09-07 15:47:09 +03:00
String unit;
int min;
int max;
Map severity;
2018-10-27 17:28:47 +03:00
HACard({
this.name,
this.id,
this.linkedEntityWrapper,
2018-11-14 19:52:17 +02:00
this.columnsCount: 4,
2018-11-15 19:08:47 +02:00
this.showName: true,
this.showHeaderToggle: true,
2018-11-15 19:08:47 +02:00
this.showState: true,
this.stateFilter: const [],
this.showEmpty: true,
2019-01-23 23:34:45 +02:00
this.content,
2019-01-29 15:00:15 +02:00
this.states,
this.conditions: const [],
2019-09-07 15:47:09 +03:00
this.unit,
this.min,
this.max,
this.severity,
2018-10-27 17:28:47 +03:00
@required this.type
2019-09-04 23:42:19 +03:00
}) {
if (this.columnsCount <= 0) {
this.columnsCount = 4;
}
}
2018-10-27 17:28:47 +03:00
List<EntityWrapper> getEntitiesToShow() {
return entities.where((entityWrapper) {
2019-10-09 20:45:46 +03:00
if (!ConnectionManager().useLovelace && entityWrapper.entity.isHidden) {
return false;
}
List currentStateFilter;
if (entityWrapper.stateFilter != null && entityWrapper.stateFilter.isNotEmpty) {
currentStateFilter = entityWrapper.stateFilter;
} else {
currentStateFilter = stateFilter;
2018-10-27 17:28:47 +03:00
}
bool showByFilter = currentStateFilter.isEmpty;
for (var allowedState in currentStateFilter) {
if (allowedState is String && allowedState == entityWrapper.entity.state) {
showByFilter = true;
break;
} else if (allowedState is Map) {
try {
var tmpVal = allowedState['attribute'] != null ? entityWrapper.entity.getAttribute(allowedState['attribute']) : entityWrapper.entity.state;
var valToCompareWith = allowedState['value'];
var valToCompare;
if (valToCompareWith is! String) {
valToCompare = double.tryParse(tmpVal);
} else {
valToCompare = tmpVal;
}
if (valToCompare != null) {
bool result;
switch (allowedState['operator']) {
case '<=': { result = valToCompare <= valToCompareWith;}
break;
case '<': { result = valToCompare < valToCompareWith;}
break;
case '>=': { result = valToCompare >= valToCompareWith;}
break;
case '>': { result = valToCompare > valToCompareWith;}
break;
case '!=': { result = valToCompare != valToCompareWith;}
break;
case 'regex': {
RegExp regExp = RegExp(valToCompareWith.toString());
result = regExp.hasMatch(valToCompare.toString());
}
break;
default: {
result = valToCompare == valToCompareWith;
}
}
if (result) {
showByFilter = true;
break;
}
}
} catch (e) {
Logger.e('Error filtering ${entityWrapper.entity.entityId} by $allowedState');
Logger.e('$e');
}
}
}
return showByFilter;
}).toList();
}
Widget build(BuildContext context) {
return CardWidget(
card: this,
);
2018-10-27 17:28:47 +03:00
}
}