diff --git a/index.js b/index.js index 0da25d6..80549c6 100644 --- a/index.js +++ b/index.js @@ -18,25 +18,24 @@ function createWindow () { width: 800 }) - browserWindow.os = os.platform() browserWindow.settings = settings loadHomeAssistantOrLoginPage() - //Change window dimensions if provided + // Change window dimensions if provided if (settings.hasDimensions()) { browserWindow.setContentSize(settings.width(), settings.height(), false) } - //Change window position if provided + // Change window position if provided if (settings.hasPosition()) { browserWindow.setPosition(settings.xpos(), settings.ypos()) } createMenu() - //Connect to home assistant + // Connect to home assistant browserWindow.connect = (url, password) => { browserWindow.url = url browserWindow.password = password @@ -44,17 +43,17 @@ function createWindow () { loadHomeAssistantOrLoginPage() } - //Save settings and reconnect to home assistant + // Save settings and reconnect to home assistant browserWindow.reload = () => { loadHomeAssistantOrLoginPage() } - //Notifiy UI that the theme color has changed + // Notifiy UI that the theme color has changed browserWindow.setColor = (color) => { browserWindow.webContents.send('colorChange', {color}) } - //Reset EVERYTHING + // Reset EVERYTHING browserWindow.reset = () => { settings.deleteAll() app.quit() @@ -66,7 +65,7 @@ function createWindow () { browserWindow = null }) - //Save window bounds on close + // Save window bounds on close browserWindow.on('close', () => { settings.saveWindowBounds(browserWindow.getPosition()[0], browserWindow.getPosition()[1], browserWindow.getBounds().width, browserWindow.getBounds().height) @@ -75,7 +74,7 @@ function createWindow () { app.on('ready', createWindow) -//Just OSX-Stuff +// Just OSX-Stuff app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() @@ -88,6 +87,15 @@ app.on('activate', () => { } }) +/** + * Fake basic-auth + */ +app.on('login', function (event, webContents, request, authInfo, callback) { + event.preventDefault() + let credentials = settings.configuratorCredentials() + callback(credentials.username, credentials.password) +}) + /** * Create the application menu */ @@ -96,7 +104,7 @@ function createMenu () { Menu.setApplicationMenu(menu) } -//Load a HTML page +// Load a HTML page function load (html) { browserWindow.loadURL(url.format({ pathname: path.join(__dirname, 'src', html), @@ -105,13 +113,13 @@ function load (html) { })) } -//Switch HomeAssistant page (eg. State) +// Switch HomeAssistant page (eg. State) function setPage (page) { browserWindow.webContents.send('change', { page }) } -//Load HomeAssistant or configuration view -function loadHomeAssistantOrLoginPage() { +// Load HomeAssistant or configuration view +function loadHomeAssistantOrLoginPage () { if (settings.kiosk()) { browserWindow.setFullScreen(true) } @@ -123,4 +131,4 @@ function loadHomeAssistantOrLoginPage() { } else { load('connect.html') } -} \ No newline at end of file +} diff --git a/menu.js b/menu.js index ee1aeba..eea7cf5 100644 --- a/menu.js +++ b/menu.js @@ -1,7 +1,7 @@ -module.exports = function (browserWindow, darwin, load, setPage) { - var menu = [{ - label: 'Go', - submenu: [ +module.exports = function (browserWindow, darwin, load, setPage) { + var menu = [{ + label: 'Go', + submenu: [ { label: 'States', accelerator: 'Cmd+1', click: () => setPage('states') }, { label: 'History', accelerator: 'Cmd+2', click: () => setPage('history') }, { label: 'Map', accelerator: 'Cmd+3', click: () => setPage('map') }, @@ -9,25 +9,25 @@ module.exports = function (browserWindow, darwin, load, setPage) { { label: 'Services', accelerator: 'Cmd+5', click: () => setPage('dev-service') }, { type: 'separator' }, { label: 'Preferences...', click: () => load('settings.html') } - ] - }, { - label: 'Edit', - submenu: [{ role: 'copy' }, { role: 'selectall' }, { role: 'paste' }] - }, { - label: 'Developer', - submenu: [ + ] + }, { + label: 'Edit', + submenu: [{ role: 'copy' }, { role: 'selectall' }, { role: 'paste' }] + }, { + label: 'Developer', + submenu: [ { role: 'toggledevtools' }, { label: 'Reload', accelerator: 'Cmd+Shift+R', click: () => browserWindow.webContents.send('reload', {}) }] - }] + }] // Mac default menu - if (darwin) { - menu.unshift({ - label: 'Home Assistant', - submenu: [ + if (darwin) { + menu.unshift({ + label: 'Home Assistant', + submenu: [ { role: 'about' }, { role: 'quit' }] - }) - } + }) + } - return menu -} \ No newline at end of file + return menu +} diff --git a/package-lock.json b/package-lock.json index d6d652c..46ba206 100644 --- a/package-lock.json +++ b/package-lock.json @@ -417,6 +417,11 @@ "integrity": "sha1-mI3zP+qxke95mmE2nddsF635V+o=", "dev": true }, + "currify": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/currify/-/currify-2.0.6.tgz", + "integrity": "sha512-F0lbcoBkA2FMcejFeHJkDEhQ1AvVkTpkn9PMzJch+7mHy5WdteZ9t+nhT6cOdga4uRay3rjvprgp8tUkixFy8w==" + }, "dashdash": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", @@ -725,6 +730,11 @@ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "dev": true }, + "fullstore": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fullstore/-/fullstore-1.1.0.tgz", + "integrity": "sha512-XNlCWr3KBIL97G8mTR+dZ/J648ECCffflfFRgZW3Zm7pO0PYnH/ZCbwZjV1Dw4LrrDdhV6gnayiIcmdIY4JTsw==" + }, "get-caller-file": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.2.tgz", @@ -1541,6 +1551,11 @@ "integrity": "sha1-wvg/Jzo+GhbtsJlWYdoO1e8DM2Q=", "dev": true }, + "smalltalk": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/smalltalk/-/smalltalk-3.0.0.tgz", + "integrity": "sha512-ayyVR3kh3g0O9QvRHSJ/Ahbw5QVI/CrHxMY+boaNf9cwiLkBFh+24b2iEQgK07vGxYT0KA4jXBf24bFFIHvOfw==" + }, "sntp": { "version": "1.0.9", "resolved": "https://registry.npmjs.org/sntp/-/sntp-1.0.9.tgz", @@ -1814,6 +1829,11 @@ } } }, + "vex": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/vex/-/vex-0.0.4.tgz", + "integrity": "sha1-oi1/sC6XvpOOSytDJ8MU0l2C9AI=" + }, "which": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/which/-/which-1.3.0.tgz", diff --git a/settings.js b/settings.js index 10065a1..57d7823 100644 --- a/settings.js +++ b/settings.js @@ -1,162 +1,181 @@ const settings = require('electron-settings') - /** * Modul for loading and storing user preferences and logins */ class Settings { - /** * @returns true if settings contain an url */ - hasUrl() { - return settings.has('url') - } + hasUrl () { + return settings.has('url') + } /** * @returns url */ - url() { - return settings.get('url') - } + url () { + return settings.get('url') + } /** * @returns password */ - password() { - return settings.get('password') - } + password () { + return settings.get('password') + } /** * @returns true if tray-menu should be shown */ - tray() { - return settings.get('tray', true) - } + tray () { + return settings.get('tray', true) + } /** * @return true if notifications should be shown */ - notifications() { - return settings.get('notifications') - } + notifications () { + return settings.get('notifications') + } /** * @return true if window dimensions are saved in the settings */ - hasDimensions() { - return settings.has('width') && settings.has('height') - } + hasDimensions () { + return settings.has('width') && settings.has('height') + } /** * @returns width */ - width() { - return settings.get('width') - } + width () { + return settings.get('width') + } /** * @returns tray icon */ - icon() { - return settings.get('icon', 'Black') - } + icon () { + return settings.get('icon', 'Black') + } /** * @returns height */ - height() { - return settings.get('height') - } + height () { + return settings.get('height') + } /** * @returns true if toolbar should not be hidden */ - toolbar() { - return settings.get('toolbar_always', true) - } + toolbar () { + return settings.get('toolbar_always', true) + } + + /** + * Set credentials for the Configurator panel + * @param {*} username + * @param {*} password + */ + setConfiguratorCredentials (username, password) { + settings.set('c_user', username) + settings.set('c_password', password) + } + + /** + * @return Credentials for the Configurator panel + */ + configuratorCredentials () { + return { + username: settings.get('c_user', ''), + password: settings.get('c_password', '') + } + } /** * @returns true if the app should start in kiosk-mode */ - kiosk() { - return settings.get('kiosk') - } + kiosk () { + return settings.get('kiosk') + } /** * @returns true if window postitions are saved in the settings */ - hasPosition() { - return settings.has('xpos') && settings.has('ypos') - } + hasPosition () { + return settings.has('xpos') && settings.has('ypos') + } /** * @returns window x-position */ - xpos() { - return settings.get('xpos') - } + xpos () { + return settings.get('xpos') + } /** * @returns window y-position */ - ypos() { - return settings.get('ypos') - } + ypos () { + return settings.get('ypos') + } /** * @returns last window color */ - color() { - return settings.get('color', '#03A9F4') - } + color () { + return settings.get('color', '#03A9F4') + } /** * Save url and password - * @param {*} url - * @param {*} password + * @param {*} url + * @param {*} password */ - setUrlAndPassword(url, password) { - settings.set('url', url) - settings.set('password', password) - } + setUrlAndPassword (url, password) { + settings.set('url', url) + settings.set('password', password) + } /** * Save user preferences - * @param {*} notifications - * @param {*} tray - * @param {*} kiosk - * @param {*} toolbar - */ - setSettings(notifications, tray, kiosk, toolbar, icon) { - settings.set('notifications', notifications) - settings.set('tray', tray) - settings.set('kiosk', kiosk) - settings.set('toolbar_always', toolbar) - settings.set('icon', icon) - } + * @param {*} notifications + * @param {*} tray + * @param {*} kiosk + * @param {*} toolbar + */ + setSettings (notifications, tray, kiosk, toolbar, icon, c_user, c_password) { + settings.set('notifications', notifications) + settings.set('tray', tray) + settings.set('kiosk', kiosk) + settings.set('toolbar_always', toolbar) + settings.set('icon', icon) + settings.set('c_user', c_user) + settings.set('c_password', c_password) + } /** * Save window position - * @param {*} x - * @param {*} y - * @param {*} w - * @param {*} h + * @param {*} x + * @param {*} y + * @param {*} w + * @param {*} h */ - saveWindowBounds(x, y, w, h) { - settings.set('xpos', x) - settings.set('ypos', y) - settings.set('width', w) - settings.set('height', h) - } + saveWindowBounds (x, y, w, h) { + settings.set('xpos', x) + settings.set('ypos', y) + settings.set('width', w) + settings.set('height', h) + } /** * Deletes ALL settings */ - deleteAll() { - settings.deleteAll() - } - + deleteAll () { + settings.deleteAll() + } } -module.exports = new Settings() \ No newline at end of file +module.exports = new Settings() diff --git a/src/index.html b/src/index.html index 6e8a402..a38936e 100644 --- a/src/index.html +++ b/src/index.html @@ -22,15 +22,16 @@ } + + +
- - diff --git a/src/preload.js b/src/preload.js index 859c32a..750b75e 100644 --- a/src/preload.js +++ b/src/preload.js @@ -8,33 +8,32 @@ const {remote, ipcRenderer} = require('electron') function login () { var password = remote.getCurrentWindow().settings.password() var notifications = remote.getCurrentWindow().settings.notifications() - document.querySelector('home-assistant').shadowRoot.querySelector('login-form').password = password - document.querySelector('home-assistant').shadowRoot.querySelector('login-form').validatePassword() - let interval = setInterval(() => { - - let main = document.querySelector('home-assistant').shadowRoot.querySelector('home-assistant-main') - if (main) { + document.querySelector('home-assistant').shadowRoot.querySelector('login-form').password = password + document.querySelector('home-assistant').shadowRoot.querySelector('login-form').validatePassword() + let interval = setInterval(() => { + let main = document.querySelector('home-assistant').shadowRoot.querySelector('home-assistant-main') + if (main) { // Main view loaded - if (remote.getCurrentWindow().settings.toolbar()) { + if (remote.getCurrentWindow().settings.toolbar()) { // Always show toolbar - main.shadowRoot.querySelector('app-drawer-layout') + main.shadowRoot.querySelector('app-drawer-layout') .querySelector('iron-pages') .querySelector('partial-cards') .shadowRoot.querySelector('ha-app-layout') .querySelector('app-header') .removeAttribute('condenses') - } + } // Get color - let hass = document.querySelector('home-assistant') - remote.getCurrentWindow().setColor(getComputedStyle(hass).getPropertyValue('--primary-color')) + let hass = document.querySelector('home-assistant') + remote.getCurrentWindow().setColor(getComputedStyle(hass).getPropertyValue('--primary-color')) - var observer = new MutationObserver(function (mutations) { - remote.getCurrentWindow().setColor(getComputedStyle(main).getPropertyValue('--primary-color')) - }) - observer.observe(hass, { attributes: true, attributeFilter: ['style'] }) - clearInterval(interval) - } - }, 100) + var observer = new MutationObserver(function (mutations) { + remote.getCurrentWindow().setColor(getComputedStyle(main).getPropertyValue('--primary-color')) + }) + observer.observe(hass, { attributes: true, attributeFilter: ['style'] }) + clearInterval(interval) + } + }, 100) if (notifications) { getNotifications() diff --git a/src/settings.html b/src/settings.html index a6f6257..8146955 100644 --- a/src/settings.html +++ b/src/settings.html @@ -88,6 +88,10 @@

Settings

Kiosk mode + +
+ Credentials for the Configurator addon +