2018-09-26 22:16:50 +03:00
|
|
|
part of 'main.dart';
|
|
|
|
|
|
|
|
class EntityCollection {
|
|
|
|
|
2018-10-21 14:43:52 +03:00
|
|
|
Map<String, Entity> _allEntities;
|
2018-10-25 00:54:20 +03:00
|
|
|
//Map<String, Entity> views;
|
2018-09-26 22:16:50 +03:00
|
|
|
|
2018-10-21 14:43:52 +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() {
|
2018-10-21 14:43:52 +03:00
|
|
|
_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");
|
2018-09-28 13:33:15 +03:00
|
|
|
|
2018-09-27 14:51:57 +03:00
|
|
|
void parse(List rawData) {
|
2018-10-21 14:43:52 +03:00
|
|
|
_allEntities.clear();
|
2018-10-25 00:54:20 +03:00
|
|
|
//views.clear();
|
2018-09-27 14:51:57 +03:00
|
|
|
|
2018-12-15 14:09:37 +02:00
|
|
|
Logger.d("Parsing ${rawData.length} Home Assistant entities");
|
2018-09-27 14:51:57 +03:00
|
|
|
rawData.forEach((rawEntityData) {
|
2018-10-21 14:43:52 +03:00
|
|
|
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) {
|
2018-10-21 14:43:52 +03:00
|
|
|
views[entityId] = entity;
|
2018-10-25 00:54:20 +03:00
|
|
|
}*/
|
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);
|
|
|
|
}
|
2018-11-11 18:36:49 +02:00
|
|
|
case "media_player": {
|
|
|
|
return MediaPlayerEntity(rawEntityData);
|
|
|
|
}
|
2018-10-28 20:01:01 +02:00
|
|
|
case 'sensor': {
|
|
|
|
return SensorEntity(rawEntityData);
|
|
|
|
}
|
2018-11-23 19:18:17 +02:00
|
|
|
case 'lock': {
|
|
|
|
return LockEntity(rawEntityData);
|
|
|
|
}
|
2019-01-25 23:48:31 +02:00
|
|
|
case "automation": {
|
|
|
|
return AutomationEntity(rawEntityData);
|
|
|
|
}
|
|
|
|
|
2018-10-15 00:15:09 +03:00
|
|
|
case "input_boolean":
|
2018-10-17 02:19:46 +03:00
|
|
|
case "switch": {
|
|
|
|
return SwitchEntity(rawEntityData);
|
|
|
|
}
|
2018-10-15 00:15:09 +03:00
|
|
|
case "light": {
|
2018-10-17 02:19:46 +03:00
|
|
|
return LightEntity(rawEntityData);
|
2018-10-15 00:15:09 +03:00
|
|
|
}
|
2018-11-24 11:33:59 +02:00
|
|
|
case "group": {
|
|
|
|
return GroupEntity(rawEntityData);
|
|
|
|
}
|
2018-10-15 00:15:09 +03:00
|
|
|
case "script":
|
|
|
|
case "scene": {
|
2018-10-17 02:19:46 +03:00
|
|
|
return ButtonEntity(rawEntityData);
|
2018-10-15 00:15:09 +03:00
|
|
|
}
|
|
|
|
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-11-24 17:00:45 +02:00
|
|
|
case "fan": {
|
|
|
|
return FanEntity(rawEntityData);
|
|
|
|
}
|
2019-02-08 23:59:33 +02:00
|
|
|
case "camera": {
|
2019-01-29 21:59:05 +02:00
|
|
|
return CameraEntity(rawEntityData);
|
2019-02-08 23:59:33 +02:00
|
|
|
}
|
2019-01-28 16:48:49 +02:00
|
|
|
case "alarm_control_panel": {
|
|
|
|
return AlarmControlPanelEntity(rawEntityData);
|
|
|
|
}
|
2018-10-15 00:15:09 +03:00
|
|
|
default: {
|
|
|
|
return Entity(rawEntityData);
|
|
|
|
}
|
|
|
|
}
|
2018-09-29 13:49:25 +03:00
|
|
|
}
|
|
|
|
|
2018-12-15 14:09:37 +02:00
|
|
|
bool updateState(Map rawStateData) {
|
2018-09-27 14:51:57 +03:00
|
|
|
if (isExist(rawStateData["entity_id"])) {
|
2018-09-29 12:02:41 +03:00
|
|
|
updateFromRaw(rawStateData["new_state"] ?? rawStateData["old_state"]);
|
2018-12-15 14:09:37 +02:00
|
|
|
return false;
|
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-12-15 14:09:37 +02:00
|
|
|
return true;
|
2018-09-26 22:16:50 +03:00
|
|
|
}
|
2018-09-27 14:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void add(Entity entity) {
|
2018-10-21 14:43:52 +03:00
|
|
|
_allEntities[entity.entityId] = entity;
|
2018-09-27 14:51:57 +03:00
|
|
|
}
|
|
|
|
|
2018-12-15 14:09:37 +02:00
|
|
|
void addFromRaw(Map rawEntityData) {
|
2018-09-29 13:49:25 +03:00
|
|
|
Entity entity = _createEntityInstance(rawEntityData);
|
2018-10-21 14:43:52 +03:00
|
|
|
_allEntities[entity.entityId] = entity;
|
2018-09-27 14:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-10-21 14:43:52 +03:00
|
|
|
return _allEntities[entityId];
|
2018-09-27 14:51:57 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2018-10-21 14:43:52 +03:00
|
|
|
return _allEntities[entityId] != null;
|
2018-09-27 14:51:57 +03:00
|
|
|
}
|
|
|
|
|
2018-10-25 00:05:29 +03:00
|
|
|
List<Entity> filterEntitiesForDefaultView() {
|
|
|
|
List<Entity> result = [];
|
|
|
|
List<Entity> groups = [];
|
|
|
|
List<Entity> nonGroupEntities = [];
|
2018-10-21 14:43:52 +03:00
|
|
|
_allEntities.forEach((id, entity){
|
2018-11-23 19:30:16 +02:00
|
|
|
if (entity.isGroup && (entity.attributes['auto'] == null || (entity.attributes['auto'] && !entity.isHidden)) && (!entity.isView)) {
|
2018-10-25 00:05:29 +03:00
|
|
|
groups.add(entity);
|
2018-09-28 13:33:15 +03:00
|
|
|
}
|
|
|
|
if (!entity.isGroup) {
|
2018-10-25 00:05:29 +03:00
|
|
|
nonGroupEntities.add(entity);
|
2018-09-28 13:33:15 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-25 00:05:29 +03:00
|
|
|
nonGroupEntities.forEach((entity) {
|
2018-09-28 13:33:15 +03:00
|
|
|
bool foundInGroup = false;
|
2018-10-25 00:05:29 +03:00
|
|
|
groups.forEach((groupEntity) {
|
|
|
|
if (groupEntity.childEntityIds.contains(entity.entityId)) {
|
2018-09-28 13:33:15 +03:00
|
|
|
foundInGroup = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!foundInGroup) {
|
2018-10-25 00:05:29 +03:00
|
|
|
result.add(entity);
|
2018-09-28 13:33:15 +03:00
|
|
|
}
|
|
|
|
});
|
2018-10-25 00:05:29 +03:00
|
|
|
result.insertAll(0, groups);
|
2018-09-28 13:33:15 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2018-09-26 22:16:50 +03:00
|
|
|
}
|