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:http/http.dart' as http;
|
|
|
|
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';
|
|
|
|
import 'package:web_socket_channel/status.dart' as socketStatus;
|
2018-09-15 01:46:15 +03:00
|
|
|
import 'package:progress_indicators/progress_indicators.dart';
|
2018-09-10 03:06:35 +03:00
|
|
|
|
|
|
|
part 'settings.dart';
|
2018-09-15 01:46:15 +03:00
|
|
|
part 'data_model.dart';
|
2018-09-10 00:34:52 +03:00
|
|
|
|
2018-09-12 00:32:04 +03:00
|
|
|
void main() => runApp(new HassClientApp());
|
2018-09-10 00:34:52 +03:00
|
|
|
|
2018-09-12 00:32:04 +03:00
|
|
|
class HassClientApp 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(
|
|
|
|
title: 'Hass Client',
|
|
|
|
theme: new ThemeData(
|
|
|
|
primarySwatch: Colors.blue,
|
|
|
|
),
|
2018-09-10 03:06:35 +03:00
|
|
|
initialRoute: "/",
|
|
|
|
routes: {
|
|
|
|
"/": (context) => MainPage(title: 'Hass Client'),
|
|
|
|
"/settings": (context) => SettingsPage(title: "Settings")
|
|
|
|
},
|
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-10 03:06:35 +03:00
|
|
|
class _MainPageState extends State<MainPage> {
|
2018-09-15 01:46:15 +03:00
|
|
|
HassioDataModel _dataModel;
|
|
|
|
Map _entitiesData;
|
2018-09-15 12:56:42 +03:00
|
|
|
Map _uiStructure;
|
2018-09-15 01:46:15 +03:00
|
|
|
String _dataModelErrorMessage = "";
|
|
|
|
bool loading = true;
|
2018-09-16 00:06:07 +03:00
|
|
|
Map _stateIconColors = {
|
|
|
|
"on": Colors.amber,
|
|
|
|
"off": Colors.blueGrey,
|
|
|
|
"unavailable": Colors.black12,
|
|
|
|
"unknown": Colors.black12,
|
|
|
|
"playing": Colors.amber
|
|
|
|
};
|
2018-09-10 03:06:35 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2018-09-12 00:32:04 +03:00
|
|
|
_initClient();
|
2018-09-10 03:06:35 +03:00
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
|
2018-09-12 00:32:04 +03:00
|
|
|
_initClient() async {
|
2018-09-10 03:06:35 +03:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2018-09-16 00:06:07 +03:00
|
|
|
String _hassioAPIEndpoint = "wss://" +
|
|
|
|
prefs.getString('hassio-domain') +
|
|
|
|
":" +
|
|
|
|
prefs.getString('hassio-port') +
|
|
|
|
"/api/websocket";
|
2018-09-15 01:46:15 +03:00
|
|
|
String _hassioPassword = prefs.getString('hassio-password');
|
|
|
|
_dataModel = HassioDataModel(_hassioAPIEndpoint, _hassioPassword);
|
|
|
|
await _refreshData();
|
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-15 01:46:15 +03:00
|
|
|
loading = true;
|
2018-09-12 00:32:04 +03:00
|
|
|
});
|
2018-09-15 01:46:15 +03:00
|
|
|
_dataModelErrorMessage = "";
|
|
|
|
if (_dataModel != null) {
|
|
|
|
await _dataModel.fetch().then((result) {
|
|
|
|
setState(() {
|
2018-09-15 12:56:42 +03:00
|
|
|
_entitiesData = _dataModel.entities;
|
|
|
|
_uiStructure = _dataModel.uiStructure;
|
2018-09-15 01:46:15 +03:00
|
|
|
loading = false;
|
|
|
|
});
|
|
|
|
}).catchError((e) {
|
|
|
|
setState(() {
|
|
|
|
_dataModelErrorMessage = e.toString();
|
|
|
|
loading = false;
|
|
|
|
});
|
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-15 12:56:42 +03:00
|
|
|
Widget _buildEntityAction(String entityId) {
|
|
|
|
var entity = _entitiesData[entityId];
|
2018-09-16 00:06:07 +03:00
|
|
|
Widget result;
|
2018-09-15 12:56:42 +03:00
|
|
|
if (entity["actionType"] == "switch") {
|
2018-09-16 00:06:07 +03:00
|
|
|
result = Switch(
|
2018-09-15 12:56:42 +03:00
|
|
|
value: (entity["state"] == "on"),
|
|
|
|
onChanged: ((state) {
|
2018-09-16 00:06:07 +03:00
|
|
|
_dataModel.callService(
|
|
|
|
entity["domain"], state ? "turn_on" : "turn_off", entityId);
|
2018-09-15 12:56:42 +03:00
|
|
|
setState(() {
|
|
|
|
_entitiesData[entityId]["state"] = state ? "on" : "off";
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
2018-09-16 00:06:07 +03:00
|
|
|
} else if (entity["actionType"] == "statelessIcon") {
|
|
|
|
result = SizedBox(
|
|
|
|
width: 60.0,
|
|
|
|
child: FlatButton(
|
|
|
|
onPressed: (() {
|
|
|
|
_dataModel.callService(entity["domain"], "turn_on", entityId);
|
|
|
|
}),
|
|
|
|
child: Text(
|
|
|
|
"Run",
|
|
|
|
textAlign: TextAlign.right,
|
|
|
|
style: new TextStyle(fontSize: 16.0, color: Colors.blue),
|
2018-09-15 12:56:42 +03:00
|
|
|
),
|
2018-09-16 00:06:07 +03:00
|
|
|
));
|
2018-09-15 12:56:42 +03:00
|
|
|
} else {
|
2018-09-16 00:06:07 +03:00
|
|
|
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"] : ''}",
|
|
|
|
textAlign: TextAlign.right,
|
|
|
|
style: new TextStyle(
|
|
|
|
fontSize: 16.0,
|
|
|
|
)));
|
2018-09-15 12:56:42 +03:00
|
|
|
}
|
2018-09-16 00:06:07 +03:00
|
|
|
/*return SizedBox(
|
|
|
|
width: 60.0,
|
|
|
|
// height: double.infinity,
|
|
|
|
child: result
|
|
|
|
);*/
|
|
|
|
return result;
|
|
|
|
}
|
2018-09-15 12:56:42 +03:00
|
|
|
|
|
|
|
Card _buildEntityGroup(List<String> ids, String name) {
|
|
|
|
List<Widget> body = [];
|
|
|
|
body.add(_buildEntityGroupHeader(name));
|
|
|
|
body.addAll(_buildEntityGroupBody(ids));
|
2018-09-16 00:06:07 +03:00
|
|
|
Card result =
|
|
|
|
Card(child: new Column(mainAxisSize: MainAxisSize.min, children: body));
|
2018-09-15 12:56:42 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildEntityGroupHeader(String name) {
|
|
|
|
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-15 12:56:42 +03:00
|
|
|
List<Widget> _buildEntityGroupBody(List<String> ids) {
|
|
|
|
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-16 00:06:07 +03:00
|
|
|
entities.add(new ListTile(
|
|
|
|
leading: Icon(
|
|
|
|
IconData(data["iconCode"], fontFamily: 'Material Design Icons'),
|
|
|
|
color: _stateIconColors[data["state"]] ?? Colors.blueGrey,
|
|
|
|
),
|
|
|
|
//subtitle: Text("${data['entity_id']}"),
|
|
|
|
trailing: _buildEntityAction(id),
|
|
|
|
title: Text(
|
|
|
|
"${data["display_name"]}",
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
|
|
|
));
|
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-12 22:31:58 +03:00
|
|
|
List<Widget> buildEntitiesView() {
|
2018-09-16 00:06:07 +03:00
|
|
|
if ((_entitiesData != null) && (_uiStructure != null)) {
|
2018-09-15 01:46:15 +03:00
|
|
|
List<Widget> result = [];
|
|
|
|
if (_dataModelErrorMessage.length == 0) {
|
2018-09-15 12:56:42 +03:00
|
|
|
_uiStructure["standalone"].forEach((entityId) {
|
|
|
|
result.add(_buildEntityGroup([entityId], ""));
|
|
|
|
});
|
|
|
|
_uiStructure["groups"].forEach((group) {
|
2018-09-16 00:06:07 +03:00
|
|
|
result.add(_buildEntityGroup(
|
|
|
|
group["children"], group["friendly_name"].toString()));
|
2018-09-15 01:46:15 +03:00
|
|
|
});
|
|
|
|
} else {
|
2018-09-15 12:56:42 +03:00
|
|
|
//TODO
|
|
|
|
//result.add(Text(_dataModelErrorMessage));
|
2018-09-15 01:46:15 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} else {
|
2018-09-15 12:56:42 +03:00
|
|
|
//TODO
|
|
|
|
return [];
|
2018-09-15 01:46:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildTitle() {
|
|
|
|
Row titleRow = Row(
|
2018-09-16 00:06:07 +03:00
|
|
|
children: <Widget>[Text(widget.title)],
|
2018-09-15 01:46:15 +03:00
|
|
|
);
|
|
|
|
if (loading) {
|
|
|
|
titleRow.children.add(Padding(
|
|
|
|
child: JumpingDotsProgressIndicator(
|
|
|
|
fontSize: 30.0,
|
|
|
|
color: Colors.white,
|
|
|
|
),
|
|
|
|
padding: const EdgeInsets.fromLTRB(5.0, 0.0, 0.0, 40.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
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
// This method is rerun every time setState is called, for instance as done
|
|
|
|
// by the _incrementCounter method above.
|
|
|
|
//
|
|
|
|
// The Flutter framework has been optimized to make rerunning build methods
|
|
|
|
// fast, so that you can just rebuild anything that needs updating rather
|
|
|
|
// than having to individually change instances of widgets.
|
|
|
|
return new Scaffold(
|
|
|
|
appBar: new AppBar(
|
|
|
|
// 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.
|
2018-09-15 01:46:15 +03:00
|
|
|
title: _buildTitle(),
|
2018-09-10 00:34:52 +03:00
|
|
|
),
|
2018-09-10 01:25:25 +03:00
|
|
|
drawer: new Drawer(
|
|
|
|
child: ListView(
|
|
|
|
children: <Widget>[
|
2018-09-10 03:06:35 +03:00
|
|
|
new UserAccountsDrawerHeader(
|
2018-09-16 00:06:07 +03:00
|
|
|
accountName: Text("Edwin Home"),
|
|
|
|
accountEmail: Text("edwin-home.duckdns.org"),
|
|
|
|
currentAccountPicture: new CircleAvatar(
|
|
|
|
backgroundImage: new NetworkImage(
|
|
|
|
"https://edwin-home.duckdns.org:8123/static/icons/favicon-192x192.png"),
|
|
|
|
),
|
2018-09-10 03:06:35 +03:00
|
|
|
),
|
2018-09-10 01:25:25 +03:00
|
|
|
new ListTile(
|
|
|
|
leading: Icon(Icons.settings),
|
|
|
|
title: Text("Settings"),
|
2018-09-10 03:06:35 +03:00
|
|
|
onTap: () {
|
|
|
|
Navigator.pushNamed(context, '/settings');
|
|
|
|
},
|
2018-09-10 01:25:25 +03:00
|
|
|
),
|
|
|
|
new AboutListTile(
|
|
|
|
applicationName: "Hass Client",
|
|
|
|
applicationVersion: "0.1",
|
|
|
|
applicationLegalese: "Keyboard Crumbs",
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2018-09-16 00:06:07 +03:00
|
|
|
body: ListView(children: buildEntitiesView()),
|
2018-09-10 00:34:52 +03:00
|
|
|
floatingActionButton: new FloatingActionButton(
|
2018-09-15 01:46:15 +03:00
|
|
|
onPressed: _refreshData,
|
2018-09-10 00:34:52 +03:00
|
|
|
tooltip: 'Increment',
|
|
|
|
child: new Icon(Icons.refresh),
|
2018-09-15 01:46:15 +03:00
|
|
|
),
|
2018-09-10 00:34:52 +03:00
|
|
|
);
|
|
|
|
}
|
2018-09-12 00:32:04 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2018-09-15 01:46:15 +03:00
|
|
|
//TODO
|
|
|
|
//_hassioChannel.sink.close();
|
2018-09-12 00:32:04 +03:00
|
|
|
super.dispose();
|
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
}
|