From 19c85d9c16e69dec028e79e6d890b12d35ee37b0 Mon Sep 17 00:00:00 2001 From: estevez-dev Date: Fri, 19 Apr 2019 14:38:02 +0300 Subject: [PATCH] Don't handle state change if fetch is in progress --- lib/home_assistant.class.dart | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index 0140225..3c5acf2 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -25,6 +25,7 @@ class HomeAssistant { String get userAvatarText => userName.length > 0 ? userName[0] : ""; bool get isNoEntities => entities == null || entities.isEmpty; bool get isNoViews => ui == null || ui.isEmpty; + bool get isMobileAppEnabled => _instanceConfig["components"] != null && (_instanceConfig["components"] as List).contains("mobile_app"); HomeAssistant() { Connection().onStateChangeCallback = _handleEntityStateChange; @@ -48,12 +49,12 @@ class HomeAssistant { futures.add(_getServices()); futures.add(_getUserInfo()); futures.add(_getPanels()); + futures.add(Connection().sendSocketMessage( + type: "subscribe_events", + additionalData: {"event_type": "state_changed"}, + )); Future.wait(futures).then((_) { _createUI(); - Connection().sendSocketMessage( - type: "subscribe_events", - additionalData: {"event_type": "state_changed"}, - ); _fetchCompleter.complete(); }).catchError((e) { _fetchCompleter.completeError(e); @@ -127,11 +128,13 @@ class HomeAssistant { void _handleEntityStateChange(Map eventData) { //TheLogger.debug( "New state for ${eventData['entity_id']}"); - Map data = Map.from(eventData); - eventBus.fire(new StateChangedEvent( - entityId: data["entity_id"], - needToRebuildUI: entities.updateState(data) - )); + if (_fetchCompleter == null || _fetchCompleter.isCompleted) { + Map data = Map.from(eventData); + eventBus.fire(new StateChangedEvent( + entityId: data["entity_id"], + needToRebuildUI: entities.updateState(data) + )); + } } void _parseLovelace() {