Recall browserID from backend if it suddenly disappears

This commit is contained in:
Thomas Lovén 2022-07-25 19:28:39 +00:00
parent 5ecf24d501
commit ae916a3900
5 changed files with 67 additions and 1 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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)

View File

@ -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) {

View File

@ -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) {