Filter our entities from services. Use Map for entities list.
This commit is contained in:
parent
b2bb5f78f6
commit
3b6d4e468d
@ -46,7 +46,7 @@ class MainPage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MainPageState extends State<MainPage> {
|
class _MainPageState extends State<MainPage> {
|
||||||
List _entitiesData = [];
|
Map _entitiesData = {};
|
||||||
Map _servicesData = {};
|
Map _servicesData = {};
|
||||||
String _hassioAPIEndpoint = "";
|
String _hassioAPIEndpoint = "";
|
||||||
String _hassioPassword = "";
|
String _hassioPassword = "";
|
||||||
@ -71,6 +71,7 @@ class _MainPageState extends State<MainPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_connectSocket() async {
|
_connectSocket() async {
|
||||||
|
debugPrint("Socket connecting...");
|
||||||
_hassioChannel = await IOWebSocketChannel.connect(_hassioAPIEndpoint);
|
_hassioChannel = await IOWebSocketChannel.connect(_hassioAPIEndpoint);
|
||||||
_hassioChannel.stream.listen((message) {
|
_hassioChannel.stream.listen((message) {
|
||||||
_handleSocketMessage(message);
|
_handleSocketMessage(message);
|
||||||
@ -120,6 +121,9 @@ class _MainPageState extends State<MainPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_sendRawMessage(message) {
|
_sendRawMessage(message) {
|
||||||
|
if ((_hassioChannel == null) || (_hassioChannel.closeCode != null)) {
|
||||||
|
debugPrint("Socket is closed");
|
||||||
|
}
|
||||||
debugPrint("==> Sending to Home Assistant:");
|
debugPrint("==> Sending to Home Assistant:");
|
||||||
debugPrint(message);
|
debugPrint(message);
|
||||||
_hassioChannel.sink.add(message);
|
_hassioChannel.sink.add(message);
|
||||||
@ -131,8 +135,17 @@ class _MainPageState extends State<MainPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _loadServices(Map data) {
|
void _loadServices(Map data) {
|
||||||
|
Map result = {};
|
||||||
|
data.forEach((domain, services){
|
||||||
|
result[domain] = Map.from(services);
|
||||||
|
services.forEach((serviceName, serviceData){
|
||||||
|
if (_entitiesData["$domain.$serviceName"] != null) {
|
||||||
|
result[domain].remove(serviceName);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
setState(() {
|
setState(() {
|
||||||
_servicesData = Map.from(data);
|
_servicesData = result;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,8 +158,10 @@ class _MainPageState extends State<MainPage> {
|
|||||||
debugPrint("Getting Home Assistant entities: ${data.length}");
|
debugPrint("Getting Home Assistant entities: ${data.length}");
|
||||||
data.forEach((entity) {
|
data.forEach((entity) {
|
||||||
var composedEntity = Map.from(entity);
|
var composedEntity = Map.from(entity);
|
||||||
composedEntity["display_name"] = "${entity["attributes"]!=null ? entity["attributes"]["friendly_name"] ?? entity["attributes"]["name"] : "_"}";
|
|
||||||
String entityDomain = entity["entity_id"].split(".")[0];
|
String entityDomain = entity["entity_id"].split(".")[0];
|
||||||
|
String entityId = entity["entity_id"];
|
||||||
|
|
||||||
|
composedEntity["display_name"] = "${entity["attributes"]!=null ? entity["attributes"]["friendly_name"] ?? entity["attributes"]["name"] : "_"}";
|
||||||
composedEntity["domain"] = entityDomain;
|
composedEntity["domain"] = entityDomain;
|
||||||
|
|
||||||
if ((entityDomain == "automation") || (entityDomain == "light") || (entityDomain == "switch") || (entityDomain == "script")) {
|
if ((entityDomain == "automation") || (entityDomain == "light") || (entityDomain == "switch") || (entityDomain == "script")) {
|
||||||
@ -154,21 +169,21 @@ class _MainPageState extends State<MainPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_entitiesData.add(composedEntity);
|
_entitiesData[entityId] = Map.from(composedEntity);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget buildEntityButtons(int i) {
|
Widget buildEntityButtons(String entityId) {
|
||||||
if (_entitiesData[i]["services"] == null || _entitiesData[i]["services"].length == 0) {
|
if (_entitiesData[entityId]["services"] == null || _entitiesData[entityId]["services"].length == 0) {
|
||||||
return new Container(width: 0.0, height: 0.0);
|
return new Container(width: 0.0, height: 0.0);
|
||||||
}
|
}
|
||||||
List<Widget> buttons = [];
|
List<Widget> buttons = [];
|
||||||
_entitiesData[i]["services"].forEach((key, value) {
|
_entitiesData[entityId]["services"].forEach((key, value) {
|
||||||
buttons.add(new FlatButton(
|
buttons.add(new FlatButton(
|
||||||
child: Text(_entitiesData[i]["domain"] + ".$key"),
|
child: Text(_entitiesData[entityId]["domain"] + ".$key"),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
_sendServiceCall(_entitiesData[i]["domain"], key, _entitiesData[i]["entity_id"]);
|
_sendServiceCall(_entitiesData[entityId]["domain"], key, _entitiesData[entityId]["entity_id"]);
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
@ -177,30 +192,31 @@ class _MainPageState extends State<MainPage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget buildEntityCard(int i) {
|
Widget buildEntityCard(String entityId) {
|
||||||
return Card(
|
return Card(
|
||||||
child: new Column(
|
child: new Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
new ListTile(
|
new ListTile(
|
||||||
leading: const Icon(Icons.device_hub),
|
leading: const Icon(Icons.device_hub),
|
||||||
subtitle: Text("${_entitiesData[i]["entity_id"]}"),
|
subtitle: Text("$entityId"),
|
||||||
trailing: Text("${_entitiesData[i]["state"]}"),
|
trailing: Text("${_entitiesData[entityId]["state"]}"),
|
||||||
title: Text("${_entitiesData[i]["display_name"]}"),
|
title: Text("${_entitiesData[entityId]["display_name"]}"),
|
||||||
),
|
),
|
||||||
new ButtonTheme.bar( // make buttons use the appropriate styles for cards
|
new ButtonTheme.bar( // make buttons use the appropriate styles for cards
|
||||||
child: buildEntityButtons(i),
|
child: buildEntityButtons(entityId),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Widget> buildEntitiesView() {
|
||||||
|
List<Widget> result = [];
|
||||||
return Padding(
|
_entitiesData.forEach((key, data){
|
||||||
padding: EdgeInsets.all(10.0),
|
result.add(buildEntityCard(key));
|
||||||
child: Text("Row ${_entitiesData[i]["entity_id"]}")
|
});
|
||||||
);
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -242,11 +258,9 @@ class _MainPageState extends State<MainPage> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
body: ListView.builder(
|
body: ListView(
|
||||||
itemCount: _entitiesData.length,
|
children: buildEntitiesView(),
|
||||||
itemBuilder: (BuildContext context, int position) {
|
),
|
||||||
return buildEntityCard(position);
|
|
||||||
}),
|
|
||||||
floatingActionButton: new FloatingActionButton(
|
floatingActionButton: new FloatingActionButton(
|
||||||
onPressed: _startDataFetching,
|
onPressed: _startDataFetching,
|
||||||
tooltip: 'Increment',
|
tooltip: 'Increment',
|
||||||
|
Reference in New Issue
Block a user