2018-09-10 00:34:52 +03:00
|
|
|
import 'dart:convert';
|
2018-09-15 01:46:15 +03:00
|
|
|
import 'dart:async';
|
2018-09-15 12:56:42 +03:00
|
|
|
import 'package:flutter/rendering.dart';
|
2018-09-10 00:34:52 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2018-09-10 03:06:35 +03:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2018-09-12 00:32:04 +03:00
|
|
|
import 'package:web_socket_channel/io.dart';
|
2018-09-15 01:46:15 +03:00
|
|
|
import 'package:progress_indicators/progress_indicators.dart';
|
2018-09-16 18:02:12 +03:00
|
|
|
import 'package:event_bus/event_bus.dart';
|
2018-09-16 19:24:26 +03:00
|
|
|
import 'package:flutter/widgets.dart';
|
2018-09-23 02:04:44 +03:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2018-09-24 22:12:56 +03:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2018-09-25 22:11:31 +03:00
|
|
|
import 'package:flutter/services.dart';
|
2018-09-10 03:06:35 +03:00
|
|
|
|
2018-09-25 22:47:06 +03:00
|
|
|
part 'settings.page.dart';
|
|
|
|
part 'data_provider.class.dart';
|
|
|
|
part 'log.page.dart';
|
|
|
|
part 'utils.class.dart';
|
|
|
|
part 'mdi.class.dart';
|
2018-09-10 00:34:52 +03:00
|
|
|
|
2018-09-16 18:02:12 +03:00
|
|
|
EventBus eventBus = new EventBus();
|
2018-09-18 23:21:05 +03:00
|
|
|
const String appName = "HA Client";
|
2018-09-25 23:41:14 +03:00
|
|
|
const appVersion = "0.1.3";
|
2018-09-16 18:02:12 +03:00
|
|
|
|
2018-09-23 02:04:44 +03:00
|
|
|
String homeAssistantWebHost;
|
|
|
|
|
2018-09-24 20:07:22 +03:00
|
|
|
void main() {
|
|
|
|
FlutterError.onError = (errorDetails) {
|
|
|
|
TheLogger.log("Error", "${errorDetails.exception}");
|
|
|
|
if (TheLogger.isInDebugMode) {
|
|
|
|
FlutterError.dumpErrorToConsole(errorDetails);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
runZoned(() {
|
2018-09-25 22:47:06 +03:00
|
|
|
runApp(new HAClientApp());
|
2018-09-24 20:07:22 +03:00
|
|
|
}, onError: (error, stack) {
|
|
|
|
TheLogger.log("Global error", "$error");
|
|
|
|
});
|
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
|
2018-09-25 22:47:06 +03:00
|
|
|
class HAClientApp extends StatelessWidget {
|
2018-09-10 00:34:52 +03:00
|
|
|
// This widget is the root of your application.
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return new MaterialApp(
|
2018-09-17 01:03:34 +03:00
|
|
|
title: appName,
|
2018-09-10 00:34:52 +03:00
|
|
|
theme: new ThemeData(
|
|
|
|
primarySwatch: Colors.blue,
|
|
|
|
),
|
2018-09-10 03:06:35 +03:00
|
|
|
initialRoute: "/",
|
|
|
|
routes: {
|
|
|
|
"/": (context) => MainPage(title: 'Hass Client'),
|
2018-09-24 00:05:57 +03:00
|
|
|
"/connection-settings": (context) => ConnectionSettingsPage(title: "Connection Settings"),
|
|
|
|
"/log-view": (context) => LogViewPage(title: "Log")
|
2018-09-10 03:06:35 +03:00
|
|
|
},
|
2018-09-10 00:34:52 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 03:06:35 +03:00
|
|
|
class MainPage extends StatefulWidget {
|
|
|
|
MainPage({Key key, this.title}) : super(key: key);
|
2018-09-10 00:34:52 +03:00
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
@override
|
2018-09-10 03:06:35 +03:00
|
|
|
_MainPageState createState() => new _MainPageState();
|
2018-09-10 00:34:52 +03:00
|
|
|
}
|
|
|
|
|
2018-09-16 19:24:26 +03:00
|
|
|
class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
2018-09-25 22:47:06 +03:00
|
|
|
HADataProvider _dataModel;
|
2018-09-15 01:46:15 +03:00
|
|
|
Map _entitiesData;
|
2018-09-15 12:56:42 +03:00
|
|
|
Map _uiStructure;
|
2018-09-17 00:28:19 +03:00
|
|
|
Map _instanceConfig;
|
2018-09-16 14:58:21 +03:00
|
|
|
int _uiViewsCount = 0;
|
2018-09-17 00:28:19 +03:00
|
|
|
String _instanceHost;
|
2018-09-20 23:21:03 +03:00
|
|
|
int _errorCodeToBeShown = 0;
|
|
|
|
String _lastErrorMessage = "";
|
|
|
|
StreamSubscription _stateSubscription;
|
2018-09-21 00:39:49 +03:00
|
|
|
StreamSubscription _settingsSubscription;
|
2018-09-20 23:21:03 +03:00
|
|
|
bool _isLoading = true;
|
2018-09-23 16:41:50 +03:00
|
|
|
Map<String, Color> _stateIconColors = {
|
2018-09-16 00:06:07 +03:00
|
|
|
"on": Colors.amber,
|
2018-09-23 16:41:50 +03:00
|
|
|
"off": Color.fromRGBO(68, 115, 158, 1.0),
|
2018-09-16 00:06:07 +03:00
|
|
|
"unavailable": Colors.black12,
|
|
|
|
"unknown": Colors.black12,
|
|
|
|
"playing": Colors.amber
|
|
|
|
};
|
2018-09-23 16:41:50 +03:00
|
|
|
Map<String, Color> _badgeColors = {
|
|
|
|
"default": Color.fromRGBO(223, 76, 30, 1.0),
|
|
|
|
"binary_sensor": Color.fromRGBO(3, 155, 229, 1.0)
|
|
|
|
};
|
2018-09-10 03:06:35 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2018-09-16 19:24:26 +03:00
|
|
|
WidgetsBinding.instance.addObserver(this);
|
2018-09-21 00:39:49 +03:00
|
|
|
_settingsSubscription = eventBus.on<SettingsChangedEvent>().listen((event) {
|
2018-09-24 00:05:57 +03:00
|
|
|
TheLogger.log("Debug","Settings change event: reconnect=${event.reconnect}");
|
2018-09-20 23:21:03 +03:00
|
|
|
setState(() {
|
|
|
|
_errorCodeToBeShown = 0;
|
|
|
|
});
|
|
|
|
_initConnection();
|
|
|
|
});
|
|
|
|
_initConnection();
|
2018-09-10 03:06:35 +03:00
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
|
2018-09-16 19:24:26 +03:00
|
|
|
@override
|
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
2018-09-24 00:05:57 +03:00
|
|
|
TheLogger.log("Debug","$state");
|
2018-09-16 19:24:26 +03:00
|
|
|
if (state == AppLifecycleState.resumed) {
|
|
|
|
_refreshData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 23:21:03 +03:00
|
|
|
_initConnection() async {
|
2018-09-10 03:06:35 +03:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2018-09-17 00:28:19 +03:00
|
|
|
String domain = prefs.getString('hassio-domain');
|
|
|
|
String port = prefs.getString('hassio-port');
|
2018-09-17 22:20:36 +03:00
|
|
|
_instanceHost = "$domain:$port";
|
2018-09-20 23:21:03 +03:00
|
|
|
String apiEndpoint = "${prefs.getString('hassio-protocol')}://$domain:$port/api/websocket";
|
2018-09-23 02:04:44 +03:00
|
|
|
homeAssistantWebHost = "${prefs.getString('hassio-res-protocol')}://$domain:$port";
|
2018-09-20 23:21:03 +03:00
|
|
|
String apiPassword = prefs.getString('hassio-password');
|
2018-09-21 00:39:49 +03:00
|
|
|
String authType = prefs.getString('hassio-auth-type');
|
|
|
|
if ((domain == null) || (port == null) || (apiPassword == null) ||
|
|
|
|
(domain.length == 0) || (port.length == 0) || (apiPassword.length == 0)) {
|
2018-09-20 23:21:03 +03:00
|
|
|
setState(() {
|
|
|
|
_errorCodeToBeShown = 5;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (_dataModel != null) _dataModel.closeConnection();
|
2018-09-21 00:39:49 +03:00
|
|
|
_createConnection(apiEndpoint, apiPassword, authType);
|
2018-09-20 23:21:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 00:39:49 +03:00
|
|
|
_createConnection(String apiEndpoint, String apiPassword, String authType) {
|
2018-09-25 22:47:06 +03:00
|
|
|
_dataModel = HADataProvider(apiEndpoint, apiPassword, authType);
|
2018-09-17 01:03:34 +03:00
|
|
|
_refreshData();
|
2018-09-20 23:21:03 +03:00
|
|
|
if (_stateSubscription != null) _stateSubscription.cancel();
|
|
|
|
_stateSubscription = eventBus.on<StateChangedEvent>().listen((event) {
|
2018-09-16 18:02:12 +03:00
|
|
|
setState(() {
|
|
|
|
_entitiesData = _dataModel.entities;
|
|
|
|
});
|
|
|
|
});
|
2018-09-12 00:32:04 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 01:46:15 +03:00
|
|
|
_refreshData() async {
|
2018-09-12 00:32:04 +03:00
|
|
|
setState(() {
|
2018-09-20 23:21:03 +03:00
|
|
|
_isLoading = true;
|
2018-09-12 00:32:04 +03:00
|
|
|
});
|
2018-09-20 23:21:03 +03:00
|
|
|
_errorCodeToBeShown = 0;
|
2018-09-15 01:46:15 +03:00
|
|
|
if (_dataModel != null) {
|
|
|
|
await _dataModel.fetch().then((result) {
|
|
|
|
setState(() {
|
2018-09-17 00:28:19 +03:00
|
|
|
_instanceConfig = _dataModel.instanceConfig;
|
2018-09-15 12:56:42 +03:00
|
|
|
_entitiesData = _dataModel.entities;
|
|
|
|
_uiStructure = _dataModel.uiStructure;
|
2018-09-16 14:58:21 +03:00
|
|
|
_uiViewsCount = _uiStructure.length;
|
2018-09-20 23:21:03 +03:00
|
|
|
_isLoading = false;
|
2018-09-15 01:46:15 +03:00
|
|
|
});
|
|
|
|
}).catchError((e) {
|
2018-09-21 00:01:53 +03:00
|
|
|
_setErrorState(e);
|
2018-09-12 00:32:04 +03:00
|
|
|
});
|
2018-09-15 01:46:15 +03:00
|
|
|
}
|
2018-09-11 01:09:21 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 00:01:53 +03:00
|
|
|
_setErrorState(e) {
|
|
|
|
setState(() {
|
|
|
|
_errorCodeToBeShown = e["errorCode"] != null ? e["errorCode"] : 99;
|
|
|
|
_lastErrorMessage = e["errorMessage"] ?? "Unknown error";
|
|
|
|
_isLoading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _callService(String domain, String service, String entityId) {
|
|
|
|
setState(() {
|
|
|
|
_isLoading = true;
|
|
|
|
});
|
|
|
|
_dataModel.callService(domain, service, entityId).then((r) {
|
|
|
|
setState(() {
|
|
|
|
_isLoading = false;
|
|
|
|
});
|
|
|
|
}).catchError((e) => _setErrorState(e));
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:05:12 +03:00
|
|
|
List<Widget> _buildViews() {
|
|
|
|
List<Widget> result = [];
|
|
|
|
if ((_entitiesData != null) && (_uiStructure != null)) {
|
|
|
|
_uiStructure.forEach((viewId, structure) {
|
|
|
|
result.add(
|
|
|
|
RefreshIndicator(
|
|
|
|
color: Colors.amber,
|
|
|
|
child: ListView(
|
|
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
|
|
children: _buildSingleView(structure),
|
|
|
|
),
|
|
|
|
onRefresh: () => _refreshData(),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
2018-09-15 12:56:42 +03:00
|
|
|
}
|
2018-09-16 00:06:07 +03:00
|
|
|
return result;
|
|
|
|
}
|
2018-09-15 12:56:42 +03:00
|
|
|
|
2018-09-21 23:05:12 +03:00
|
|
|
List<Widget> _buildSingleView(structure) {
|
|
|
|
List<Widget> result = [];
|
2018-09-23 00:35:16 +03:00
|
|
|
if (structure["badges"]["children"].length > 0) {
|
|
|
|
result.add(
|
|
|
|
Wrap(
|
|
|
|
alignment: WrapAlignment.center,
|
|
|
|
spacing: 10.0,
|
|
|
|
runSpacing: 4.0,
|
|
|
|
children: _buildBadges(structure["badges"]["children"]),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2018-09-21 23:05:12 +03:00
|
|
|
structure["groups"].forEach((id, group) {
|
|
|
|
if (group["children"].length > 0) {
|
|
|
|
result.add(_buildCard(
|
|
|
|
group["children"], group["friendly_name"].toString()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-23 00:35:16 +03:00
|
|
|
List<Widget> _buildBadges(List ids) {
|
|
|
|
List<Widget> result = [];
|
|
|
|
ids.forEach((entityId) {
|
|
|
|
var data = _entitiesData[entityId];
|
2018-09-24 00:05:57 +03:00
|
|
|
if (data != null) {
|
2018-09-23 00:35:16 +03:00
|
|
|
result.add(
|
2018-09-24 00:05:57 +03:00
|
|
|
_buildSingleBadge(data)
|
2018-09-23 00:35:16 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildSingleBadge(data) {
|
2018-09-23 16:07:41 +03:00
|
|
|
double iconSize = 26.0;
|
2018-09-23 00:35:16 +03:00
|
|
|
Widget badgeIcon;
|
2018-09-23 02:39:45 +03:00
|
|
|
String badgeTextValue;
|
2018-09-23 16:41:50 +03:00
|
|
|
Color iconColor = _badgeColors[data["domain"]] ?? _badgeColors["default"];
|
2018-09-23 00:35:16 +03:00
|
|
|
switch (data["domain"]) {
|
|
|
|
case "sun": {
|
2018-09-23 16:07:41 +03:00
|
|
|
badgeIcon = data["state"] == "below_horizon" ?
|
|
|
|
Icon(
|
|
|
|
MaterialDesignIcons.createIconDataFromIconCode(0xf0dc),
|
|
|
|
size: iconSize,
|
|
|
|
) :
|
|
|
|
Icon(
|
|
|
|
MaterialDesignIcons.createIconDataFromIconCode(0xf5a8),
|
|
|
|
size: iconSize,
|
|
|
|
);
|
2018-09-23 00:35:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "sensor": {
|
2018-09-23 02:39:45 +03:00
|
|
|
badgeTextValue = data["attributes"]["unit_of_measurement"];
|
2018-09-23 00:35:16 +03:00
|
|
|
badgeIcon = Center(
|
|
|
|
child: Text(
|
|
|
|
"${data['state']}",
|
2018-09-23 02:04:44 +03:00
|
|
|
overflow: TextOverflow.fade,
|
|
|
|
softWrap: false,
|
2018-09-23 00:35:16 +03:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(fontSize: 18.0),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-23 02:39:45 +03:00
|
|
|
case "device_tracker": {
|
2018-09-23 16:07:41 +03:00
|
|
|
badgeIcon = MaterialDesignIcons.createIconFromEntityData(data, iconSize,Colors.black);
|
2018-09-23 02:39:45 +03:00
|
|
|
badgeTextValue = data["state"];
|
|
|
|
break;
|
|
|
|
}
|
2018-09-23 00:35:16 +03:00
|
|
|
default: {
|
2018-09-23 16:07:41 +03:00
|
|
|
badgeIcon = MaterialDesignIcons.createIconFromEntityData(data, iconSize,Colors.black);
|
2018-09-23 00:35:16 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-23 02:39:45 +03:00
|
|
|
Widget badgeText;
|
|
|
|
if (badgeTextValue == null) {
|
|
|
|
badgeText = Container(width: 0.0, height: 0.0);
|
|
|
|
} else {
|
|
|
|
badgeText = Container(
|
|
|
|
padding: EdgeInsets.fromLTRB(6.0, 2.0, 6.0, 2.0),
|
|
|
|
child: Text("$badgeTextValue",
|
|
|
|
style: TextStyle(fontSize: 13.0, color: Colors.white),
|
|
|
|
textAlign: TextAlign.center, softWrap: false, overflow: TextOverflow.fade),
|
|
|
|
decoration: new BoxDecoration(
|
|
|
|
// Circle shape
|
|
|
|
//shape: BoxShape.circle,
|
2018-09-23 16:41:50 +03:00
|
|
|
color: iconColor,
|
2018-09-23 02:39:45 +03:00
|
|
|
borderRadius: BorderRadius.circular(9.0),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2018-09-23 00:35:16 +03:00
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
|
|
|
margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
|
|
|
|
width: 50.0,
|
|
|
|
height: 50.0,
|
|
|
|
decoration: new BoxDecoration(
|
|
|
|
// Circle shape
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
color: Colors.white,
|
|
|
|
// The border you want
|
|
|
|
border: new Border.all(
|
|
|
|
width: 2.0,
|
2018-09-23 16:41:50 +03:00
|
|
|
color: iconColor,
|
2018-09-23 00:35:16 +03:00
|
|
|
),
|
|
|
|
),
|
2018-09-23 02:39:45 +03:00
|
|
|
child: Stack(
|
|
|
|
overflow: Overflow.visible,
|
|
|
|
children: <Widget>[
|
|
|
|
Positioned(
|
|
|
|
width: 46.0,
|
|
|
|
height: 46.0,
|
|
|
|
top: 0.0,
|
|
|
|
left: 0.0,
|
|
|
|
child: badgeIcon,
|
|
|
|
),
|
|
|
|
Positioned(
|
|
|
|
//width: 50.0,
|
|
|
|
bottom: -9.0,
|
|
|
|
left: -15.0,
|
|
|
|
right: -15.0,
|
|
|
|
child: Center(
|
|
|
|
child: badgeText,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2018-09-23 00:35:16 +03:00
|
|
|
),
|
|
|
|
Container(
|
|
|
|
width: 60.0,
|
|
|
|
child: Text(
|
|
|
|
"${data['display_name']}",
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
softWrap: true,
|
|
|
|
maxLines: 2,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:05:12 +03:00
|
|
|
Card _buildCard(List ids, String name) {
|
2018-09-15 12:56:42 +03:00
|
|
|
List<Widget> body = [];
|
2018-09-16 14:58:21 +03:00
|
|
|
body.add(_buildCardHeader(name));
|
|
|
|
body.addAll(_buildCardBody(ids));
|
2018-09-16 00:06:07 +03:00
|
|
|
Card result =
|
2018-09-21 23:05:12 +03:00
|
|
|
Card(child: new Column(mainAxisSize: MainAxisSize.min, children: body));
|
2018-09-15 12:56:42 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-16 14:58:21 +03:00
|
|
|
Widget _buildCardHeader(String name) {
|
2018-09-15 12:56:42 +03:00
|
|
|
var result;
|
|
|
|
if (name.length > 0) {
|
|
|
|
result = new ListTile(
|
|
|
|
//leading: const Icon(Icons.device_hub),
|
|
|
|
//subtitle: Text(".."),
|
|
|
|
//trailing: Text("${data["state"]}"),
|
2018-09-16 00:06:07 +03:00
|
|
|
title: Text("$name",
|
2018-09-15 12:56:42 +03:00
|
|
|
textAlign: TextAlign.left,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
2018-09-16 00:06:07 +03:00
|
|
|
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0)),
|
2018-09-15 12:56:42 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
result = new Container(width: 0.0, height: 0.0);
|
|
|
|
}
|
|
|
|
return result;
|
2018-09-10 00:34:52 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 23:05:12 +03:00
|
|
|
List<Widget> _buildCardBody(List ids) {
|
2018-09-15 12:56:42 +03:00
|
|
|
List<Widget> entities = [];
|
2018-09-16 00:06:07 +03:00
|
|
|
ids.forEach((id) {
|
2018-09-15 12:56:42 +03:00
|
|
|
var data = _entitiesData[id];
|
2018-09-24 00:05:57 +03:00
|
|
|
if (data != null) {
|
2018-09-17 22:20:36 +03:00
|
|
|
entities.add(new ListTile(
|
2018-09-23 02:39:45 +03:00
|
|
|
leading: MaterialDesignIcons.createIconFromEntityData(data, 28.0, _stateIconColors[data["state"]] ?? Colors.blueGrey),
|
2018-09-17 22:20:36 +03:00
|
|
|
//subtitle: Text("${data['entity_id']}"),
|
2018-09-23 16:41:50 +03:00
|
|
|
trailing: _buildEntityActionWidget(data),
|
2018-09-17 22:20:36 +03:00
|
|
|
title: Text(
|
|
|
|
"${data["display_name"]}",
|
2018-09-23 02:04:44 +03:00
|
|
|
overflow: TextOverflow.fade,
|
|
|
|
softWrap: false,
|
2018-09-17 22:20:36 +03:00
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
2018-09-15 12:56:42 +03:00
|
|
|
});
|
|
|
|
return entities;
|
2018-09-12 22:31:58 +03:00
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
|
2018-09-23 16:41:50 +03:00
|
|
|
Widget _buildEntityActionWidget(data) {
|
2018-09-23 02:04:44 +03:00
|
|
|
String entityId = data["entity_id"];
|
2018-09-21 23:05:12 +03:00
|
|
|
Widget result;
|
2018-09-23 16:41:50 +03:00
|
|
|
switch (data["domain"]) {
|
|
|
|
case "automation":
|
|
|
|
case "switch":
|
|
|
|
case "light": {
|
|
|
|
result = Switch(
|
|
|
|
value: (data["state"] == "on"),
|
|
|
|
onChanged: ((state) {
|
|
|
|
_callService(
|
|
|
|
data["domain"], state ? "turn_on" : "turn_off", entityId);
|
|
|
|
setState(() {
|
|
|
|
_entitiesData[entityId]["state"] = state ? "on" : "off";
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "script":
|
|
|
|
case "scene": {
|
|
|
|
result = SizedBox(
|
2018-09-21 23:05:12 +03:00
|
|
|
width: 60.0,
|
|
|
|
child: FlatButton(
|
|
|
|
onPressed: (() {
|
2018-09-23 02:04:44 +03:00
|
|
|
_callService(data["domain"], "turn_on", entityId);
|
2018-09-21 23:05:12 +03:00
|
|
|
}),
|
|
|
|
child: Text(
|
|
|
|
"Run",
|
|
|
|
textAlign: TextAlign.right,
|
|
|
|
style: new TextStyle(fontSize: 16.0, color: Colors.blue),
|
|
|
|
),
|
2018-09-23 16:41:50 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
result = Padding(
|
2018-09-21 23:05:12 +03:00
|
|
|
padding: EdgeInsets.fromLTRB(0.0, 0.0, 16.0, 0.0),
|
|
|
|
child: Text(
|
2018-09-23 16:41:50 +03:00
|
|
|
"${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,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2018-09-15 01:46:15 +03:00
|
|
|
}
|
2018-09-23 16:41:50 +03:00
|
|
|
|
2018-09-21 23:05:12 +03:00
|
|
|
/*return SizedBox(
|
|
|
|
width: 60.0,
|
|
|
|
// height: double.infinity,
|
|
|
|
child: result
|
|
|
|
);*/
|
2018-09-16 14:58:21 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Tab> buildUIViewTabs() {
|
|
|
|
List<Tab> result = [];
|
|
|
|
if ((_entitiesData != null) && (_uiStructure != null)) {
|
|
|
|
_uiStructure.forEach((viewId, structure) {
|
|
|
|
result.add(
|
2018-09-17 22:20:36 +03:00
|
|
|
Tab(
|
2018-09-23 02:04:44 +03:00
|
|
|
icon: MaterialDesignIcons.createIconFromEntityData(structure, 24.0, null)
|
2018-09-17 22:20:36 +03:00
|
|
|
)
|
2018-09-16 14:58:21 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
2018-09-15 01:46:15 +03:00
|
|
|
}
|
|
|
|
|
2018-09-18 00:12:11 +03:00
|
|
|
Widget _buildAppTitle() {
|
2018-09-15 01:46:15 +03:00
|
|
|
Row titleRow = Row(
|
2018-09-17 01:03:34 +03:00
|
|
|
children: [Text(_instanceConfig != null ? _instanceConfig["location_name"] : "")],
|
2018-09-15 01:46:15 +03:00
|
|
|
);
|
2018-09-20 23:21:03 +03:00
|
|
|
if (_isLoading) {
|
2018-09-15 01:46:15 +03:00
|
|
|
titleRow.children.add(Padding(
|
|
|
|
child: JumpingDotsProgressIndicator(
|
2018-09-17 00:28:19 +03:00
|
|
|
fontSize: 26.0,
|
2018-09-15 01:46:15 +03:00
|
|
|
color: Colors.white,
|
|
|
|
),
|
2018-09-17 00:28:19 +03:00
|
|
|
padding: const EdgeInsets.fromLTRB(5.0, 0.0, 0.0, 30.0),
|
2018-09-16 00:06:07 +03:00
|
|
|
));
|
2018-09-15 01:46:15 +03:00
|
|
|
}
|
|
|
|
return titleRow;
|
2018-09-10 00:34:52 +03:00
|
|
|
}
|
|
|
|
|
2018-09-16 14:58:21 +03:00
|
|
|
Drawer _buildAppDrawer() {
|
|
|
|
return new Drawer(
|
|
|
|
child: ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
new UserAccountsDrawerHeader(
|
2018-09-17 00:28:19 +03:00
|
|
|
accountName: Text(_instanceConfig != null ? _instanceConfig["location_name"] : "Unknown"),
|
|
|
|
accountEmail: Text(_instanceHost ?? "Not configured"),
|
|
|
|
currentAccountPicture: new Image.asset('images/hassio-192x192.png'),
|
2018-09-16 14:58:21 +03:00
|
|
|
),
|
|
|
|
new ListTile(
|
|
|
|
leading: Icon(Icons.settings),
|
2018-09-17 00:28:19 +03:00
|
|
|
title: Text("Connection settings"),
|
2018-09-16 14:58:21 +03:00
|
|
|
onTap: () {
|
2018-09-24 22:23:01 +03:00
|
|
|
Navigator.of(context).pop();
|
|
|
|
Navigator.of(context).pushNamed('/connection-settings');
|
2018-09-16 14:58:21 +03:00
|
|
|
},
|
|
|
|
),
|
2018-09-24 00:05:57 +03:00
|
|
|
new ListTile(
|
|
|
|
leading: Icon(Icons.insert_drive_file),
|
|
|
|
title: Text("Log"),
|
|
|
|
onTap: () {
|
2018-09-24 22:23:01 +03:00
|
|
|
Navigator.of(context).pop();
|
|
|
|
Navigator.of(context).pushNamed('/log-view');
|
2018-09-24 00:05:57 +03:00
|
|
|
},
|
|
|
|
),
|
2018-09-24 22:12:56 +03:00
|
|
|
new ListTile(
|
|
|
|
leading: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:github-circle")),
|
|
|
|
title: Text("Reprot issue"),
|
|
|
|
onTap: () {
|
2018-09-24 22:23:01 +03:00
|
|
|
Navigator.of(context).pop();
|
2018-09-24 22:12:56 +03:00
|
|
|
haUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new");
|
|
|
|
},
|
|
|
|
),
|
2018-09-16 14:58:21 +03:00
|
|
|
new AboutListTile(
|
2018-09-17 01:03:34 +03:00
|
|
|
applicationName: appName,
|
|
|
|
applicationVersion: appVersion,
|
2018-09-21 00:57:30 +03:00
|
|
|
applicationLegalese: "Keyboard Crumbs | www.keyboardcrumbs.io",
|
2018-09-16 14:58:21 +03:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:12:11 +03:00
|
|
|
_checkShowInfo(BuildContext context) {
|
2018-09-20 23:21:03 +03:00
|
|
|
if (_errorCodeToBeShown > 0) {
|
|
|
|
String message = _lastErrorMessage;
|
2018-09-18 00:12:11 +03:00
|
|
|
SnackBarAction action;
|
2018-09-20 23:21:03 +03:00
|
|
|
switch (_errorCodeToBeShown) {
|
2018-09-18 00:12:11 +03:00
|
|
|
case 1: {
|
|
|
|
action = SnackBarAction(
|
|
|
|
label: "Retry",
|
2018-09-20 23:21:03 +03:00
|
|
|
onPressed: () {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
|
|
|
_refreshData();
|
|
|
|
},
|
2018-09-18 00:12:11 +03:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-20 23:21:03 +03:00
|
|
|
|
|
|
|
case 5: {
|
|
|
|
message = "Check connection settings";
|
|
|
|
action = SnackBarAction(
|
|
|
|
label: "Open",
|
|
|
|
onPressed: () {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
|
|
|
Navigator.pushNamed(context, '/connection-settings');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 6: {
|
|
|
|
action = SnackBarAction(
|
|
|
|
label: "Settings",
|
|
|
|
onPressed: () {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
|
|
|
Navigator.pushNamed(context, '/connection-settings');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 7: {
|
|
|
|
action = SnackBarAction(
|
|
|
|
label: "Retry",
|
|
|
|
onPressed: () {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
|
|
|
_refreshData();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-21 00:01:53 +03:00
|
|
|
|
|
|
|
case 8: {
|
|
|
|
action = SnackBarAction(
|
|
|
|
label: "Reconnect",
|
|
|
|
onPressed: () {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
|
|
|
_refreshData();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-18 00:12:11 +03:00
|
|
|
}
|
|
|
|
Timer(Duration(seconds: 1), () {
|
|
|
|
_scaffoldKey.currentState.hideCurrentSnackBar();
|
|
|
|
_scaffoldKey.currentState.showSnackBar(
|
|
|
|
SnackBar(
|
2018-09-20 23:21:03 +03:00
|
|
|
content: Text("$message (code: $_errorCodeToBeShown)"),
|
2018-09-18 00:12:11 +03:00
|
|
|
action: action,
|
|
|
|
duration: Duration(hours: 1),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
2018-09-21 00:39:49 +03:00
|
|
|
} else {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
2018-09-18 00:12:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
|
|
|
|
|
2018-09-24 22:54:51 +03:00
|
|
|
Scaffold _buildScaffold(bool empty) {
|
|
|
|
return Scaffold(
|
|
|
|
key: _scaffoldKey,
|
|
|
|
appBar: AppBar(
|
|
|
|
title: _buildAppTitle(),
|
|
|
|
bottom: empty ? null : TabBar(tabs: buildUIViewTabs()),
|
|
|
|
),
|
|
|
|
drawer: _buildAppDrawer(),
|
|
|
|
body: empty ?
|
|
|
|
Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Icon(
|
|
|
|
MaterialDesignIcons.createIconDataFromIconName("mdi:home-assistant"),
|
|
|
|
size: 100.0,
|
|
|
|
color: _errorCodeToBeShown == 0 ? Colors.blue : Colors.redAccent,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
:
|
|
|
|
TabBarView(
|
|
|
|
children: _buildViews()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-10 00:34:52 +03:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2018-09-18 00:12:11 +03:00
|
|
|
_checkShowInfo(context);
|
2018-09-16 14:58:21 +03:00
|
|
|
// This method is rerun every time setState is called.
|
|
|
|
if (_entitiesData == null) {
|
2018-09-24 22:54:51 +03:00
|
|
|
return _buildScaffold(true);
|
2018-09-16 14:58:21 +03:00
|
|
|
} else {
|
|
|
|
return DefaultTabController(
|
|
|
|
length: _uiViewsCount,
|
2018-09-24 22:54:51 +03:00
|
|
|
child: _buildScaffold(false)
|
2018-09-16 14:58:21 +03:00
|
|
|
);
|
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
}
|
2018-09-12 00:32:04 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2018-09-16 19:24:26 +03:00
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
2018-09-21 00:39:49 +03:00
|
|
|
if (_stateSubscription != null) _stateSubscription.cancel();
|
|
|
|
if (_settingsSubscription != null) _settingsSubscription.cancel();
|
2018-09-16 19:24:26 +03:00
|
|
|
_dataModel.closeConnection();
|
2018-09-12 00:32:04 +03:00
|
|
|
super.dispose();
|
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
}
|