Massive refactoring: UIBuilder, Vew, HACArd, Badge

This commit is contained in:
estevez
2018-09-28 10:15:25 +03:00
parent 375ae36884
commit 098a556279
7 changed files with 170 additions and 97 deletions

View File

@ -3,30 +3,25 @@ part of 'main.dart';
class EntityCollection {
Map<String, Entity> _entities;
Map<String, dynamic> _uiStructure = {};
List _topBadgeDomains = ["alarm_control_panel", "binary_sensor", "device_tracker", "updater", "sun", "timer", "sensor"];
List<String> viewList;
EntityCollection() {
_entities = {};
viewList = [];
}
Map<String, dynamic> get ui => _uiStructure;
void parse(List rawData) {
_entities.clear();
_uiStructure.clear();
viewList.clear();
TheLogger.log("Debug","Parsing ${rawData.length} Home Assistant entities");
rawData.forEach((rawEntityData) {
Entity newEntity = addFromRaw(rawEntityData);
if (newEntity.isView) {
_uiStructure.addAll({newEntity.entityId: {}});
viewList.add(newEntity.entityId);
}
});
_createViews();
}
void updateState(Map rawStateData) {
@ -60,66 +55,4 @@ class EntityCollection {
return _entities[entityId] != null;
}
void _createViews() async {
TheLogger.log("Debug","Gethering views");
int viewCounter = 0;
_uiStructure.forEach((viewId, viewStructure) { //Each view
try {
viewCounter += 1;
Entity viewGroupData = get(viewId);
if (viewGroupData != null) {
viewStructure["groups"] = {};
viewStructure["state"] = "on";
viewStructure["entity_id"] = viewGroupData.entityId;
viewStructure["badges"] = {"children": []};
viewStructure["attributes"] = {
"icon": viewGroupData.icon
};
viewGroupData.childEntities.forEach((
entityId) { //Each entity or group in view
Map newGroup = {};
if (isExist(entityId)) {
Entity cardOrEntityData = get(entityId);
if (!cardOrEntityData.isGroup) {
if (_topBadgeDomains.contains(cardOrEntityData.domain)) {
viewStructure["badges"]["children"].add(entityId);
} else {
String autoGroupID = "${cardOrEntityData.domain}.${cardOrEntityData.domain}$viewCounter";
if (viewStructure["groups"]["$autoGroupID"] == null) {
newGroup["entity_id"] = autoGroupID;
newGroup["friendly_name"] = cardOrEntityData.domain;
newGroup["children"] = [];
newGroup["children"].add(entityId);
viewStructure["groups"]["$autoGroupID"] =
Map.from(newGroup);
} else {
viewStructure["groups"]["$autoGroupID"]["children"].add(
entityId);
}
}
} else {
newGroup["entity_id"] = entityId;
newGroup["friendly_name"] = cardOrEntityData.displayName;
newGroup["children"] = List<String>();
cardOrEntityData.childEntities.forEach((
groupedEntityId) {
newGroup["children"].add(groupedEntityId);
});
viewStructure["groups"]["$entityId"] = Map.from(newGroup);
}
} else {
TheLogger.log("Warning", "Unknown entity inside view: $entityId");
}
});
} else {
TheLogger.log("Warning", "No state or attributes found for view: $viewId");
}
} catch (error) {
TheLogger.log("Error","Error parsing view: $viewId");
}
});
}
}