Resolves #512 Support all state_filter options
This commit is contained in:
@ -50,10 +50,67 @@ class HACard {
|
|||||||
if (!ConnectionManager().useLovelace && entityWrapper.entity.isHidden) {
|
if (!ConnectionManager().useLovelace && entityWrapper.entity.isHidden) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (stateFilter.isNotEmpty) {
|
List currentStateFilter;
|
||||||
return stateFilter.contains(entityWrapper.entity.state);
|
if (entityWrapper.stateFilter != null && entityWrapper.stateFilter.isNotEmpty) {
|
||||||
|
currentStateFilter = entityWrapper.stateFilter;
|
||||||
|
} else {
|
||||||
|
currentStateFilter = stateFilter;
|
||||||
}
|
}
|
||||||
return true;
|
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();
|
}).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,7 +221,7 @@ class Entity {
|
|||||||
|
|
||||||
String getAttribute(String attributeName) {
|
String getAttribute(String attributeName) {
|
||||||
if (attributes != null) {
|
if (attributes != null) {
|
||||||
return attributes["$attributeName"];
|
return attributes["$attributeName"].toString();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -8,13 +8,15 @@ class EntityWrapper {
|
|||||||
String entityPicture;
|
String entityPicture;
|
||||||
EntityUIAction uiAction;
|
EntityUIAction uiAction;
|
||||||
Entity entity;
|
Entity entity;
|
||||||
|
List stateFilter;
|
||||||
|
|
||||||
|
|
||||||
EntityWrapper({
|
EntityWrapper({
|
||||||
this.entity,
|
this.entity,
|
||||||
String icon,
|
String icon,
|
||||||
String displayName,
|
String displayName,
|
||||||
this.uiAction
|
this.uiAction,
|
||||||
|
this.stateFilter
|
||||||
}) {
|
}) {
|
||||||
if (entity.statelessType == StatelessEntityType.NONE || entity.statelessType == StatelessEntityType.CALL_SERVICE || entity.statelessType == StatelessEntityType.WEBLINK) {
|
if (entity.statelessType == StatelessEntityType.NONE || entity.statelessType == StatelessEntityType.CALL_SERVICE || entity.statelessType == StatelessEntityType.WEBLINK) {
|
||||||
this.icon = icon ?? entity.icon;
|
this.icon = icon ?? entity.icon;
|
||||||
|
@ -303,6 +303,7 @@ class HomeAssistant {
|
|||||||
entity: e,
|
entity: e,
|
||||||
displayName: rawEntity["name"],
|
displayName: rawEntity["name"],
|
||||||
icon: rawEntity["icon"],
|
icon: rawEntity["icon"],
|
||||||
|
stateFilter: rawEntity['state_filter'] ?? [],
|
||||||
uiAction: EntityUIAction(rawEntityData: rawEntity)
|
uiAction: EntityUIAction(rawEntityData: rawEntity)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -333,6 +334,7 @@ class HomeAssistant {
|
|||||||
entity: e,
|
entity: e,
|
||||||
icon: en["icon"],
|
icon: en["icon"],
|
||||||
displayName: en["name"],
|
displayName: en["name"],
|
||||||
|
stateFilter: en['state_filter'] ?? [],
|
||||||
uiAction: EntityUIAction(rawEntityData: rawCard)
|
uiAction: EntityUIAction(rawEntityData: rawCard)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -74,7 +74,7 @@ class HAView {
|
|||||||
|
|
||||||
Widget buildTab() {
|
Widget buildTab() {
|
||||||
if (linkedEntity == null) {
|
if (linkedEntity == null) {
|
||||||
if (iconName != null) {
|
if (iconName != null && iconName.isNotEmpty) {
|
||||||
return
|
return
|
||||||
Tab(
|
Tab(
|
||||||
icon:
|
icon:
|
||||||
|
Reference in New Issue
Block a user