[#32] Use entity picture instead of icon if exist

This commit is contained in:
estevez 2018-09-23 02:04:44 +03:00
parent 1133a996b9
commit cc0278ee55
5 changed files with 86 additions and 23 deletions

View File

@ -3211,15 +3211,37 @@ class MaterialDesignIcons {
"mdi:blank": 0xf68c "mdi:blank": 0xf68c
}; };
static IconData createIconDataFromEntityData(Map data) { static Widget createIconFromEntityData(Map data, double size, Color color) {
String iconName = data["attributes"] != null ? data["attributes"]["icon"] : null; if ((data["attributes"] != null) && (data["attributes"]["entity_picture"] != null)) {
int iconCode = 0; if (homeAssistantWebHost != null) {
if (iconName != null) { return CircleAvatar(
iconCode = getIconCodeByIconName(iconName); backgroundColor: Colors.white,
backgroundImage: CachedNetworkImageProvider(
"$homeAssistantWebHost${data["attributes"]["entity_picture"]}",
),
);
} else {
return Container(width: 0.0, height: 0.0);
}
} else { } 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) { static IconData createIconDataFromIconCode(int code) {

View File

@ -7,6 +7,7 @@ import 'package:web_socket_channel/io.dart';
import 'package:progress_indicators/progress_indicators.dart'; import 'package:progress_indicators/progress_indicators.dart';
import 'package:event_bus/event_bus.dart'; import 'package:event_bus/event_bus.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:cached_network_image/cached_network_image.dart';
part 'settings.dart'; part 'settings.dart';
part 'data_model.dart'; part 'data_model.dart';
@ -15,6 +16,8 @@ EventBus eventBus = new EventBus();
const String appName = "HA Client"; const String appName = "HA Client";
const appVersion = "0.0.11-alpha"; const appVersion = "0.0.11-alpha";
String homeAssistantWebHost;
void main() => runApp(new HassClientApp()); void main() => runApp(new HassClientApp());
class HassClientApp extends StatelessWidget { class HassClientApp extends StatelessWidget {
@ -92,6 +95,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
String port = prefs.getString('hassio-port'); String port = prefs.getString('hassio-port');
_instanceHost = "$domain:$port"; _instanceHost = "$domain:$port";
String apiEndpoint = "${prefs.getString('hassio-protocol')}://$domain:$port/api/websocket"; 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 apiPassword = prefs.getString('hassio-password');
String authType = prefs.getString('hassio-auth-type'); String authType = prefs.getString('hassio-auth-type');
if ((domain == null) || (port == null) || (apiPassword == null) || if ((domain == null) || (port == null) || (apiPassword == null) ||
@ -226,6 +230,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
badgeIcon = Center( badgeIcon = Center(
child: Text( child: Text(
"${data['state']}", "${data['state']}",
overflow: TextOverflow.fade,
softWrap: false,
textAlign: TextAlign.center, textAlign: TextAlign.center,
style: TextStyle(fontSize: 18.0), style: TextStyle(fontSize: 18.0),
), ),
@ -233,7 +239,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
break; break;
} }
default: { default: {
badgeIcon = Icon(MaterialDesignIcons.createIconDataFromEntityData(data)); badgeIcon = MaterialDesignIcons.createIconFromEntityData(data, 50.0,Colors.black);
} }
} }
return Column( return Column(
@ -303,15 +309,13 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
debugPrint("Hiding unknown entity from card: $id"); debugPrint("Hiding unknown entity from card: $id");
} else { } else {
entities.add(new ListTile( entities.add(new ListTile(
leading: Icon( leading: MaterialDesignIcons.createIconFromEntityData(data, 24.0, _stateIconColors[data["state"]] ?? Colors.blueGrey),
MaterialDesignIcons.createIconDataFromEntityData(data),
color: _stateIconColors[data["state"]] ?? Colors.blueGrey,
),
//subtitle: Text("${data['entity_id']}"), //subtitle: Text("${data['entity_id']}"),
trailing: _buildEntityAction(id), trailing: _buildEntityStateWidget(data),
title: Text( title: Text(
"${data["display_name"]}", "${data["display_name"]}",
overflow: TextOverflow.ellipsis, overflow: TextOverflow.fade,
softWrap: false,
), ),
)); ));
} }
@ -319,26 +323,26 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
return entities; return entities;
} }
Widget _buildEntityAction(String entityId) { Widget _buildEntityStateWidget(data) {
var entity = _entitiesData[entityId]; String entityId = data["entity_id"];
Widget result; Widget result;
if (entity["actionType"] == "switch") { if (data["actionType"] == "switch") {
result = Switch( result = Switch(
value: (entity["state"] == "on"), value: (data["state"] == "on"),
onChanged: ((state) { onChanged: ((state) {
_callService( _callService(
entity["domain"], state ? "turn_on" : "turn_off", entityId); data["domain"], state ? "turn_on" : "turn_off", entityId);
setState(() { setState(() {
_entitiesData[entityId]["state"] = state ? "on" : "off"; _entitiesData[entityId]["state"] = state ? "on" : "off";
}); });
}), }),
); );
} else if (entity["actionType"] == "statelessIcon") { } else if (data["actionType"] == "statelessIcon") {
result = SizedBox( result = SizedBox(
width: 60.0, width: 60.0,
child: FlatButton( child: FlatButton(
onPressed: (() { onPressed: (() {
_callService(entity["domain"], "turn_on", entityId); _callService(data["domain"], "turn_on", entityId);
}), }),
child: Text( child: Text(
"Run", "Run",
@ -350,7 +354,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
result = Padding( result = Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 16.0, 0.0), padding: EdgeInsets.fromLTRB(0.0, 0.0, 16.0, 0.0),
child: Text( 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, textAlign: TextAlign.right,
style: new TextStyle( style: new TextStyle(
fontSize: 16.0, fontSize: 16.0,
@ -370,7 +374,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
_uiStructure.forEach((viewId, structure) { _uiStructure.forEach((viewId, structure) {
result.add( result.add(
Tab( Tab(
icon: Icon(MaterialDesignIcons.createIconDataFromEntityData(structure)) icon: MaterialDesignIcons.createIconFromEntityData(structure, 24.0, null)
) )
); );
}); });

View File

@ -40,6 +40,7 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
prefs.setString("hassio-port", _hassioPort); prefs.setString("hassio-port", _hassioPort);
prefs.setString("hassio-password", _hassioPassword); prefs.setString("hassio-password", _hassioPassword);
prefs.setString("hassio-protocol", _socketProtocol); prefs.setString("hassio-protocol", _socketProtocol);
prefs.setString("hassio-res-protocol", _socketProtocol == "wss" ? "https" : "http");
prefs.setString("hassio-auth-type", _authType); prefs.setString("hassio-auth-type", _authType);
} }

View File

@ -36,6 +36,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.4" 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: charcode:
dependency: transitive dependency: transitive
description: description:
@ -92,6 +99,13 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" 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: flutter_launcher_icons:
dependency: "direct main" dependency: "direct main"
description: description:
@ -251,6 +265,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.6.2" version: "1.6.2"
path_provider:
dependency: transitive
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.1"
petitparser: petitparser:
dependency: transitive dependency: transitive
description: description:
@ -375,6 +396,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.4" version: "1.0.4"
synchronized:
dependency: transitive
description:
name: synchronized
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.3"
term_glyph: term_glyph:
dependency: transitive dependency: transitive
description: description:
@ -403,6 +431,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.9.0+5" version: "0.9.0+5"
uuid:
dependency: transitive
description:
name: uuid
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:

View File

@ -14,6 +14,7 @@ dependencies:
event_bus: ^1.0.1 event_bus: ^1.0.1
package_info: ^0.3.2 package_info: ^0.3.2
flutter_launcher_icons: ^0.6.1 flutter_launcher_icons: ^0.6.1
cached_network_image: ^0.4.1
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.