From cc0278ee55714f316f95795a9db113ef2fc6e2f8 Mon Sep 17 00:00:00 2001 From: estevez Date: Sun, 23 Sep 2018 02:04:44 +0300 Subject: [PATCH] [#32] Use entity picture instead of icon if exist --- lib/data_model.dart | 36 +++++++++++++++++++++++++++++------- lib/main.dart | 36 ++++++++++++++++++++---------------- lib/settings.dart | 1 + pubspec.lock | 35 +++++++++++++++++++++++++++++++++++ pubspec.yaml | 1 + 5 files changed, 86 insertions(+), 23 deletions(-) diff --git a/lib/data_model.dart b/lib/data_model.dart index 0a59b55..6108dc1 100644 --- a/lib/data_model.dart +++ b/lib/data_model.dart @@ -3211,15 +3211,37 @@ class MaterialDesignIcons { "mdi:blank": 0xf68c }; - static IconData createIconDataFromEntityData(Map data) { - String iconName = data["attributes"] != null ? data["attributes"]["icon"] : null; - int iconCode = 0; - if (iconName != null) { - iconCode = getIconCodeByIconName(iconName); + static Widget createIconFromEntityData(Map data, double size, Color color) { + if ((data["attributes"] != null) && (data["attributes"]["entity_picture"] != null)) { + if (homeAssistantWebHost != null) { + return CircleAvatar( + backgroundColor: Colors.white, + backgroundImage: CachedNetworkImageProvider( + "$homeAssistantWebHost${data["attributes"]["entity_picture"]}", + ), + ); + } else { + return Container(width: 0.0, height: 0.0); + } } else { - iconCode = getDefaultIconByEntityId(data["entity_id"], data["attributes"] != null ? data["attributes"]["device_class"] : null, data["state"]); // + String iconName = data["attributes"] != null + ? data["attributes"]["icon"] + : null; + int iconCode = 0; + if (iconName != null) { + iconCode = getIconCodeByIconName(iconName); + } else { + iconCode = getDefaultIconByEntityId(data["entity_id"], + data["attributes"] != null + ? data["attributes"]["device_class"] + : null, data["state"]); // + } + return Icon( + IconData(iconCode, fontFamily: 'Material Design Icons'), + size: size, + color: color, + ); } - return IconData(iconCode, fontFamily: 'Material Design Icons'); } static IconData createIconDataFromIconCode(int code) { diff --git a/lib/main.dart b/lib/main.dart index dbad708..2fd13c9 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -7,6 +7,7 @@ import 'package:web_socket_channel/io.dart'; import 'package:progress_indicators/progress_indicators.dart'; import 'package:event_bus/event_bus.dart'; import 'package:flutter/widgets.dart'; +import 'package:cached_network_image/cached_network_image.dart'; part 'settings.dart'; part 'data_model.dart'; @@ -15,6 +16,8 @@ EventBus eventBus = new EventBus(); const String appName = "HA Client"; const appVersion = "0.0.11-alpha"; +String homeAssistantWebHost; + void main() => runApp(new HassClientApp()); class HassClientApp extends StatelessWidget { @@ -92,6 +95,7 @@ class _MainPageState extends State with WidgetsBindingObserver { String port = prefs.getString('hassio-port'); _instanceHost = "$domain:$port"; String apiEndpoint = "${prefs.getString('hassio-protocol')}://$domain:$port/api/websocket"; + homeAssistantWebHost = "${prefs.getString('hassio-res-protocol')}://$domain:$port"; String apiPassword = prefs.getString('hassio-password'); String authType = prefs.getString('hassio-auth-type'); if ((domain == null) || (port == null) || (apiPassword == null) || @@ -226,6 +230,8 @@ class _MainPageState extends State with WidgetsBindingObserver { badgeIcon = Center( child: Text( "${data['state']}", + overflow: TextOverflow.fade, + softWrap: false, textAlign: TextAlign.center, style: TextStyle(fontSize: 18.0), ), @@ -233,7 +239,7 @@ class _MainPageState extends State with WidgetsBindingObserver { break; } default: { - badgeIcon = Icon(MaterialDesignIcons.createIconDataFromEntityData(data)); + badgeIcon = MaterialDesignIcons.createIconFromEntityData(data, 50.0,Colors.black); } } return Column( @@ -303,15 +309,13 @@ class _MainPageState extends State with WidgetsBindingObserver { debugPrint("Hiding unknown entity from card: $id"); } else { entities.add(new ListTile( - leading: Icon( - MaterialDesignIcons.createIconDataFromEntityData(data), - color: _stateIconColors[data["state"]] ?? Colors.blueGrey, - ), + leading: MaterialDesignIcons.createIconFromEntityData(data, 24.0, _stateIconColors[data["state"]] ?? Colors.blueGrey), //subtitle: Text("${data['entity_id']}"), - trailing: _buildEntityAction(id), + trailing: _buildEntityStateWidget(data), title: Text( "${data["display_name"]}", - overflow: TextOverflow.ellipsis, + overflow: TextOverflow.fade, + softWrap: false, ), )); } @@ -319,26 +323,26 @@ class _MainPageState extends State with WidgetsBindingObserver { return entities; } - Widget _buildEntityAction(String entityId) { - var entity = _entitiesData[entityId]; + Widget _buildEntityStateWidget(data) { + String entityId = data["entity_id"]; Widget result; - if (entity["actionType"] == "switch") { + if (data["actionType"] == "switch") { result = Switch( - value: (entity["state"] == "on"), + value: (data["state"] == "on"), onChanged: ((state) { _callService( - entity["domain"], state ? "turn_on" : "turn_off", entityId); + data["domain"], state ? "turn_on" : "turn_off", entityId); setState(() { _entitiesData[entityId]["state"] = state ? "on" : "off"; }); }), ); - } else if (entity["actionType"] == "statelessIcon") { + } else if (data["actionType"] == "statelessIcon") { result = SizedBox( width: 60.0, child: FlatButton( onPressed: (() { - _callService(entity["domain"], "turn_on", entityId); + _callService(data["domain"], "turn_on", entityId); }), child: Text( "Run", @@ -350,7 +354,7 @@ class _MainPageState extends State with WidgetsBindingObserver { result = Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 16.0, 0.0), child: Text( - "${entity["state"]}${(entity["attributes"] != null && entity["attributes"]["unit_of_measurement"] != null) ? entity["attributes"]["unit_of_measurement"] : ''}", + "${data["state"]}${(data["attributes"] != null && data["attributes"]["unit_of_measurement"] != null) ? data["attributes"]["unit_of_measurement"] : ''}", textAlign: TextAlign.right, style: new TextStyle( fontSize: 16.0, @@ -370,7 +374,7 @@ class _MainPageState extends State with WidgetsBindingObserver { _uiStructure.forEach((viewId, structure) { result.add( Tab( - icon: Icon(MaterialDesignIcons.createIconDataFromEntityData(structure)) + icon: MaterialDesignIcons.createIconFromEntityData(structure, 24.0, null) ) ); }); diff --git a/lib/settings.dart b/lib/settings.dart index 7603773..db02d27 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -40,6 +40,7 @@ class _ConnectionSettingsPageState extends State { prefs.setString("hassio-port", _hassioPort); prefs.setString("hassio-password", _hassioPassword); prefs.setString("hassio-protocol", _socketProtocol); + prefs.setString("hassio-res-protocol", _socketProtocol == "wss" ? "https" : "http"); prefs.setString("hassio-auth-type", _authType); } diff --git a/pubspec.lock b/pubspec.lock index 2d42dd7..94bf4b2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -36,6 +36,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.4" + cached_network_image: + dependency: "direct main" + description: + name: cached_network_image + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.2" charcode: dependency: transitive description: @@ -92,6 +99,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_cache_manager: + dependency: transitive + description: + name: flutter_cache_manager + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.2" flutter_launcher_icons: dependency: "direct main" description: @@ -251,6 +265,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.6.2" + path_provider: + dependency: transitive + description: + name: path_provider + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.1" petitparser: dependency: transitive description: @@ -375,6 +396,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.4" + synchronized: + dependency: transitive + description: + name: synchronized + url: "https://pub.dartlang.org" + source: hosted + version: "1.5.3" term_glyph: dependency: transitive description: @@ -403,6 +431,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.9.0+5" + uuid: + dependency: transitive + description: + name: uuid + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" vector_math: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 6ed4b55..33e272c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,6 +14,7 @@ dependencies: event_bus: ^1.0.1 package_info: ^0.3.2 flutter_launcher_icons: ^0.6.1 + cached_network_image: ^0.4.1 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.