Skiping entity and views that cause parsing errors

This commit is contained in:
estevez 2018-09-23 22:20:48 +03:00
parent 4dc211f2f7
commit 86738a0515

View File

@ -49,7 +49,7 @@ class HassioDataModel {
debugPrint("Previous fetch is not complited"); debugPrint("Previous fetch is not complited");
} else { } else {
//TODO: Fetch timeout timer. Should be removed after #21 fix //TODO: Fetch timeout timer. Should be removed after #21 fix
_fetchingTimer = Timer(Duration(seconds: 10), () { _fetchingTimer = Timer(Duration(seconds: 15), () {
closeConnection(); closeConnection();
_fetchCompleter.completeError({"errorCode" : 1,"errorMessage": "Connection timeout"}); _fetchCompleter.completeError({"errorCode" : 1,"errorMessage": "Connection timeout"});
}); });
@ -215,19 +215,25 @@ class HassioDataModel {
_servicesCompleter.completeError({"errorCode": 4, "errorMessage": response["error"]["message"]}); _servicesCompleter.completeError({"errorCode": 4, "errorMessage": response["error"]["message"]});
return; return;
} }
Map data = response["result"]; try {
Map result = {}; Map data = response["result"];
debugPrint("Parsing ${data.length} Home Assistant service domains"); Map result = {};
data.forEach((domain, services){ debugPrint("Parsing ${data.length} Home Assistant service domains");
result[domain] = Map.from(services); data.forEach((domain, services) {
services.forEach((serviceName, serviceData){ result[domain] = Map.from(services);
if (_entitiesData["$domain.$serviceName"] != null) { services.forEach((serviceName, serviceData) {
result[domain].remove(serviceName); if (_entitiesData["$domain.$serviceName"] != null) {
} result[domain].remove(serviceName);
}
});
}); });
}); _servicesData = result;
_servicesData = result; _servicesCompleter.complete();
_servicesCompleter.complete(); } catch (e) {
//TODO hadle it properly
debugPrint("Error parsing services");
_servicesCompleter.complete();
}
} }
void _parseEntities(response) async { void _parseEntities(response) async {
@ -241,67 +247,80 @@ class HassioDataModel {
debugPrint("Parsing ${data.length} Home Assistant entities"); debugPrint("Parsing ${data.length} Home Assistant entities");
List<String> uiGroups = []; List<String> uiGroups = [];
data.forEach((entity) { data.forEach((entity) {
var composedEntity = _parseEntity(entity); try {
var composedEntity = _parseEntity(entity);
if (composedEntity["attributes"] != null) { if (composedEntity["attributes"] != null) {
if ((composedEntity["domain"] == "group")&&(composedEntity["attributes"]["view"] == true)) { if ((composedEntity["domain"] == "group") &&
uiGroups.add(composedEntity["entity_id"]); (composedEntity["attributes"]["view"] == true)) {
uiGroups.add(composedEntity["entity_id"]);
}
} }
_entitiesData[entity["entity_id"]] = composedEntity;
} catch (error) {
debugPrint("Error parsing entity: ${entity['entity_id']}");
debugPrint("$error");
} }
_entitiesData[composedEntity["entity_id"]] = composedEntity;
}); });
//Gethering information for UI //Gethering information for UI
debugPrint("Gethering views"); debugPrint("Gethering views");
int viewCounter = 0; int viewCounter = 0;
uiGroups.forEach((viewId) { //Each view uiGroups.forEach((viewId) { //Each view
viewCounter +=1; try {
var viewGroup = _entitiesData[viewId]; Map viewGroupStructure = {};
Map viewGroupStructure = {}; viewCounter += 1;
if (viewGroup != null) { var viewGroup = _entitiesData[viewId];
viewGroupStructure["groups"] = {}; if (viewGroup != null) {
viewGroupStructure["state"] = "on"; viewGroupStructure["groups"] = {};
viewGroupStructure["entity_id"] = viewGroup["entity_id"]; viewGroupStructure["state"] = "on";
viewGroupStructure["badges"] = {"children": []}; viewGroupStructure["entity_id"] = viewGroup["entity_id"];
viewGroupStructure["attributes"] = viewGroup["attributes"] != null ? {"icon": viewGroup["attributes"]["icon"]} : {"icon": "none"}; viewGroupStructure["badges"] = {"children": []};
viewGroupStructure["attributes"] = viewGroup["attributes"] != null ? {
"icon": viewGroup["attributes"]["icon"]
} : {"icon": "none"};
viewGroup["attributes"]["entity_id"].forEach((entityId) { //Each entity or group in view viewGroup["attributes"]["entity_id"].forEach((
Map newGroup = {}; entityId) { //Each entity or group in view
String domain = _entitiesData[entityId]["domain"]; Map newGroup = {};
if (domain != "group") { String domain = _entitiesData[entityId]["domain"];
if (_topBadgeDomains.contains(domain)) { if (domain != "group") {
viewGroupStructure["badges"]["children"].add(entityId); if (_topBadgeDomains.contains(domain)) {
} else { viewGroupStructure["badges"]["children"].add(entityId);
String autoGroupID = "$domain.$domain$viewCounter";
if (viewGroupStructure["groups"]["$autoGroupID"] == null) {
newGroup["entity_id"] = "$domain.$domain$viewCounter";
newGroup["friendly_name"] = "$domain";
newGroup["children"] = [];
newGroup["children"].add(entityId);
viewGroupStructure["groups"]["$autoGroupID"] =
Map.from(newGroup);
} else { } else {
viewGroupStructure["groups"]["$autoGroupID"]["children"].add( String autoGroupID = "$domain.$domain$viewCounter";
entityId); if (viewGroupStructure["groups"]["$autoGroupID"] == null) {
newGroup["entity_id"] = "$domain.$domain$viewCounter";
newGroup["friendly_name"] = "$domain";
newGroup["children"] = [];
newGroup["children"].add(entityId);
viewGroupStructure["groups"]["$autoGroupID"] =
Map.from(newGroup);
} else {
viewGroupStructure["groups"]["$autoGroupID"]["children"].add(
entityId);
}
} }
} else {
newGroup["entity_id"] = entityId;
newGroup["friendly_name"] =
(_entitiesData[entityId]['attributes'] != null)
? (_entitiesData[entityId]['attributes']['friendly_name'] ??
"")
: "";
newGroup["children"] = List<String>();
_entitiesData[entityId]["attributes"]["entity_id"].forEach((
groupedEntityId) {
newGroup["children"].add(groupedEntityId);
});
viewGroupStructure["groups"]["$entityId"] = Map.from(newGroup);
} }
} else { });
newGroup["entity_id"] = entityId; }
newGroup["friendly_name"] = _uiStructure[viewId.split(".")[1]] = viewGroupStructure;
(_entitiesData[entityId]['attributes'] != null) } catch (error) {
? (_entitiesData[entityId]['attributes']['friendly_name'] ?? "") debugPrint("Error parsing view: $viewId");
: "";
newGroup["children"] = List<String>();
_entitiesData[entityId]["attributes"]["entity_id"].forEach((
groupedEntityId) {
newGroup["children"].add(groupedEntityId);
});
viewGroupStructure["groups"]["$entityId"] = Map.from(newGroup);
}
});
_uiStructure[viewId.split(".")[1]] = viewGroupStructure;
} }
}); });
_statesCompleter.complete(); _statesCompleter.complete();