Skip to content

Commit

Permalink
Merge pull request #39 from cgiesche/streamdeck-homeassistant-38
Browse files Browse the repository at this point in the history
Fixed bug that prevented users to save settings after clean install. …
  • Loading branch information
cgiesche committed May 4, 2021
2 parents 938ba0c + c411283 commit da4fe2c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 42 deletions.
82 changes: 41 additions & 41 deletions src/Pi.vue
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ export default {
computed: {
serverUrlState: function () {
return this.serverUrl.length > 4
return this.serverUrl && this.serverUrl.length > 4
},
accessTokenState: function () {
return this.accessToken.length > 4
return this.accessToken && this.accessToken.length > 4
},
serviceDataFeedback: function () {
Expand Down Expand Up @@ -333,47 +333,47 @@ export default {
this.$HA.close();
}
this.$HA = new Homeassistant(this.serverUrl, this.accessToken, () => {
this.haConnected = true;
this.$HA.getStates((states) => {
this.availableDomains = Array.from(states
.map(state => new Entity(state.entity_id).domain)
.sort()
.reduce(
(acc, curr) => acc.add(curr), new Set()
));
this.availableEntities = states
.map((state) => {
return {
value: new Entity(state.entity_id),
text: state.attributes.friendly_name || state.entity_id
try {
this.$HA = new Homeassistant(this.serverUrl, this.accessToken, () => {
this.haConnected = true;
this.$HA.getStates((states) => {
this.availableDomains = Array.from(states
.map(state => new Entity(state.entity_id).domain)
.sort()
.reduce(
(acc, curr) => acc.add(curr), new Set()
));
this.availableEntities = states
.map((state) => {
return {
value: new Entity(state.entity_id),
text: state.attributes.friendly_name || state.entity_id
}
}
)
.sort((a, b) => (a.text > b.text) ? 1 : ((b.text > a.text) ? -1 : 0))
this.availableAttributes = states
.map((state) => {
return {
entity: new Entity(state.entity_id),
attributes: ObjectUtils.paths(state.attributes)
}
)
.sort((a, b) => (a.text > b.text) ? 1 : ((b.text > a.text) ? -1 : 0))
this.availableAttributes = states
.map((state) => {
return {
entity: new Entity(state.entity_id),
attributes: ObjectUtils.paths(state.attributes)
}
})
});
this.$HA.getServices((services) => {
this.availableServices = services;
});
},
() => {
this.haConnected = false;
this.haError = "Failed to connect websocket.";
},
() => {
this.haConnected = false;
this.haError = "Websocket was closed.";
}
)
})
});
this.$HA.getServices((services) => {
this.availableServices = services;
});
},
(message) => {
this.haConnected = false;
this.haError = message;
}
)
} catch (e) {
this.haError = e
}
},
saveGlobalSettings: function () {
Expand Down
11 changes: 10 additions & 1 deletion src/modules/common/homeassistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export class Homeassistant {
this.websocket = new WebSocket(url)
this.accessToken = accessToken;
this.onReady = onReady;
this.onError = onError;

this.websocket.onmessage = (evt) => this.handleMessage(evt);
this.websocket.onerror = onError;
this.websocket.onerror = () => { this.onError("Failed to connect to " + url) };
this.websocket.onclose = onClose;
}

Expand Down Expand Up @@ -42,6 +43,14 @@ export class Homeassistant {
}
break;
case "auth_failed":
if (this.onError) {
this.onError(messageData.message);
}
break;
case "auth_invalid":
if (this.onError) {
this.onError(messageData.message);
}
break;
}
}
Expand Down

0 comments on commit da4fe2c

Please sign in to comment.