diff --git a/homeassistant/components/http/www_static/polymer/home-assistant-api.html b/homeassistant/components/http/www_static/polymer/home-assistant-api.html
index 6f10dbaa84c..3bccf223179 100644
--- a/homeassistant/components/http/www_static/polymer/home-assistant-api.html
+++ b/homeassistant/components/http/www_static/polymer/home-assistant-api.html
@@ -139,6 +139,12 @@
this.fire('states-updated');
},
+ _pushNewStates: function(new_states) {
+ new_states.map(function(state) {
+ this._pushNewState(state);
+ }.bind(this));
+ },
+
// call api methods
fetchAll: function() {
this.fetchStates();
@@ -205,19 +211,14 @@
"GET", "services", null, successServicesUpdated.bind(this), onError);
},
- turn_on: function(entity_id) {
- // we call the turn_on method on the domain of the entity_id
- // because the call to homeassistant.turn_on does not wait
- // till the call is done.
- var parts = entity_id.split(".");
-
- this.call_service(parts[0], "turn_on", {entity_id: entity_id});
+ turn_on: function(entity_id, options) {
+ this.call_service(
+ "homeassistant", "turn_on", {entity_id: entity_id}, options);
},
- turn_off: function(entity_id) {
- var parts = entity_id.split(".");
-
- this.call_service(parts[0], "turn_off", {entity_id: entity_id});
+ turn_off: function(entity_id, options) {
+ this.call_service(
+ "homeassistant", "turn_off", {entity_id: entity_id}, options);
},
set_state: function(entity_id, state, attributes) {
@@ -236,10 +237,11 @@
payload, successToast.bind(this));
},
- call_service: function(domain, service, parameters) {
+ call_service: function(domain, service, parameters, options) {
parameters = parameters || {};
+ options = options || {};
- var successToast = function() {
+ var successHandler = function(changed_states) {
if(service == "turn_on" && parameters.entity_id) {
this.showToast("Turned on " + parameters.entity_id + '.');
} else if(service == "turn_off" && parameters.entity_id) {
@@ -248,14 +250,21 @@
this.showToast("Service "+domain+"/"+service+" called.");
}
- // if we call a service on an entity_id, update the state
- if(parameters && parameters.entity_id) {
- this.fetchStates();
+ this._pushNewStates(changed_states);
+
+ if(options.success) {
+ options.success();
+ }
+ };
+
+ var errorHandler = function(error_data) {
+ if(options.error) {
+ options.error(error_data);
}
};
this.call_api("POST", "services/" + domain + "/" + service,
- parameters, successToast.bind(this));
+ parameters, successHandler.bind(this), errorHandler);
},
fire_event: function(eventType, eventData) {
diff --git a/homeassistant/components/http/www_static/polymer/state-card.html b/homeassistant/components/http/www_static/polymer/state-card.html
index c7656e3787a..8b0be4dc076 100755
--- a/homeassistant/components/http/www_static/polymer/state-card.html
+++ b/homeassistant/components/http/www_static/polymer/state-card.html
@@ -139,31 +139,34 @@
},
stateChanged: function(oldVal, newVal) {
- this.stateUnknown = newVal === null;
this.toggleChecked = newVal === "on";
},
turn_on: function() {
+ // We call stateChanged after a successful call to re-sync the toggle
+ // with the state. It will be out of sync if our service call did not
+ // result in the entity to be turned on. Since the state is not changing,
+ // the resync is not called automatic.
if(this.cb_turn_on) {
- this.cb_turn_on(this.stateObj.entity_id);
-
- // unset state while we wait for an update
- var delayUnsetSate = function() {
- this.stateObj.state = null;
- };
- setTimeout(delayUnsetSate.bind(this), 500);
+ this.cb_turn_on(this.stateObj.entity_id, {
+ success: function() {
+ this.stateChanged(this.stateObj.state, this.stateObj.state);
+ }.bind(this)
+ });
}
},
turn_off: function() {
+ // We call stateChanged after a successful call to re-sync the toggle
+ // with the state. It will be out of sync if our service call did not
+ // result in the entity to be turned on. Since the state is not changing,
+ // the resync is not called automatic.
if(this.cb_turn_off) {
- this.cb_turn_off(this.stateObj.entity_id);
-
- // unset state while we wait for an update
- var delayUnsetSate = function() {
- this.stateObj.state = null;
- };
- setTimeout(delayUnsetSate.bind(this), 500);
+ this.cb_turn_off(this.stateObj.entity_id, {
+ success: function() {
+ this.stateChanged(this.stateObj.state, this.stateObj.state);
+ }.bind(this)
+ });
}
},