Recall browserID from backend if it suddenly disappears
This commit is contained in:
parent
5ecf24d501
commit
ae916a3900
@ -156,7 +156,17 @@ def getBrowser(hass, browserID, *, create=True):
|
|||||||
|
|
||||||
|
|
||||||
def deleteBrowser(hass, browserID):
|
def deleteBrowser(hass, browserID):
|
||||||
|
"""Delete a browser by BrowserID."""
|
||||||
browsers = hass.data[DOMAIN][DATA_BROWSERS]
|
browsers = hass.data[DOMAIN][DATA_BROWSERS]
|
||||||
if browserID in browsers:
|
if browserID in browsers:
|
||||||
browsers[browserID].delete(hass)
|
browsers[browserID].delete(hass)
|
||||||
del browsers[browserID]
|
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
|
||||||
|
@ -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() {
|
get browserID() {
|
||||||
if (document.querySelector("hc-main"))
|
if (document.querySelector("hc-main"))
|
||||||
return "CAST";
|
return "CAST";
|
||||||
if (localStorage[ID_STORAGE_KEY])
|
if (localStorage[ID_STORAGE_KEY])
|
||||||
return localStorage[ID_STORAGE_KEY];
|
return localStorage[ID_STORAGE_KEY];
|
||||||
this.browserID = "";
|
this.browserID = "";
|
||||||
|
this.recall_id();
|
||||||
return this.browserID;
|
return this.browserID;
|
||||||
}
|
}
|
||||||
set browserID(id) {
|
set browserID(id) {
|
||||||
|
@ -18,7 +18,7 @@ from .const import (
|
|||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .browser import getBrowser, deleteBrowser
|
from .browser import getBrowser, deleteBrowser, getBrowserByConnection
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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)})
|
await store.set_global_settings(**{msg["key"]: msg.get("value", None)})
|
||||||
pass
|
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_connect)
|
||||||
async_register_command(hass, handle_register)
|
async_register_command(hass, handle_register)
|
||||||
async_register_command(hass, handle_unregister)
|
async_register_command(hass, handle_unregister)
|
||||||
async_register_command(hass, handle_reregister)
|
async_register_command(hass, handle_reregister)
|
||||||
async_register_command(hass, handle_update)
|
async_register_command(hass, handle_update)
|
||||||
async_register_command(hass, handle_settings)
|
async_register_command(hass, handle_settings)
|
||||||
|
async_register_command(hass, handle_recall_id)
|
||||||
|
async_register_command(hass, handle_log)
|
||||||
|
@ -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() {
|
get browserID() {
|
||||||
if (document.querySelector("hc-main")) return "CAST";
|
if (document.querySelector("hc-main")) return "CAST";
|
||||||
if (localStorage[ID_STORAGE_KEY]) return localStorage[ID_STORAGE_KEY];
|
if (localStorage[ID_STORAGE_KEY]) return localStorage[ID_STORAGE_KEY];
|
||||||
this.browserID = "";
|
this.browserID = "";
|
||||||
|
this.recall_id();
|
||||||
return this.browserID;
|
return this.browserID;
|
||||||
}
|
}
|
||||||
set browserID(id) {
|
set browserID(id) {
|
||||||
|
@ -16,6 +16,11 @@ export const ConnectionMixin = (SuperClass) => {
|
|||||||
return;
|
return;
|
||||||
const dt = new Date();
|
const dt = new Date();
|
||||||
console.log(`${dt.toLocaleTimeString()}`, ...args);
|
console.log(`${dt.toLocaleTimeString()}`, ...args);
|
||||||
|
|
||||||
|
this.connection.sendMessage({
|
||||||
|
type: "browser_mod/log",
|
||||||
|
message: args[0],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private fireEvent(event, detail = undefined) {
|
private fireEvent(event, detail = undefined) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user