2019-09-07 18:23:04 +03:00
|
|
|
part of 'main.dart';
|
2018-10-27 14:27:41 +03:00
|
|
|
|
|
|
|
class HomeAssistantUI {
|
|
|
|
List<HAView> views;
|
2018-11-04 21:02:12 +02:00
|
|
|
String title;
|
2018-10-27 14:27:41 +03:00
|
|
|
|
2019-03-21 14:08:07 +02:00
|
|
|
bool get isEmpty => views == null || views.isEmpty;
|
|
|
|
|
2020-04-03 00:16:34 +03:00
|
|
|
HomeAssistantUI({rawLovelaceConfig}) {
|
|
|
|
if (rawLovelaceConfig == null) {
|
|
|
|
rawLovelaceConfig = _generateLovelaceConfig();
|
|
|
|
}
|
2018-10-27 14:27:41 +03:00
|
|
|
views = [];
|
2020-03-22 01:11:00 +02:00
|
|
|
Logger.d("--Title: ${rawLovelaceConfig["title"]}");
|
|
|
|
title = rawLovelaceConfig["title"];
|
|
|
|
int viewCounter = 0;
|
|
|
|
Logger.d("--Views count: ${rawLovelaceConfig['views'].length}");
|
|
|
|
rawLovelaceConfig["views"].forEach((rawView){
|
2020-05-14 19:44:50 +03:00
|
|
|
Logger.d("----view: ${rawView['path'] ?? viewCounter}");
|
2020-03-22 01:11:00 +02:00
|
|
|
HAView view = HAView(
|
|
|
|
count: viewCounter,
|
|
|
|
rawData: rawView
|
|
|
|
);
|
|
|
|
|
|
|
|
views.add(
|
|
|
|
view
|
|
|
|
);
|
|
|
|
viewCounter += 1;
|
|
|
|
});
|
2018-10-27 14:27:41 +03:00
|
|
|
}
|
|
|
|
|
2020-04-03 00:16:34 +03:00
|
|
|
Map _generateLovelaceConfig() {
|
2020-05-20 01:01:07 +03:00
|
|
|
Map result = {
|
|
|
|
'title': 'Home'
|
|
|
|
};
|
|
|
|
List<Entity> left = HomeAssistant().entities.getByDomains(
|
|
|
|
excludeDomains: ['sensor','binary_sensor', 'device_tracker', 'person', 'sun']
|
|
|
|
);
|
|
|
|
List<Map> cards = [];
|
|
|
|
Map<String, Map> cardsByDomains = {};
|
|
|
|
left.forEach((Entity entity) {
|
|
|
|
if (entity is GroupEntity) {
|
|
|
|
cards.add({
|
|
|
|
'type': CardType.ENTITIES,
|
|
|
|
'title': entity.displayName,
|
|
|
|
'entities': entity.childEntities.map((e) => e.entityId)
|
|
|
|
});
|
|
|
|
} else if (entity is MediaPlayerEntity) {
|
|
|
|
cards.add({
|
|
|
|
'type': CardType.MEDIA_CONTROL,
|
|
|
|
'entity': entity.entityId
|
|
|
|
});
|
|
|
|
} else if (entity is AlarmControlPanelEntity) {
|
|
|
|
cards.add({
|
|
|
|
'type': CardType.ALARM_PANEL,
|
|
|
|
'entity': entity.entityId
|
|
|
|
});
|
|
|
|
} else if (cardsByDomains.containsKey(entity.domain)) {
|
|
|
|
cardsByDomains[entity.domain]['entities'].add(entity.entityId);
|
|
|
|
} else {
|
|
|
|
cardsByDomains[entity.domain] = {
|
|
|
|
'type': 'entities',
|
|
|
|
'entities': [entity.entityId],
|
|
|
|
'title': entity.domain
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
cards.addAll(cardsByDomains.values);
|
2020-04-03 00:16:34 +03:00
|
|
|
result['views'] = [
|
2020-04-03 01:04:56 +03:00
|
|
|
{
|
|
|
|
'icon': 'mdi:home',
|
|
|
|
'badges': HomeAssistant().entities.getByDomains(
|
|
|
|
includeDomains: ['sensor', 'binary_sensor', 'device_tracker', 'person', 'sun']
|
|
|
|
).map(
|
|
|
|
(en) => en.entityId
|
|
|
|
).toList(),
|
2020-05-20 01:01:07 +03:00
|
|
|
'cards': cards
|
2020-04-03 01:04:56 +03:00
|
|
|
}
|
2020-04-03 00:16:34 +03:00
|
|
|
];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-21 15:35:58 +02:00
|
|
|
Widget build(BuildContext context, TabController tabController) {
|
2018-10-27 14:27:41 +03:00
|
|
|
return TabBarView(
|
2019-02-21 15:35:58 +02:00
|
|
|
controller: tabController,
|
2018-11-16 14:30:43 +02:00
|
|
|
children: _buildViews(context)
|
2018-10-27 14:27:41 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Widget> _buildViews(BuildContext context) {
|
2020-04-21 12:01:21 +03:00
|
|
|
return views.map((view) => view.build(context)).toList();
|
2018-10-27 14:27:41 +03:00
|
|
|
}
|
|
|
|
|
2019-03-21 14:08:07 +02:00
|
|
|
void clear() {
|
|
|
|
views.clear();
|
|
|
|
}
|
|
|
|
|
2018-10-27 14:27:41 +03:00
|
|
|
}
|