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,6 +215,7 @@ class HassioDataModel {
_servicesCompleter.completeError({"errorCode": 4, "errorMessage": response["error"]["message"]}); _servicesCompleter.completeError({"errorCode": 4, "errorMessage": response["error"]["message"]});
return; return;
} }
try {
Map data = response["result"]; Map data = response["result"];
Map result = {}; Map result = {};
debugPrint("Parsing ${data.length} Home Assistant service domains"); debugPrint("Parsing ${data.length} Home Assistant service domains");
@ -228,6 +229,11 @@ class HassioDataModel {
}); });
_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,33 +247,42 @@ 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) {
try {
var composedEntity = _parseEntity(entity); var composedEntity = _parseEntity(entity);
if (composedEntity["attributes"] != null) { if (composedEntity["attributes"] != null) {
if ((composedEntity["domain"] == "group")&&(composedEntity["attributes"]["view"] == true)) { if ((composedEntity["domain"] == "group") &&
(composedEntity["attributes"]["view"] == true)) {
uiGroups.add(composedEntity["entity_id"]); uiGroups.add(composedEntity["entity_id"]);
} }
} }
_entitiesData[entity["entity_id"]] = composedEntity;
_entitiesData[composedEntity["entity_id"]] = composedEntity; } catch (error) {
debugPrint("Error parsing entity: ${entity['entity_id']}");
debugPrint("$error");
}
}); });
//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
try {
Map viewGroupStructure = {};
viewCounter += 1; viewCounter += 1;
var viewGroup = _entitiesData[viewId]; var viewGroup = _entitiesData[viewId];
Map viewGroupStructure = {};
if (viewGroup != null) { if (viewGroup != null) {
viewGroupStructure["groups"] = {}; viewGroupStructure["groups"] = {};
viewGroupStructure["state"] = "on"; viewGroupStructure["state"] = "on";
viewGroupStructure["entity_id"] = viewGroup["entity_id"]; viewGroupStructure["entity_id"] = viewGroup["entity_id"];
viewGroupStructure["badges"] = {"children": []}; viewGroupStructure["badges"] = {"children": []};
viewGroupStructure["attributes"] = viewGroup["attributes"] != null ? {"icon": viewGroup["attributes"]["icon"]} : {"icon": "none"}; 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((
entityId) { //Each entity or group in view
Map newGroup = {}; Map newGroup = {};
String domain = _entitiesData[entityId]["domain"]; String domain = _entitiesData[entityId]["domain"];
if (domain != "group") { if (domain != "group") {
@ -291,7 +306,8 @@ class HassioDataModel {
newGroup["entity_id"] = entityId; newGroup["entity_id"] = entityId;
newGroup["friendly_name"] = newGroup["friendly_name"] =
(_entitiesData[entityId]['attributes'] != null) (_entitiesData[entityId]['attributes'] != null)
? (_entitiesData[entityId]['attributes']['friendly_name'] ?? "") ? (_entitiesData[entityId]['attributes']['friendly_name'] ??
"")
: ""; : "";
newGroup["children"] = List<String>(); newGroup["children"] = List<String>();
_entitiesData[entityId]["attributes"]["entity_id"].forEach(( _entitiesData[entityId]["attributes"]["entity_id"].forEach((
@ -301,7 +317,10 @@ class HassioDataModel {
viewGroupStructure["groups"]["$entityId"] = Map.from(newGroup); viewGroupStructure["groups"]["$entityId"] = Map.from(newGroup);
} }
}); });
}
_uiStructure[viewId.split(".")[1]] = viewGroupStructure; _uiStructure[viewId.split(".")[1]] = viewGroupStructure;
} catch (error) {
debugPrint("Error parsing view: $viewId");
} }
}); });
_statesCompleter.complete(); _statesCompleter.complete();