From 293b5e02420f86fae65134565d517b00f6517df2 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Wed, 1 Apr 2020 17:04:32 +0000 Subject: [PATCH] zha_map WIP: get inital data --- lib/home_assistant.class.dart | 17 +++++++ lib/main.dart | 2 + lib/pages/play_media.page.dart | 2 +- lib/pages/zha_page.dart | 90 ++++++++++++++++++++++++++++++++++ lib/panels/panel_class.dart | 4 +- 5 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 lib/pages/zha_page.dart diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index 2561f22..62794ac 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -317,10 +317,27 @@ class HomeAssistant { } } + bool isServiceExist(String service) { + return services != null && + services.isNotEmpty && + services.containsKey(service); + } + void _createUI() { if (!autoUi && (_rawLovelaceData != null)) { Logger.d("Creating Lovelace UI"); ui = HomeAssistantUI(_rawLovelaceData); + if (isServiceExist('zha_map')) { + panels.add( + Panel( + id: 'haclient_zha', + componentName: 'haclient_zha', + title: 'ZHA', + urlPath: '/haclient_zha', + icon: 'mdi:zigbee' + ) + ); + } } else { Logger.e("No lovelace config!!!!"); } diff --git a/lib/main.dart b/lib/main.dart index 478d2e2..24befa8 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -112,6 +112,7 @@ part 'pages/widgets/page_loading_error.dart'; part 'pages/panel.page.dart'; part 'pages/main/main.page.dart'; part 'pages/integration_settings.page.dart'; +part 'pages/zha_page.dart'; part 'home_assistant.class.dart'; part 'pages/log.page.dart'; part 'pages/entity.page.dart'; @@ -250,6 +251,7 @@ class _HAClientAppState extends State { ), ), "/whats-new": (context) => WhatsNewPage(), + "/haclient_zha": (context) => ZhaPage(), "/auth": (context) => new standaloneWebview.WebviewScaffold( url: "${ConnectionManager().oauthUrl}", appBar: new AppBar( diff --git a/lib/pages/play_media.page.dart b/lib/pages/play_media.page.dart index 8eddd4a..e87d23e 100644 --- a/lib/pages/play_media.page.dart +++ b/lib/pages/play_media.page.dart @@ -57,7 +57,7 @@ class _PlayMediaPageState extends State { _loaded = false; }); } else { - _isMediaExtractorExist = HomeAssistant().services.containsKey("media_extractor"); + _isMediaExtractorExist = HomeAssistant().isServiceExist("media_extractor"); //_useMediaExtractor = _isMediaExtractorExist; _players = HomeAssistant().entities.getByDomains(domains: ["media_player"]); setState(() { diff --git a/lib/pages/zha_page.dart b/lib/pages/zha_page.dart new file mode 100644 index 0000000..78cdfcb --- /dev/null +++ b/lib/pages/zha_page.dart @@ -0,0 +1,90 @@ +part of '../main.dart'; + +class ZhaPage extends StatefulWidget { + ZhaPage({Key key}) : super(key: key); + + @override + _ZhaPageState createState() => new _ZhaPageState(); +} + +class _ZhaPageState extends State { + + List data = []; + String error = ""; + + @override + void initState() { + super.initState(); + _loadData(); + } + + _loadData() async { + setState(() { + data.clear(); + error = ""; + }); + ConnectionManager().sendSocketMessage( + type: 'zha_map/devices' + ).then((response){ + setState(() { + data = response['devices']; + }); + }).catchError((e){ + setState(() { + error = '$e'; + }); + }); + } + + @override + Widget build(BuildContext context) { + Widget body; + if (error.isNotEmpty) { + body = PageLoadingError(errorText: error,); + } else if (data.isEmpty) { + body = PageLoadingIndicator(); + } else { + List devicesListWindet = []; + data.forEach((device) { + devicesListWindet.add( + Card( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + CardHeader( + name: '${device['ieee']}', + subtitle: Text('${device['manufacturer']}'), + ), + Text('${device['device_type']}'), + Text('model: ${device['model']}'), + Text('offline: ${device['offline']}'), + Text('neighbours: ${device['neighbours'].length}'), + Text('raw: $device'), + ], + ), + ) + ); + }); + body = ListView( + children: devicesListWindet + ); + } + return new Scaffold( + appBar: new AppBar( + leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){ + Navigator.pop(context); + }), + actions: [ + IconButton( + icon: Icon(Icons.refresh), + onPressed: () => _loadData(), + ) + ], + // Here we take the value from the MyHomePage object that was created by + // the App.build method, and use it to set our appbar title. + title: new Text('ZHA'), + ), + body: body + ); + } +} \ No newline at end of file diff --git a/lib/panels/panel_class.dart b/lib/panels/panel_class.dart index ec01871..d2dbd9b 100644 --- a/lib/panels/panel_class.dart +++ b/lib/panels/panel_class.dart @@ -24,7 +24,7 @@ class Panel { icon = Panel.iconsByComponent[componentName]; } isHidden = (componentName == 'kiosk' || componentName == 'states' || componentName == 'profile' || componentName == 'developer-tools'); - isWebView = (componentName != 'config' && componentName != 'lovelace'); + isWebView = (componentName != 'config' && componentName != 'lovelace' && !componentName.startsWith('haclient')); } void handleOpen(BuildContext context) { @@ -34,6 +34,8 @@ class Panel { builder: (context) => PanelPage(title: "$title", panel: this), ) ); + } else if (componentName.startsWith('haclient')) { + Navigator.of(context).pushNamed(urlPath); } else if (componentName == 'lovelace') { HomeAssistant().lovelaceDashboardUrl = this.urlPath; HomeAssistant().autoUi = false;