This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/entity_collection.class.dart

146 lines
3.6 KiB
Dart
Raw Normal View History

2018-09-26 22:16:50 +03:00
part of 'main.dart';
class EntityCollection {
Map<String, Entity> _allEntities;
2018-10-25 00:54:20 +03:00
//Map<String, Entity> views;
2018-09-26 22:16:50 +03:00
bool get isEmpty => _allEntities.isEmpty;
2018-10-25 00:05:29 +03:00
List<Entity> get viewEntities => _allEntities.values.where((entity) => entity.isView).toList();
2018-10-07 02:17:14 +03:00
2018-09-26 22:16:50 +03:00
EntityCollection() {
_allEntities = {};
2018-10-25 00:54:20 +03:00
//views = {};
2018-09-26 22:16:50 +03:00
}
2018-10-25 00:05:29 +03:00
bool get hasDefaultView => _allEntities.keys.contains("group.default_view");
void parse(List rawData) {
_allEntities.clear();
2018-10-25 00:54:20 +03:00
//views.clear();
2018-10-27 01:24:23 +03:00
TheLogger.debug("Parsing ${rawData.length} Home Assistant entities");
rawData.forEach((rawEntityData) {
addFromRaw(rawEntityData);
});
_allEntities.forEach((entityId, entity){
if ((entity.isGroup) && (entity.childEntityIds != null)) {
entity.childEntities = getAll(entity.childEntityIds);
}
2018-10-25 00:54:20 +03:00
/*if (entity.isView) {
views[entityId] = entity;
2018-10-25 00:54:20 +03:00
}*/
});
}
Entity _createEntityInstance(rawEntityData) {
switch (rawEntityData["entity_id"].split(".")[0]) {
case 'sun': {
return SunEntity(rawEntityData);
}
case "automation":
case "input_boolean":
2018-10-17 02:19:46 +03:00
case "switch": {
return SwitchEntity(rawEntityData);
}
case "light": {
2018-10-17 02:19:46 +03:00
return LightEntity(rawEntityData);
}
case "script":
case "scene": {
2018-10-17 02:19:46 +03:00
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);
}
default: {
return Entity(rawEntityData);
}
}
}
void updateState(Map rawStateData) {
if (isExist(rawStateData["entity_id"])) {
updateFromRaw(rawStateData["new_state"] ?? rawStateData["old_state"]);
} else {
addFromRaw(rawStateData["new_state"] ?? rawStateData["old_state"]);
2018-09-26 22:16:50 +03:00
}
}
void add(Entity entity) {
_allEntities[entity.entityId] = entity;
}
Entity addFromRaw(Map rawEntityData) {
Entity entity = _createEntityInstance(rawEntityData);
_allEntities[entity.entityId] = entity;
return entity;
}
void updateFromRaw(Map rawEntityData) {
2018-10-07 10:41:41 +03:00
get("${rawEntityData["entity_id"]}")?.update(rawEntityData);
}
Entity get(String entityId) {
return _allEntities[entityId];
}
2018-10-07 02:17:14 +03:00
List<Entity> getAll(List ids) {
List<Entity> result = [];
ids.forEach((id){
Entity en = get(id);
if (en != null) {
result.add(en);
2018-10-07 02:17:14 +03:00
}
});
return result;
}
bool isExist(String entityId) {
return _allEntities[entityId] != null;
}
2018-10-25 00:05:29 +03:00
List<Entity> filterEntitiesForDefaultView() {
List<Entity> result = [];
List<Entity> groups = [];
List<Entity> nonGroupEntities = [];
_allEntities.forEach((id, entity){
if ((id.indexOf("group.") == 0) && (id.indexOf(".all_") == -1) && (!entity.isView)) {
2018-10-25 00:05:29 +03:00
groups.add(entity);
}
if (!entity.isGroup) {
2018-10-25 00:05:29 +03:00
nonGroupEntities.add(entity);
}
});
2018-10-25 00:05:29 +03:00
nonGroupEntities.forEach((entity) {
bool foundInGroup = false;
2018-10-25 00:05:29 +03:00
groups.forEach((groupEntity) {
if (groupEntity.childEntityIds.contains(entity.entityId)) {
foundInGroup = true;
}
});
if (!foundInGroup) {
2018-10-25 00:05:29 +03:00
result.add(entity);
}
});
2018-10-25 00:05:29 +03:00
result.insertAll(0, groups);
return result;
}
2018-09-26 22:16:50 +03:00
}