Skip to content

Commit

Permalink
Metric weather units and sonos testing
Browse files Browse the repository at this point in the history
  • Loading branch information
EricAndrechek committed Feb 5, 2021
1 parent 7925efb commit ea77106
Show file tree
Hide file tree
Showing 15 changed files with 573 additions and 765 deletions.
40 changes: 12 additions & 28 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
}
},
"Spotify": {
"title": "Create a Spotify API Key to use the Spotify player functionality. ** Note: This only works with Spotify Premium Accounts **",
"title": "Spotify",
"expandable": true,
"type": "object",
"properties": {
Expand All @@ -173,33 +173,8 @@
}
}
},
"Sonos": {
"title": "Create a Sonos API Key to use the Sonos player functionality.",
"expandable": true,
"type": "object",
"properties": {
"cid": {
"title": "Client ID",
"type": "string",
"default": "",
"required": false
},
"cs": {
"title": "Client Secret",
"type": "string",
"default": "",
"required": false
},
"rurl": {
"title": "Callback URL for Sonos Login",
"type": "string",
"placeholder": "http://homebridge.local:6969/setup",
"required": false
}
}
},
"Newsfeed": {
"title": "Add links to RSS News Feeds below",
"title": "RSS News Feeds",
"expandable": true,
"type": "object",
"properties": {
Expand All @@ -220,10 +195,19 @@
}
},
"Weather": {
"title": "Show the current weather on your display. Register for an API Key with OpenWeather and follow the instructions on my GitHub",
"title": "Weather",
"expandable": true,
"type": "object",
"properties": {
"units": {
"title": "Units",
"type": "string",
"default": "F",
"oneOf": [
{ "title": "Celsius", "enum": ["C"] },
{ "title": "Fahrenheit", "enum": ["F"] }
]
},
"api_key": {
"title": "API Key",
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
window.onresize();
});

var socket = io();
var socket = io({ transports: ["websocket"] });

socket.on("connect", function () {
socket.emit("update");
Expand Down
72 changes: 24 additions & 48 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,57 +155,24 @@ class HomebridgeDisplay {
error_trigger = true;
}
} else if (boxtype[i] === "sonos") {
const sono = require("./sonos");
let sonos_settings = this.config.Sonos || false;
if (sonos_settings !== false) {
let cid = sonos_settings.cid || undefined;
let cs = sonos_settings.cs || undefined;
let refresh = plugin_storage.refresh || undefined;
let rurl = sonos_settings.rurl || undefined;
if (
cid === undefined ||
cs === undefined ||
rurl === undefined
) {
this.log.error(
"Sonos is not done being set up, got to homebridge-display's settings to add it."
);
error_trigger = true;
} else {
let auth_url =
"https://api.sonos.com/login/v3/oauth?client_id=" +
cid +
"&response_type=code&state=login&scope=playback-control-all&redirect_uri=" +
encodeURIComponent(rurl);
this.sonos_obj = new sono(
cid,
cs,
refresh,
auth_url,
rurl,
this.log,
this.config,
this.api
);
this.box[i] = this.sonos_obj;
}
} else {
this.log.error(
"Sonos not set up, go to homebridge-display's settings to add it."
);
error_trigger = true;
}
const sono = require("./sonos-controller");
this.sonos_obj = new sono(this.log, this.config, this.api);
this.box[i] = this.sonos_obj;
} else if (boxtype[i] === "weather") {
const wet = require("./weather");
let wet_settings = this.config.Weather || false;
if (wet_settings !== false) {
let api_key = wet_settings.api_key || undefined;
let unit = wet_settings.units || undefined;
let lat = wet_settings.lat || undefined;
let lon = wet_settings.lon || undefined;
if (
api_key === undefined ||
unit === undefined ||
lat === undefined ||
lon === undefined
lon === undefined ||
unit === null ||
unit === "None"
) {
this.log.error(
"Weather is not done being set up, got to homebridge-display's settings to add it."
Expand All @@ -214,6 +181,7 @@ class HomebridgeDisplay {
} else {
this.wet_obj = new wet(
api_key,
unit,
lat,
lon,
this.log,
Expand Down Expand Up @@ -592,8 +560,8 @@ class HomebridgeDisplay {
res.end("404 not found");
}
});
const io = require("socket.io").listen(server);
io.sockets.on("connection", function (socket) {
const io = require("socket.io")(server);
io.on("connection", function (socket) {
socket.on("update", function () {
// Spotify update route
spot_obj.update(function (result) {
Expand All @@ -610,7 +578,7 @@ class HomebridgeDisplay {
};
request(options, (err, res, body) => {
if (err) {
this.log.debug("[LYRICS] - " + err);
log.debug("[LYRICS] - " + err);
socket.emit(
"lyrics",
JSON.stringify({ error: err })
Expand Down Expand Up @@ -645,7 +613,7 @@ class HomebridgeDisplay {
socket.on("iot", function () {
iot_obj.get_status(function (result) {
let iot_data = JSON.stringify(result);
// log.debug('[ACCESSORY CONTROL - ' + iot_data);
// log.debug('[ACCESSORY CONTROL] - ' + iot_data);
socket.emit("iot", iot_data);
});
});
Expand All @@ -655,7 +623,7 @@ class HomebridgeDisplay {
socket.on("weather", function () {
wet_obj.update(function (result) {
let wet_update = JSON.stringify(result);
// log.debug(wet_update);
// log.debug('[WEATHER] - ' + wet_update);
socket.emit("weather", wet_update);
});
});
Expand Down Expand Up @@ -703,12 +671,20 @@ class HomebridgeDisplay {
spot_obj.put("/repeat?state=" + data, null);
log.debug("[SPOTIFY] - Repeat set to " + data);
});
socket.on("sonos-update", function () {
// Sonos update route
sonos_obj.get_status(function (result) {
let update_data = JSON.stringify(result);
log.debug("[SONOS] - " + update_data);
socket.emit("sonos-update", update_data);
});
});
});
server.on("error", (err) => {
log.debug(err);
log.error(err);
});
server.listen(parseInt(this.config.Config.port), () => {
log(
log.debug(
"Starting Homebridge-Display server on port",
parseInt(this.config.Config.port)
);
Expand Down
29 changes: 29 additions & 0 deletions dist/sonos-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const request = require("request");
const { Sonos } = require("sonos");

class sono {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.devices = [];
const DeviceDiscovery = require("sonos").AsyncDeviceDiscovery;
let discovery = new DeviceDiscovery();
discovery.discover().then((device, model) => {
this.devices.push(new Sonos(device.host));
});
}
get_status() {
for (var device in this.devices) {
device.currentState().then((state) => {
if (state) {
device.currentTrack().then((track) => {
return track;
});
}
});
}
}
}

module.exports = sono;
Loading

0 comments on commit ea77106

Please sign in to comment.