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
|
|
|
|
2018-10-07 02:17:14 +03:00
|
|
|
bool get isEmpty => _entities.isEmpty;
|
|
|
|
|
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) {
|
2018-10-15 00:15:09 +03:00
|
|
|
switch (rawEntityData["entity_id"].split(".")[0]) {
|
|
|
|
case 'sun': {
|
|
|
|
return SunEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
case "automation":
|
|
|
|
case "input_boolean":
|
|
|
|
case "switch":
|
|
|
|
case "light": {
|
|
|
|
return SwitchEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
case "script":
|
|
|
|
case "scene": {
|
|
|
|
return ButtonEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
case "input_datetime": {
|
|
|
|
return DateTimeEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
case "input_select": {
|
|
|
|
return SelectEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
case "input_number": {
|
|
|
|
return SliderEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
case "input_text": {
|
|
|
|
return TextEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
case "climate": {
|
|
|
|
return ClimateEntity(rawEntityData);
|
|
|
|
}
|
2018-10-16 17:35:13 +03:00
|
|
|
case "cover": {
|
|
|
|
return CoverEntity(rawEntityData);
|
|
|
|
}
|
2018-10-15 00:15:09 +03:00
|
|
|
default: {
|
|
|
|
return Entity(rawEntityData);
|
|
|
|
}
|
|
|
|
}
|
2018-09-29 13:49:25 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2018-10-07 10:41:41 +03:00
|
|
|
get("${rawEntityData["entity_id"]}")?.update(rawEntityData);
|
2018-09-27 14:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Entity get(String entityId) {
|
|
|
|
return _entities[entityId];
|
|
|
|
}
|
|
|
|
|
2018-10-07 02:17:14 +03:00
|
|
|
List<Entity> getAll(List ids) {
|
|
|
|
List<Entity> result = [];
|
2018-10-07 10:36:50 +03:00
|
|
|
ids.forEach((id){
|
|
|
|
Entity en = get(id);
|
|
|
|
if (en != null) {
|
|
|
|
result.add(en);
|
2018-10-07 02:17:14 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:51:57 +03:00
|
|
|
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) {
|
2018-10-07 02:17:14 +03:00
|
|
|
if (_entities[userGroupId].childEntityIds.contains(entiyId)) {
|
2018-09-28 13:33:15 +03:00
|
|
|
foundInGroup = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!foundInGroup) {
|
|
|
|
result["notGroupedEntities"].add(entiyId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:16:50 +03:00
|
|
|
}
|