diff --git a/custom_components/browser_mod/browser.py b/custom_components/browser_mod/browser.py index ce279b3..ae2c014 100644 --- a/custom_components/browser_mod/browser.py +++ b/custom_components/browser_mod/browser.py @@ -156,7 +156,17 @@ def getBrowser(hass, browserID, *, create=True): def deleteBrowser(hass, browserID): + """Delete a browser by BrowserID.""" browsers = hass.data[DOMAIN][DATA_BROWSERS] if browserID in browsers: browsers[browserID].delete(hass) del browsers[browserID] + + +def getBrowserByConnection(hass, connection): + """Get the browser that has a given connection open.""" + browsers = hass.data[DOMAIN][DATA_BROWSERS] + + for k, v in browsers.items(): + if any([c[0] == connection for c in v.connection]): + return v diff --git a/custom_components/browser_mod/browser_mod.js b/custom_components/browser_mod/browser_mod.js index 8479c80..a574ee2 100644 --- a/custom_components/browser_mod/browser_mod.js +++ b/custom_components/browser_mod/browser_mod.js @@ -2062,12 +2062,25 @@ const BrowserIDMixin = (SuperClass) => { } } } + async recall_id() { + // If the connection is still open, but the BrowserID has disappeared - recall it from the backend + // This happens e.g. when the frontend cache is reset in the Compainon app + if (!this.connection) + return; + const recalledID = await this.connection.sendMessagePromise({ + type: "browser_mod/recall_id", + }); + if (recalledID) { + localStorage[ID_STORAGE_KEY] = recalledID; + } + } get browserID() { if (document.querySelector("hc-main")) return "CAST"; if (localStorage[ID_STORAGE_KEY]) return localStorage[ID_STORAGE_KEY]; this.browserID = ""; + this.recall_id(); return this.browserID; } set browserID(id) { diff --git a/custom_components/browser_mod/connection.py b/custom_components/browser_mod/connection.py index d1e432a..9c75900 100644 --- a/custom_components/browser_mod/connection.py +++ b/custom_components/browser_mod/connection.py @@ -18,7 +18,7 @@ from .const import ( DOMAIN, ) -from .browser import getBrowser, deleteBrowser +from .browser import getBrowser, deleteBrowser, getBrowserByConnection _LOGGER = logging.getLogger(__name__) @@ -159,9 +159,34 @@ async def async_setup_connection(hass): await store.set_global_settings(**{msg["key"]: msg.get("value", None)}) pass + @websocket_api.websocket_command( + { + vol.Required("type"): "browser_mod/recall_id", + } + ) + def handle_recall_id(hass, connection, msg): + dev = getBrowserByConnection(hass, connection) + if dev: + connection.send_message( + websocket_api.result_message(msg["id"], dev.browserID) + ) + connection.send_message(websocket_api.result_message(msg["id"], None)) + + @websocket_api.websocket_command( + { + vol.Required("type"): "browser_mod/log", + vol.Required("message"): str, + } + ) + def handle_log(hass, connection, msg): + _LOGGER.info("LOG MESSAGE") + _LOGGER.info(msg["message"]) + async_register_command(hass, handle_connect) async_register_command(hass, handle_register) async_register_command(hass, handle_unregister) async_register_command(hass, handle_reregister) async_register_command(hass, handle_update) async_register_command(hass, handle_settings) + async_register_command(hass, handle_recall_id) + async_register_command(hass, handle_log) diff --git a/js/plugin/browserID.ts b/js/plugin/browserID.ts index f07cd67..e12cd59 100644 --- a/js/plugin/browserID.ts +++ b/js/plugin/browserID.ts @@ -20,10 +20,23 @@ export const BrowserIDMixin = (SuperClass) => { } } + async recall_id() { + // If the connection is still open, but the BrowserID has disappeared - recall it from the backend + // This happens e.g. when the frontend cache is reset in the Compainon app + if (!this.connection) return; + const recalledID = await this.connection.sendMessagePromise({ + type: "browser_mod/recall_id", + }); + if (recalledID) { + localStorage[ID_STORAGE_KEY] = recalledID; + } + } + get browserID() { if (document.querySelector("hc-main")) return "CAST"; if (localStorage[ID_STORAGE_KEY]) return localStorage[ID_STORAGE_KEY]; this.browserID = ""; + this.recall_id(); return this.browserID; } set browserID(id) { diff --git a/js/plugin/connection.ts b/js/plugin/connection.ts index 8267724..5b7b0f5 100644 --- a/js/plugin/connection.ts +++ b/js/plugin/connection.ts @@ -16,6 +16,11 @@ export const ConnectionMixin = (SuperClass) => { return; const dt = new Date(); console.log(`${dt.toLocaleTimeString()}`, ...args); + + this.connection.sendMessage({ + type: "browser_mod/log", + message: args[0], + }); } private fireEvent(event, detail = undefined) {