Chachesd HomeAssistance instance for every view in app
This commit is contained in:
parent
0efef33e53
commit
5ae580ecf1
@ -69,12 +69,12 @@ class HomeAssistant {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateSettings(String url, String password, bool useLovelace) {
|
/*void updateSettings(String url, String password, bool useLovelace) {
|
||||||
_webSocketAPIEndpoint = url;
|
_webSocketAPIEndpoint = url;
|
||||||
_password = password;
|
_password = password;
|
||||||
_useLovelace = useLovelace;
|
_useLovelace = useLovelace;
|
||||||
Logger.d( "Use lovelace is $_useLovelace");
|
Logger.d( "Use lovelace is $_useLovelace");
|
||||||
}
|
}*/
|
||||||
|
|
||||||
Future fetch() {
|
Future fetch() {
|
||||||
if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) {
|
if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) {
|
||||||
|
@ -126,6 +126,8 @@ void main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class HAClientApp extends StatelessWidget {
|
class HAClientApp extends StatelessWidget {
|
||||||
|
|
||||||
|
final HomeAssistant homeAssistant = HomeAssistant();
|
||||||
// This widget is the root of your application.
|
// This widget is the root of your application.
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -136,7 +138,7 @@ class HAClientApp extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
initialRoute: "/",
|
initialRoute: "/",
|
||||||
routes: {
|
routes: {
|
||||||
"/": (context) => MainPage(title: 'HA Client'),
|
"/": (context) => MainPage(title: 'HA Client', homeAssistant: homeAssistant,),
|
||||||
"/connection-settings": (context) => ConnectionSettingsPage(title: "Settings"),
|
"/connection-settings": (context) => ConnectionSettingsPage(title: "Settings"),
|
||||||
"/configuration": (context) => PanelPage(title: "Configuration"),
|
"/configuration": (context) => PanelPage(title: "Configuration"),
|
||||||
"/log-view": (context) => LogViewPage(title: "Log")
|
"/log-view": (context) => LogViewPage(title: "Log")
|
||||||
@ -146,16 +148,17 @@ class HAClientApp extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MainPage extends StatefulWidget {
|
class MainPage extends StatefulWidget {
|
||||||
MainPage({Key key, this.title}) : super(key: key);
|
MainPage({Key key, this.title, this.homeAssistant}) : super(key: key);
|
||||||
|
|
||||||
final String title;
|
final String title;
|
||||||
|
final HomeAssistant homeAssistant;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_MainPageState createState() => new _MainPageState();
|
_MainPageState createState() => new _MainPageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _MainPageState extends State<MainPage> with WidgetsBindingObserver, TickerProviderStateMixin {
|
class _MainPageState extends State<MainPage> with WidgetsBindingObserver, TickerProviderStateMixin {
|
||||||
HomeAssistant _homeAssistant;
|
//HomeAssistant _homeAssistant;
|
||||||
//Map _instanceConfig;
|
//Map _instanceConfig;
|
||||||
//String _webSocketApiEndpoint;
|
//String _webSocketApiEndpoint;
|
||||||
//String _password;
|
//String _password;
|
||||||
@ -175,15 +178,14 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
Logger.d("<!!!> Creating new HomeAssistant instance");
|
//widget.homeAssistant = HomeAssistant();
|
||||||
_homeAssistant = HomeAssistant();
|
|
||||||
//_settingsLoaded = false;
|
//_settingsLoaded = false;
|
||||||
WidgetsBinding.instance.addObserver(this);
|
WidgetsBinding.instance.addObserver(this);
|
||||||
|
|
||||||
_settingsSubscription = eventBus.on<SettingsChangedEvent>().listen((event) {
|
_settingsSubscription = eventBus.on<SettingsChangedEvent>().listen((event) {
|
||||||
Logger.d("Settings change event: reconnect=${event.reconnect}");
|
Logger.d("Settings change event: reconnect=${event.reconnect}");
|
||||||
if (event.reconnect) {
|
if (event.reconnect) {
|
||||||
_homeAssistant.disconnect().then((_){
|
widget.homeAssistant.disconnect().then((_){
|
||||||
_initialLoad();
|
_initialLoad();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -193,7 +195,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _initialLoad() {
|
void _initialLoad() {
|
||||||
_homeAssistant.loadConnectionSettings().then((_){
|
widget.homeAssistant.loadConnectionSettings().then((_){
|
||||||
_subscribe();
|
_subscribe();
|
||||||
_refreshData();
|
_refreshData();
|
||||||
}, onError: (_) {
|
}, onError: (_) {
|
||||||
@ -204,7 +206,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
@override
|
@override
|
||||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||||
//Logger.d("$state");
|
//Logger.d("$state");
|
||||||
if (state == AppLifecycleState.resumed && _homeAssistant.isSettingsLoaded) {
|
if (state == AppLifecycleState.resumed && widget.homeAssistant.isSettingsLoaded) {
|
||||||
_refreshData();
|
_refreshData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -260,7 +262,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
|
|
||||||
_firebaseMessaging.getToken().then((String token) {
|
_firebaseMessaging.getToken().then((String token) {
|
||||||
//Logger.d("FCM token: $token");
|
//Logger.d("FCM token: $token");
|
||||||
_homeAssistant.sendHTTPRequest('{"token": "$token"}');
|
widget.homeAssistant.sendHTTPRequest('{"token": "$token"}');
|
||||||
});
|
});
|
||||||
_firebaseMessaging.configure(
|
_firebaseMessaging.configure(
|
||||||
onLaunch: (data) {
|
onLaunch: (data) {
|
||||||
@ -276,12 +278,12 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
}
|
}
|
||||||
|
|
||||||
_refreshData() async {
|
_refreshData() async {
|
||||||
//_homeAssistant.updateSettings(_webSocketApiEndpoint, _password, _useLovelaceUI);
|
//widget.homeAssistant.updateSettings(_webSocketApiEndpoint, _password, _useLovelaceUI);
|
||||||
_hideBottomBar();
|
_hideBottomBar();
|
||||||
_showInfoBottomBar(progress: true,);
|
_showInfoBottomBar(progress: true,);
|
||||||
await _homeAssistant.fetch().then((result) {
|
await widget.homeAssistant.fetch().then((result) {
|
||||||
_hideBottomBar();
|
_hideBottomBar();
|
||||||
int currentViewCount = _homeAssistant.ui?.views?.length ?? 0;
|
int currentViewCount = widget.homeAssistant.ui?.views?.length ?? 0;
|
||||||
if (_previousViewCount != currentViewCount) {
|
if (_previousViewCount != currentViewCount) {
|
||||||
Logger.d("Views count changed ($_previousViewCount->$currentViewCount). Creating new tabs controller.");
|
Logger.d("Views count changed ($_previousViewCount->$currentViewCount). Creating new tabs controller.");
|
||||||
_viewsTabController = TabController(vsync: this, length: currentViewCount);
|
_viewsTabController = TabController(vsync: this, length: currentViewCount);
|
||||||
@ -314,14 +316,14 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
message: "Calling $domain.$service",
|
message: "Calling $domain.$service",
|
||||||
duration: Duration(seconds: 3)
|
duration: Duration(seconds: 3)
|
||||||
);
|
);
|
||||||
_homeAssistant.callService(domain, service, entityId, additionalParams).catchError((e) => _setErrorState(e));
|
widget.homeAssistant.callService(domain, service, entityId, additionalParams).catchError((e) => _setErrorState(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _showEntityPage(String entityId) {
|
void _showEntityPage(String entityId) {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (context) => EntityViewPage(entityId: entityId, homeAssistant: _homeAssistant),
|
builder: (context) => EntityViewPage(entityId: entityId, homeAssistant: widget.homeAssistant),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -329,8 +331,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
List<Tab> buildUIViewTabs() {
|
List<Tab> buildUIViewTabs() {
|
||||||
List<Tab> result = [];
|
List<Tab> result = [];
|
||||||
|
|
||||||
if (_homeAssistant.ui.views.isNotEmpty) {
|
if (widget.homeAssistant.ui.views.isNotEmpty) {
|
||||||
_homeAssistant.ui.views.forEach((HAView view) {
|
widget.homeAssistant.ui.views.forEach((HAView view) {
|
||||||
result.add(view.buildTab());
|
result.add(view.buildTab());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -342,8 +344,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
List<Widget> menuItems = [];
|
List<Widget> menuItems = [];
|
||||||
menuItems.add(
|
menuItems.add(
|
||||||
UserAccountsDrawerHeader(
|
UserAccountsDrawerHeader(
|
||||||
accountName: Text(_homeAssistant.userName),
|
accountName: Text(widget.homeAssistant.userName),
|
||||||
accountEmail: Text(_homeAssistant.hostname ?? "Not configured"),
|
accountEmail: Text(widget.homeAssistant.hostname ?? "Not configured"),
|
||||||
onDetailsPressed: () {
|
onDetailsPressed: () {
|
||||||
setState(() {
|
setState(() {
|
||||||
_accountMenuExpanded = !_accountMenuExpanded;
|
_accountMenuExpanded = !_accountMenuExpanded;
|
||||||
@ -351,7 +353,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
},
|
},
|
||||||
currentAccountPicture: CircleAvatar(
|
currentAccountPicture: CircleAvatar(
|
||||||
child: Text(
|
child: Text(
|
||||||
_homeAssistant.userAvatarText,
|
widget.homeAssistant.userAvatarText,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
fontSize: 32.0
|
fontSize: 32.0
|
||||||
),
|
),
|
||||||
@ -366,14 +368,14 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
title: Text("Settings"),
|
title: Text("Settings"),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
Navigator.of(context).pushNamed('/connection-settings');
|
Navigator.of(context).pushNamed('/connection-settings', arguments: {"homeAssistant", widget.homeAssistant});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
Divider(),
|
Divider(),
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
if (_homeAssistant != null && _homeAssistant.panels.isNotEmpty) {
|
if (widget.homeAssistant != null && widget.homeAssistant.panels.isNotEmpty) {
|
||||||
_homeAssistant.panels.forEach((Panel panel) {
|
widget.homeAssistant.panels.forEach((Panel panel) {
|
||||||
if (!panel.isHidden) {
|
if (!panel.isHidden) {
|
||||||
menuItems.add(
|
menuItems.add(
|
||||||
new ListTile(
|
new ListTile(
|
||||||
@ -388,7 +390,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
new ListTile(
|
new ListTile(
|
||||||
leading: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:home-assistant")),
|
leading: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:home-assistant")),
|
||||||
title: Text("Open Web UI"),
|
title: Text("Open Web UI"),
|
||||||
onTap: () => HAUtils.launchURL(_homeAssistant.httpAPIEndpoint),
|
onTap: () => HAUtils.launchURL(widget.homeAssistant.httpAPIEndpoint),
|
||||||
),
|
),
|
||||||
Divider()
|
Divider()
|
||||||
]);
|
]);
|
||||||
@ -579,7 +581,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
floating: true,
|
floating: true,
|
||||||
pinned: true,
|
pinned: true,
|
||||||
primary: true,
|
primary: true,
|
||||||
title: Text(_homeAssistant != null ? _homeAssistant.locationName : ""),
|
title: Text(widget.homeAssistant != null ? widget.homeAssistant.locationName : ""),
|
||||||
actions: <Widget>[
|
actions: <Widget>[
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: Icon(MaterialDesignIcons.getIconDataFromIconName(
|
icon: Icon(MaterialDesignIcons.getIconDataFromIconName(
|
||||||
@ -632,7 +634,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
:
|
:
|
||||||
_homeAssistant.buildViews(context, _viewsTabController),
|
widget.homeAssistant.buildViews(context, _viewsTabController),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -689,7 +691,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// This method is rerun every time setState is called.
|
// This method is rerun every time setState is called.
|
||||||
if (_homeAssistant.ui == null || _homeAssistant.ui.views == null) {
|
if (widget.homeAssistant.ui == null || widget.homeAssistant.ui.views == null) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
key: _scaffoldKey,
|
key: _scaffoldKey,
|
||||||
primary: false,
|
primary: false,
|
||||||
@ -705,7 +707,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
bottomNavigationBar: bottomBar,
|
bottomNavigationBar: bottomBar,
|
||||||
body: HomeAssistantModel(
|
body: HomeAssistantModel(
|
||||||
child: _buildScaffoldBody(false),
|
child: _buildScaffoldBody(false),
|
||||||
homeAssistant: _homeAssistant
|
homeAssistant: widget.homeAssistant
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -720,7 +722,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
if (_serviceCallSubscription != null) _serviceCallSubscription.cancel();
|
if (_serviceCallSubscription != null) _serviceCallSubscription.cancel();
|
||||||
if (_showEntityPageSubscription != null) _showEntityPageSubscription.cancel();
|
if (_showEntityPageSubscription != null) _showEntityPageSubscription.cancel();
|
||||||
if (_showErrorSubscription != null) _showErrorSubscription.cancel();
|
if (_showErrorSubscription != null) _showErrorSubscription.cancel();
|
||||||
_homeAssistant.disconnect();
|
widget.homeAssistant.disconnect();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
part of 'main.dart';
|
part of 'main.dart';
|
||||||
|
|
||||||
class ConnectionSettingsPage extends StatefulWidget {
|
class ConnectionSettingsPage extends StatefulWidget {
|
||||||
ConnectionSettingsPage({Key key, this.title}) : super(key: key);
|
ConnectionSettingsPage({Key key, this.title, this.homeAssistant}) : super(key: key);
|
||||||
|
|
||||||
final String title;
|
final String title;
|
||||||
|
final HomeAssistant homeAssistant;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_ConnectionSettingsPageState createState() => new _ConnectionSettingsPageState();
|
_ConnectionSettingsPageState createState() => new _ConnectionSettingsPageState();
|
||||||
|
Reference in New Issue
Block a user