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

191 lines
5.1 KiB
Dart
Raw Normal View History

2018-09-26 22:16:50 +03:00
part of 'main.dart';
class EntityCollection {
2019-03-19 23:07:40 +02:00
final homeAssistantWebHost;
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
2019-03-19 23:07:40 +02:00
EntityCollection(this.homeAssistantWebHost) {
_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();
Logger.d("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
}*/
});
}
2019-03-20 23:38:57 +02:00
void clear() {
_allEntities.clear();
}
Entity _createEntityInstance(rawEntityData) {
switch (rawEntityData["entity_id"].split(".")[0]) {
case 'sun': {
2019-03-19 23:07:40 +02:00
return SunEntity(rawEntityData, homeAssistantWebHost);
}
2018-11-11 18:36:49 +02:00
case "media_player": {
2019-03-19 23:07:40 +02:00
return MediaPlayerEntity(rawEntityData, homeAssistantWebHost);
2018-11-11 18:36:49 +02:00
}
2018-10-28 20:01:01 +02:00
case 'sensor': {
2019-03-19 23:07:40 +02:00
return SensorEntity(rawEntityData, homeAssistantWebHost);
2018-10-28 20:01:01 +02:00
}
2018-11-23 19:18:17 +02:00
case 'lock': {
2019-03-19 23:07:40 +02:00
return LockEntity(rawEntityData, homeAssistantWebHost);
2018-11-23 19:18:17 +02:00
}
2019-01-25 23:48:31 +02:00
case "automation": {
2019-03-19 23:07:40 +02:00
return AutomationEntity(rawEntityData, homeAssistantWebHost);
2019-01-25 23:48:31 +02:00
}
case "input_boolean":
2018-10-17 02:19:46 +03:00
case "switch": {
2019-03-19 23:07:40 +02:00
return SwitchEntity(rawEntityData, homeAssistantWebHost);
2018-10-17 02:19:46 +03:00
}
case "light": {
2019-03-19 23:07:40 +02:00
return LightEntity(rawEntityData, homeAssistantWebHost);
}
case "group": {
2019-03-19 23:07:40 +02:00
return GroupEntity(rawEntityData, homeAssistantWebHost);
}
case "script":
case "scene": {
2019-03-19 23:07:40 +02:00
return ButtonEntity(rawEntityData, homeAssistantWebHost);
}
case "input_datetime": {
2019-03-19 23:07:40 +02:00
return DateTimeEntity(rawEntityData, homeAssistantWebHost);
}
case "input_select": {
2019-03-19 23:07:40 +02:00
return SelectEntity(rawEntityData, homeAssistantWebHost);
}
case "input_number": {
2019-03-19 23:07:40 +02:00
return SliderEntity(rawEntityData, homeAssistantWebHost);
}
case "input_text": {
2019-03-19 23:07:40 +02:00
return TextEntity(rawEntityData, homeAssistantWebHost);
}
case "climate": {
2019-03-19 23:07:40 +02:00
return ClimateEntity(rawEntityData, homeAssistantWebHost);
}
2018-10-16 17:35:13 +03:00
case "cover": {
2019-03-19 23:07:40 +02:00
return CoverEntity(rawEntityData, homeAssistantWebHost);
2018-10-16 17:35:13 +03:00
}
2018-11-24 17:00:45 +02:00
case "fan": {
2019-03-19 23:07:40 +02:00
return FanEntity(rawEntityData, homeAssistantWebHost);
2018-11-24 17:00:45 +02:00
}
case "camera": {
2019-03-19 23:07:40 +02:00
return CameraEntity(rawEntityData, homeAssistantWebHost);
}
2019-01-28 16:48:49 +02:00
case "alarm_control_panel": {
2019-03-19 23:07:40 +02:00
return AlarmControlPanelEntity(rawEntityData, homeAssistantWebHost);
2019-01-28 16:48:49 +02:00
}
2019-03-13 21:33:58 +02:00
case "timer": {
2019-03-19 23:07:40 +02:00
return TimerEntity(rawEntityData, homeAssistantWebHost);
2019-03-13 21:33:58 +02:00
}
default: {
2019-03-19 23:07:40 +02:00
return Entity(rawEntityData, homeAssistantWebHost);
}
}
}
bool updateState(Map rawStateData) {
if (isExist(rawStateData["entity_id"])) {
updateFromRaw(rawStateData["new_state"] ?? rawStateData["old_state"]);
return false;
} else {
addFromRaw(rawStateData["new_state"] ?? rawStateData["old_state"]);
return true;
2018-09-26 22:16:50 +03:00
}
}
void add(Entity entity) {
_allEntities[entity.entityId] = entity;
}
void addFromRaw(Map rawEntityData) {
Entity entity = _createEntityInstance(rawEntityData);
_allEntities[entity.entityId] = entity;
}
void updateFromRaw(Map rawEntityData) {
2019-03-19 23:07:40 +02:00
get("${rawEntityData["entity_id"]}")?.update(rawEntityData, homeAssistantWebHost);
}
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;
}
List<Entity> getByDomains(List<String> domains) {
List<Entity> result = [];
_allEntities.forEach((id, entity) {
if (domains.contains(entity.domain)) {
Logger.d("getByDomain: ${entity.isHidden}");
result.add(entity);
}
});
return result;
}
2018-10-25 00:05:29 +03:00
List<Entity> filterEntitiesForDefaultView() {
List<Entity> result = [];
List<Entity> groups = [];
List<Entity> nonGroupEntities = [];
_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);
}
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
}