mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
Refactor Javascript backend
This commit is contained in:
parent
ac8d70d547
commit
f9462613f5
@ -13,11 +13,52 @@
|
|||||||
<state-set-dialog id="stateDialog" api={{api}}></state-set-dialog>
|
<state-set-dialog id="stateDialog" api={{api}}></state-set-dialog>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
|
State = function(json, api) {
|
||||||
|
this.api = api;
|
||||||
|
|
||||||
|
this.attributes = json.attributes;
|
||||||
|
|
||||||
|
this.entity_id = json.entity_id;
|
||||||
|
var parts = json.entity_id.split(".");
|
||||||
|
this.domain = parts[0];
|
||||||
|
this.entity = parts[1];
|
||||||
|
|
||||||
|
if(this.attributes.friendly_name) {
|
||||||
|
this.entityDisplay = this.attributes.friendly_name;
|
||||||
|
} else {
|
||||||
|
this.entityDisplay = this.entity.replace(/_/g, " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.state = json.state;
|
||||||
|
this.last_changed = json.last_changed;
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.defineProperties(State.prototype, {
|
||||||
|
"stateDisplay": {
|
||||||
|
get: function() {
|
||||||
|
return this.state.replace(/_/g, " ");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
"isCustomGroup": {
|
||||||
|
get: function() {
|
||||||
|
return this.domain == "group" && !this.attributes.auto;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
"canToggle": {
|
||||||
|
get: function() {
|
||||||
|
return this.api.hasService(this.domain, 'turn_on');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Polymer({
|
Polymer({
|
||||||
auth: "not-set",
|
auth: "not-set",
|
||||||
states: [],
|
states: [],
|
||||||
services: {},
|
services: [],
|
||||||
events: {},
|
events: [],
|
||||||
stateUpdateTimeout: null,
|
stateUpdateTimeout: null,
|
||||||
|
|
||||||
computed: {
|
computed: {
|
||||||
@ -34,11 +75,19 @@
|
|||||||
|
|
||||||
// local methods
|
// local methods
|
||||||
getState: function(entityId) {
|
getState: function(entityId) {
|
||||||
for(var i = 0; i < this.states.length; i++) {
|
var found = this.states.filter(function(state) {
|
||||||
if(this.states[i].entity_id == entityId) {
|
return state.entity_id == entityId;
|
||||||
return this.states[i];
|
}, this);
|
||||||
}
|
|
||||||
}
|
return found.length > 0 ? found[0] : null;
|
||||||
|
},
|
||||||
|
|
||||||
|
hasService: function(domain, service) {
|
||||||
|
var found = this.services.filter(function(serv) {
|
||||||
|
return serv.domain == domain && serv.services.indexOf(service) !== -1;
|
||||||
|
}, this);
|
||||||
|
|
||||||
|
return found.length > 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
_laterFetchStates: function() {
|
_laterFetchStates: function() {
|
||||||
@ -79,30 +128,20 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!stateFound) {
|
if(!stateFound) {
|
||||||
this._enhanceState(state);
|
this.states.push(new State(new_state, this));
|
||||||
this.states.push(new_state);
|
|
||||||
this._sortStates(this.states);
|
this._sortStates(this.states);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.fire('states-updated')
|
this.fire('states-updated')
|
||||||
},
|
},
|
||||||
|
|
||||||
_enhanceState: function(state) {
|
// call api methods
|
||||||
var parts = state.entity_id.split(".");
|
fetchAll: function() {
|
||||||
state.domain = parts[0];
|
this.fetchStates();
|
||||||
state.entity = parts[1];
|
this.fetchServices();
|
||||||
state.stateDisplay = state.state.replace(/_/g, " ");
|
this.fetchEvents();
|
||||||
state.canToggle = state.state == "on" || state.state == "off";
|
|
||||||
state.isCustomGroup = state.domain == "group" && !state.attributes.auto;
|
|
||||||
|
|
||||||
if(state.attributes.friendly_name) {
|
|
||||||
state.entityDisplay = state.attributes.friendly_name;
|
|
||||||
} else {
|
|
||||||
state.entityDisplay = state.entity.replace(/_/g, " ");
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// call api methods
|
|
||||||
fetchState: function(entityId) {
|
fetchState: function(entityId) {
|
||||||
var successStateUpdate = function(new_state) {
|
var successStateUpdate = function(new_state) {
|
||||||
this._pushNewState(new_state);
|
this._pushNewState(new_state);
|
||||||
@ -114,40 +153,52 @@
|
|||||||
fetchStates: function(onSuccess, onError) {
|
fetchStates: function(onSuccess, onError) {
|
||||||
var successStatesUpdate = function(newStates) {
|
var successStatesUpdate = function(newStates) {
|
||||||
this._sortStates(newStates);
|
this._sortStates(newStates);
|
||||||
newStates.map(this._enhanceState);
|
|
||||||
this.states = newStates;
|
this.states = newStates.map(function(json) {
|
||||||
|
return new State(json, this);
|
||||||
|
}.bind(this));
|
||||||
|
|
||||||
this.fire('states-updated')
|
this.fire('states-updated')
|
||||||
|
|
||||||
this._laterFetchStates();
|
this._laterFetchStates();
|
||||||
|
|
||||||
if(onSuccess) {
|
if(onSuccess) {
|
||||||
onSuccess(newStates);
|
onSuccess(this.states);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.call_api("GET", "states", null, successStatesUpdate.bind(this), onError);
|
this.call_api(
|
||||||
|
"GET", "states", null, successStatesUpdate.bind(this), onError);
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchEvents: function() {
|
fetchEvents: function(onSuccess, onError) {
|
||||||
var successEventsUpdated = function(events) {
|
var successEventsUpdated = function(events) {
|
||||||
this.events = events;
|
this.events = this.events;
|
||||||
|
|
||||||
this.fire('events-updated')
|
this.fire('events-updated')
|
||||||
|
|
||||||
|
if(onSuccess) {
|
||||||
|
onSuccess(events);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.call_api("GET", "events", null, successEventsUpdated.bind(this));
|
this.call_api(
|
||||||
|
"GET", "events", null, successEventsUpdated.bind(this), onError);
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchServices: function() {
|
fetchServices: function(onSuccess, onError) {
|
||||||
var successServicesUpdated = function(services) {
|
var successServicesUpdated = function(services) {
|
||||||
this.services = services;
|
this.services = services;
|
||||||
|
|
||||||
this.fire('services-updated')
|
this.fire('services-updated')
|
||||||
|
|
||||||
|
if(onSuccess) {
|
||||||
|
onSuccess(this.services);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.call_api("GET", "services", null,
|
this.call_api(
|
||||||
successServicesUpdated.bind(this));
|
"GET", "services", null, successServicesUpdated.bind(this), onError);
|
||||||
},
|
},
|
||||||
|
|
||||||
turn_on: function(entity_id) {
|
turn_on: function(entity_id) {
|
||||||
|
@ -93,7 +93,7 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
handleRefreshClick: function() {
|
handleRefreshClick: function() {
|
||||||
this.api.fetchStates();
|
this.api.fetchAll();
|
||||||
},
|
},
|
||||||
|
|
||||||
handleEventClick: function() {
|
handleEventClick: function() {
|
||||||
|
@ -75,6 +75,7 @@
|
|||||||
// log out functionality
|
// log out functionality
|
||||||
if(newVal == "" && this.state == "valid_auth") {
|
if(newVal == "" && this.state == "valid_auth") {
|
||||||
this.state = "no_auth";
|
this.state = "no_auth";
|
||||||
|
this.$.validateMessage.innerHTML = "Validating password...";
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -90,10 +91,12 @@
|
|||||||
this.$.validateMessage.removeAttribute('hidden');
|
this.$.validateMessage.removeAttribute('hidden');
|
||||||
|
|
||||||
var passwordValid = function(result) {
|
var passwordValid = function(result) {
|
||||||
this.api.fetchServices();
|
this.$.validateMessage.innerHTML = "Loading data...";
|
||||||
this.api.fetchEvents();
|
this.api.fetchEvents();
|
||||||
|
|
||||||
this.state = "valid_auth";
|
this.api.fetchStates(function() {
|
||||||
|
this.state = "valid_auth";
|
||||||
|
}.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
var passwordInvalid = function(result) {
|
var passwordInvalid = function(result) {
|
||||||
@ -109,7 +112,7 @@
|
|||||||
this.$.passwordInput.focus();
|
this.$.passwordInput.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.api.fetchStates(passwordValid.bind(this), passwordInvalid.bind(this));
|
this.api.fetchServices(passwordValid.bind(this), passwordInvalid.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user