2018-09-26 22:16:50 +03:00
|
|
|
part of 'main.dart';
|
|
|
|
|
|
|
|
class EntityCollection {
|
|
|
|
|
|
|
|
Map<String, Entity> _entities;
|
2018-09-28 10:15:25 +03:00
|
|
|
List<String> viewList;
|
2018-09-26 22:16:50 +03:00
|
|
|
|
|
|
|
EntityCollection() {
|
|
|
|
_entities = {};
|
2018-09-28 10:15:25 +03:00
|
|
|
viewList = [];
|
2018-09-26 22:16:50 +03:00
|
|
|
}
|
|
|
|
|
2018-09-28 13:33:15 +03:00
|
|
|
bool get hasDefaultView => _entities["group.default_view"] != null;
|
|
|
|
|
2018-09-27 14:51:57 +03:00
|
|
|
void parse(List rawData) {
|
2018-09-26 22:16:50 +03:00
|
|
|
_entities.clear();
|
2018-09-28 10:15:25 +03:00
|
|
|
viewList.clear();
|
2018-09-27 14:51:57 +03:00
|
|
|
|
|
|
|
TheLogger.log("Debug","Parsing ${rawData.length} Home Assistant entities");
|
|
|
|
rawData.forEach((rawEntityData) {
|
|
|
|
Entity newEntity = addFromRaw(rawEntityData);
|
|
|
|
|
|
|
|
if (newEntity.isView) {
|
2018-09-28 10:15:25 +03:00
|
|
|
viewList.add(newEntity.entityId);
|
2018-09-27 14:51:57 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-29 13:49:25 +03:00
|
|
|
Entity _createEntityInstance(rawEntityData) {
|
|
|
|
switch (rawEntityData["entity_id"].split(".")[0]) {
|
|
|
|
case "automation":
|
|
|
|
case "input_boolean ":
|
|
|
|
case "switch":
|
|
|
|
case "light": {
|
|
|
|
return SwitchEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
|
|
|
|
case "script":
|
|
|
|
case "scene": {
|
|
|
|
return ButtonEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
|
2018-09-30 23:12:27 +03:00
|
|
|
case "input_datetime": {
|
|
|
|
return DateTimeEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
|
2018-09-30 20:57:07 +03:00
|
|
|
case "input_select": {
|
|
|
|
return SelectEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
|
2018-09-29 13:49:25 +03:00
|
|
|
case "input_number": {
|
2018-09-30 20:24:13 +03:00
|
|
|
return SliderEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
|
|
|
|
case "input_text": {
|
2018-09-30 20:57:07 +03:00
|
|
|
return TextEntity(rawEntityData);
|
2018-09-29 13:49:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
return Entity(rawEntityData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:51:57 +03:00
|
|
|
void updateState(Map rawStateData) {
|
|
|
|
if (isExist(rawStateData["entity_id"])) {
|
2018-09-29 12:02:41 +03:00
|
|
|
updateFromRaw(rawStateData["new_state"] ?? rawStateData["old_state"]);
|
2018-09-27 14:51:57 +03:00
|
|
|
} else {
|
2018-09-29 12:02:41 +03:00
|
|
|
addFromRaw(rawStateData["new_state"] ?? rawStateData["old_state"]);
|
2018-09-26 22:16:50 +03:00
|
|
|
}
|
2018-09-27 14:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void add(Entity entity) {
|
|
|
|
_entities[entity.entityId] = entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
Entity addFromRaw(Map rawEntityData) {
|
2018-09-29 13:49:25 +03:00
|
|
|
Entity entity = _createEntityInstance(rawEntityData);
|
2018-09-27 14:51:57 +03:00
|
|
|
_entities[entity.entityId] = entity;
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateFromRaw(Map rawEntityData) {
|
|
|
|
//TODO pass entity in this function and call update from it
|
|
|
|
_entities[rawEntityData["entity_id"]].update(rawEntityData);
|
|
|
|
}
|
|
|
|
|
|
|
|
Entity get(String entityId) {
|
|
|
|
return _entities[entityId];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isExist(String entityId) {
|
|
|
|
return _entities[entityId] != null;
|
|
|
|
}
|
|
|
|
|
2018-09-28 13:33:15 +03:00
|
|
|
Map<String,List<String>> getDefaultViewTopLevelEntities() {
|
|
|
|
Map<String,List<String>> result = {"userGroups": [], "notGroupedEntities": []};
|
|
|
|
List<String> entities = [];
|
|
|
|
_entities.forEach((id, entity){
|
|
|
|
if ((id.indexOf("group.") == 0) && (id.indexOf(".all_") == -1) && (!entity.isView)) {
|
|
|
|
result["userGroups"].add(id);
|
|
|
|
}
|
|
|
|
if (!entity.isGroup) {
|
|
|
|
entities.add(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
entities.forEach((entiyId) {
|
|
|
|
bool foundInGroup = false;
|
|
|
|
result["userGroups"].forEach((userGroupId) {
|
|
|
|
if (_entities[userGroupId].childEntities.contains(entiyId)) {
|
|
|
|
foundInGroup = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!foundInGroup) {
|
|
|
|
result["notGroupedEntities"].add(entiyId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:16:50 +03:00
|
|
|
}
|