This commit is contained in:
estevez-dev 2019-03-21 16:55:25 +02:00
parent 5ebd25e0d1
commit d29d7e5b3b

View File

@ -14,7 +14,6 @@ class HomeAssistant {
SendMessageQueue _messageQueue; SendMessageQueue _messageQueue;
int _currentMessageId = 0; int _currentMessageId = 0;
int _subscriptionMessageId = 0;
Map<int, Completer> _messageResolver = {}; Map<int, Completer> _messageResolver = {};
EntityCollection entities; EntityCollection entities;
HomeAssistantUI ui; HomeAssistantUI ui;
@ -200,13 +199,16 @@ class HomeAssistant {
futures.add(_getServices()); futures.add(_getServices());
futures.add(_getUserInfo()); futures.add(_getUserInfo());
futures.add(_getPanels()); futures.add(_getPanels());
try { futures.add(
await Future.wait(futures); _sendSocketMessage(
type: "subscribe_events",
additionalData: {"event_type": "state_changed"},
)
);
await Future.wait(futures).then((_) {
_createUI(); _createUI();
_completeFetching(null); _completeFetching(null);
} catch (error) { }).catchError((e) => _completeFetching(e));
_completeFetching(error);
}
} }
void _completeFetching(error) { void _completeFetching(error) {
@ -249,14 +251,17 @@ class HomeAssistant {
} else if (data["type"] == "auth_ok") { } else if (data["type"] == "auth_ok") {
Logger.d("[Received] <== ${data.toString()}"); Logger.d("[Received] <== ${data.toString()}");
_completeConnecting(null); _completeConnecting(null);
_sendSubscribe();
} else if (data["type"] == "auth_invalid") { } else if (data["type"] == "auth_invalid") {
Logger.d("[Received] <== ${data.toString()}"); Logger.d("[Received] <== ${data.toString()}");
logout(); logout();
_completeConnecting({"errorCode": 62, "errorMessage": "${data["message"]}"}); _completeConnecting({"errorCode": 62, "errorMessage": "${data["message"]}"});
} else if (data["type"] == "result") { } else if (data["type"] == "result") {
Logger.d("[Received] <== id: ${data['id']}, success: ${data['success']}"); Logger.d("[Received] <== id: ${data['id']}, success: ${data['success']}");
_messageResolver[data["id"]]?.complete(data); if (data["success"]) {
_messageResolver[data["id"]]?.complete(data["result"]);
} else {
_messageResolver[data["id"]]?.completeError(data["error"]);
}
_messageResolver.remove(data["id"]); _messageResolver.remove(data["id"]);
} else if (data["type"] == "event") { } else if (data["type"] == "event") {
if ((data["event"] != null) && (data["event"]["event_type"] == "state_changed")) { if ((data["event"] != null) && (data["event"]["event_type"] == "state_changed")) {
@ -282,18 +287,19 @@ class HomeAssistant {
entities?.clear(); entities?.clear();
} }
void _sendSubscribe() {
_incrementMessageId();
_subscriptionMessageId = _currentMessageId;
_rawSend('{"id": $_subscriptionMessageId, "type": "subscribe_events", "event_type": "state_changed"}', false);
}
Future _getConfig() async { Future _getConfig() async {
await _sendSocketMessage(type: "get_config").then((data) => _instanceConfig = Map.from(data["result"])); await _sendSocketMessage(type: "get_config").then((data) {
if (data["success"]) {
_instanceConfig = Map.from(data["result"]);
} else {
Logger.w("Couldn't get instance config: ${data["error"].toString()}");
}
});
} }
Future _getStates() async { Future _getStates() async {
await _sendSocketMessage(type: "get_states").then((data) => entities.parse(data["result"])); //TODO last change here. Try run!!
return _sendSocketMessage(type: "get_states").then((data) => entities.parse(data));
} }
Future _getLongLivedToken() async { Future _getLongLivedToken() async {