From 3504d3276c796e7b71f3be9b6dc3b2bc71a0923d Mon Sep 17 00:00:00 2001 From: estevez-dev Date: Wed, 13 Mar 2019 16:39:23 +0200 Subject: [PATCH] Resolves #11 Add Panels fetching --- lib/home_assistant.class.dart | 25 +++++++ lib/main.dart | 35 +++++++--- lib/panel.page.dart | 40 +++++++++++ lib/ui_class/panel_class.dart | 38 +++++++++++ .../config_panel_widget.dart} | 66 ++++++++----------- 5 files changed, 156 insertions(+), 48 deletions(-) create mode 100644 lib/panel.page.dart create mode 100644 lib/ui_class/panel_class.dart rename lib/{configuration.page.dart => ui_widgets/config_panel_widget.dart} (57%) diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index 389a541..84ddc81 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -19,6 +19,8 @@ class HomeAssistant { Map _rawLovelaceData; + List panels = []; + Completer _fetchCompleter; Completer _connectionCompleter; Timer _connectionTimer; @@ -159,6 +161,7 @@ class HomeAssistant { futures.add(_getConfig()); futures.add(_getServices()); futures.add(_getUserInfo()); + futures.add(_getPanels()); try { await Future.wait(futures); _createUI(); @@ -254,6 +257,28 @@ class HomeAssistant { await _sendInitialMessage("get_services").then((data) => Logger.d("We actually don`t need the list of servcies for now")); } + Future _getPanels() async { + panels.clear(); + await _sendInitialMessage("get_panels").then((data) { + if (data["success"]) { + data["result"].forEach((k,v) { + if (k != "lovelace" && !v["component_name"].startsWith("dev-") && v["component_name"]!="profile" && v["component_name"]!="states" && v["component_name"]!="kiosk") { + String title = v['title'] == null ? "${k[0].toUpperCase()}${k.substring(1)}" : "${v['title'][0].toUpperCase()}${v['title'].substring(1)}"; + panels.add(Panel( + id: k, + type: v["component_name"], + title: title, + urlPath: v["url_path"], + config: v["config"], + icon: v["icon"] + ) + ); + } + }); + } + }); + } + _incrementMessageId() { _currentMessageId += 1; } diff --git a/lib/main.dart b/lib/main.dart index 6ecd4bc..e023146 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -77,7 +77,7 @@ part 'entity_widgets/controls/media_player_widgets.dart'; part 'entity_widgets/controls/fan_controls.dart'; part 'entity_widgets/controls/alarm_control_panel_controls.dart'; part 'settings.page.dart'; -part 'configuration.page.dart'; +part 'panel.page.dart'; part 'home_assistant.class.dart'; part 'log.page.dart'; part 'entity.page.dart'; @@ -88,9 +88,11 @@ part 'ui_class/ui.dart'; part 'ui_class/view.class.dart'; part 'ui_class/card.class.dart'; part 'ui_class/sizes_class.dart'; +part 'ui_class/panel_class.dart'; part 'ui_widgets/view.dart'; part 'ui_widgets/card_widget.dart'; part 'ui_widgets/card_header_widget.dart'; +part 'ui_widgets/config_panel_widget.dart'; EventBus eventBus = new EventBus(); @@ -131,7 +133,7 @@ class HAClientApp extends StatelessWidget { routes: { "/": (context) => MainPage(title: 'HA Client'), "/connection-settings": (context) => ConnectionSettingsPage(title: "Settings"), - "/configuration": (context) => ConfigurationPage(title: "Configuration"), + "/configuration": (context) => PanelPage(title: "Configuration"), "/log-view": (context) => LogViewPage(title: "Log") }, ); @@ -302,6 +304,15 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker ); } + void _showPanelPage(Panel panel) { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => PanelPage(title: "${panel.title}", panel: panel), + ) + ); + } + List buildUIViewTabs() { List result = []; @@ -348,15 +359,19 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker Divider(), ]); } else { + if (_homeAssistant != null && _homeAssistant.panels.isNotEmpty) { + _homeAssistant.panels.forEach((Panel panel) { + menuItems.add( + new ListTile( + leading: Icon(MaterialDesignIcons.getIconDataFromIconName(panel.icon)), + title: Text("${panel.title}(${panel.urlPath})"), + onTap: () =>_showPanelPage(panel) + ) + ); + }); + menuItems.add(Divider()); + } menuItems.addAll([ - new ListTile( - leading: Icon(Icons.settings), - title: Text("Configuration"), - onTap: () { - Navigator.of(context).pop(); - Navigator.of(context).pushNamed('/configuration'); - }, - ), new ListTile( leading: Icon(Icons.insert_drive_file), title: Text("Log"), diff --git a/lib/panel.page.dart b/lib/panel.page.dart new file mode 100644 index 0000000..87388d1 --- /dev/null +++ b/lib/panel.page.dart @@ -0,0 +1,40 @@ +part of 'main.dart'; + +class PanelPage extends StatefulWidget { + PanelPage({Key key, this.title, this.panel}) : super(key: key); + + final String title; + final Panel panel; + + @override + _PanelPageState createState() => new _PanelPageState(); +} + +class _PanelPageState extends State { + + List _items; + + @override + void initState() { + super.initState(); + } + + @override + Widget build(BuildContext context) { + + return new Scaffold( + appBar: new AppBar( + leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){ + Navigator.pop(context); + }), + title: new Text(widget.title), + ), + body: widget.panel.getWidget(), + ); + } + + @override + void dispose() { + super.dispose(); + } +} diff --git a/lib/ui_class/panel_class.dart b/lib/ui_class/panel_class.dart new file mode 100644 index 0000000..9361842 --- /dev/null +++ b/lib/ui_class/panel_class.dart @@ -0,0 +1,38 @@ +part of '../main.dart'; + +class Panel { + + static const iconsByComponent = { + "config": "mdi:settings", + "history": "mdi:poll-box", + "map": "mdi:tooltip-account", + "logbook": "mdi:format-list-bulleted-type", + "custom": "mdi:home-assistant" + }; + + final String id; + final String type; + final String title; + final String urlPath; + final Map config; + String icon; + + Panel({this.id, this.type, this.title, this.urlPath, this.icon, this.config}) { + if (icon == null || !icon.startsWith("mdi:")) { + icon = Panel.iconsByComponent[type]; + } + } + + Widget getWidget() { + switch (type) { + case "config": { + return ConfigPanelWidget(); + } + + default: { + return Text("Unsupported panel component: $type"); + } + } + } + +} \ No newline at end of file diff --git a/lib/configuration.page.dart b/lib/ui_widgets/config_panel_widget.dart similarity index 57% rename from lib/configuration.page.dart rename to lib/ui_widgets/config_panel_widget.dart index 25c19cf..407d60c 100644 --- a/lib/configuration.page.dart +++ b/lib/ui_widgets/config_panel_widget.dart @@ -1,12 +1,10 @@ -part of 'main.dart'; +part of '../main.dart'; -class ConfigurationPage extends StatefulWidget { - ConfigurationPage({Key key, this.title}) : super(key: key); - - final String title; +class ConfigPanelWidget extends StatefulWidget { + ConfigPanelWidget({Key key}) : super(key: key); @override - _ConfigurationPageState createState() => new _ConfigurationPageState(); + _ConfigPanelWidgetState createState() => new _ConfigPanelWidgetState(); } class ConfigurationItem { @@ -17,7 +15,7 @@ class ConfigurationItem { final Widget body; } -class _ConfigurationPageState extends State { +class _ConfigPanelWidgetState extends State { List _items; @@ -64,37 +62,29 @@ class _ConfigurationPageState extends State { @override Widget build(BuildContext context) { - return new Scaffold( - appBar: new AppBar( - leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){ - Navigator.pop(context); - }), - title: new Text(widget.title), - ), - body: ListView( - children: [ - new ExpansionPanelList( - expansionCallback: (int index, bool isExpanded) { - setState(() { - _items[index].isExpanded = !_items[index].isExpanded; - }); - }, - children: _items.map((ConfigurationItem item) { - return new ExpansionPanel( - headerBuilder: (BuildContext context, bool isExpanded) { - return CardHeaderWidget( - name: item.header, - ); - }, - isExpanded: item.isExpanded, - body: new Container( - child: item.body, - ), - ); - }).toList(), - ), - ], - ), + return ListView( + children: [ + new ExpansionPanelList( + expansionCallback: (int index, bool isExpanded) { + setState(() { + _items[index].isExpanded = !_items[index].isExpanded; + }); + }, + children: _items.map((ConfigurationItem item) { + return new ExpansionPanel( + headerBuilder: (BuildContext context, bool isExpanded) { + return CardHeaderWidget( + name: item.header, + ); + }, + isExpanded: item.isExpanded, + body: new Container( + child: item.body, + ), + ); + }).toList(), + ), + ], ); }