From 1cb64a2c8dfa799565fa7ce6bb4ab82649bafaa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Sun, 24 Jul 2022 18:54:22 +0000 Subject: [PATCH] Make work with Cast --- custom_components/browser_mod/browser_mod.js | 9 ++++--- .../browser_mod/browser_mod_panel.js | 17 +++++++++++- custom_components/browser_mod/manifest.json | 2 +- custom_components/browser_mod/mod_view.py | 26 +++++++++++++++++++ custom_components/browser_mod/store.py | 19 +++++++------- js/config_panel/registered-browsers-card.ts | 18 ++++++++++++- js/plugin/browserID.ts | 1 + js/plugin/connection.ts | 1 + js/plugin/main.ts | 4 +-- test/configuration.yaml | 6 +++++ 10 files changed, 85 insertions(+), 18 deletions(-) diff --git a/custom_components/browser_mod/browser_mod.js b/custom_components/browser_mod/browser_mod.js index d6f02a3..5f0d0c2 100644 --- a/custom_components/browser_mod/browser_mod.js +++ b/custom_components/browser_mod/browser_mod.js @@ -338,8 +338,7 @@ const ConnectionMixin = (SuperClass) => { this.browserEntities = {}; } LOG(...args) { - const dt = new Date(); - console.log(`${dt.toLocaleTimeString()}`, ...args); + return; } fireEvent(event, detail = undefined) { this.dispatchEvent(new CustomEvent(event, { detail })); @@ -1964,6 +1963,8 @@ const BrowserIDMixin = (SuperClass) => { } } get browserID() { + if (document.querySelector("hc-main")) + return "CAST"; if (localStorage[ID_STORAGE_KEY]) return localStorage[ID_STORAGE_KEY]; this.browserID = ""; @@ -2032,12 +2033,12 @@ const BrowserIDMixin = (SuperClass) => { x Title templates - Tweaks - Quickbar tweaks (ctrl+enter)? - - Card-mod preload + x Card-mod preload - Video player? - Media_seek - Screensavers - IMPORTANT: FIX DEFAULT HIDING OF ENTITIES - - Check functionality with CAST - may need to add frontend part as a lovelace resource + X Check functionality with CAST - may need to add frontend part as a lovelace resource */ class BrowserMod extends ServicesMixin(PopupMixin(ActivityMixin(BrowserStateMixin(CameraMixin(MediaPlayerMixin(ScreenSaverMixin(AutoSettingsMixin(FullyMixin(RequireInteractMixin(ConnectionMixin(BrowserIDMixin(EventTarget)))))))))))) { constructor() { diff --git a/custom_components/browser_mod/browser_mod_panel.js b/custom_components/browser_mod/browser_mod_panel.js index 05fb8c9..50be7d9 100644 --- a/custom_components/browser_mod/browser_mod_panel.js +++ b/custom_components/browser_mod/browser_mod_panel.js @@ -365,12 +365,18 @@ class BrowserModRegisteredBrowsersCard extends s { }); } }; - window.browser_mod.showPopup("Unregister browser", `Are you sure you want to unregister browser ${browserID}?`, { + window.browser_mod.showPopup("Unregister browser", `Are you sure you want to unregister Browser ${browserID}?`, { right_button: "Yes", right_button_action: unregisterCallback, left_button: "No", }); } + register_cast() { + window.browser_mod.connection.sendMessage({ + type: "browser_mod/register", + browserID: "CAST", + }); + } render() { return $ ` @@ -389,6 +395,15 @@ class BrowserModRegisteredBrowsersCard extends s { `)} + ${window.browser_mod.browsers["CAST"] === undefined + ? $ ` +
+ + Register CAST Browser + +
+ ` + : ""}
`; } diff --git a/custom_components/browser_mod/manifest.json b/custom_components/browser_mod/manifest.json index bc60a6c..a63f790 100644 --- a/custom_components/browser_mod/manifest.json +++ b/custom_components/browser_mod/manifest.json @@ -2,7 +2,7 @@ "domain": "browser_mod", "name": "Browser mod", "documentation": "https://github.com/thomasloven/hass-browser_mod/blob/master/README.md", - "dependencies": ["panel_custom", "websocket_api", "http", "frontend"], + "dependencies": ["panel_custom", "websocket_api", "http", "frontend", "lovelace"], "codeowners": [], "requirements": [], "version": "2.0b0", diff --git a/custom_components/browser_mod/mod_view.py b/custom_components/browser_mod/mod_view.py index 96746d1..8e54c09 100644 --- a/custom_components/browser_mod/mod_view.py +++ b/custom_components/browser_mod/mod_view.py @@ -2,6 +2,10 @@ from homeassistant.components.frontend import add_extra_js_url from .const import FRONTEND_SCRIPT_URL, SETTINGS_PANEL_URL +import logging + +_LOGGER = logging.getLogger(__name__) + async def async_setup_view(hass): @@ -28,3 +32,25 @@ async def async_setup_view(hass): SETTINGS_PANEL_URL, hass.config.path("custom_components/browser_mod/browser_mod_panel.js"), ) + + # Also load Browser Mod as a lovelace resource so it's accessible to Cast + resources = hass.data["lovelace"]["resources"] + if resources: + if not resources.loaded: + await resources.async_load() + resources.loaded = True + frontend_added = False + for r in resources.async_items(): + if r["url"].startswith(FRONTEND_SCRIPT_URL): + frontend_added = True + continue + # While going through the resources, also preload card-mod if it is found + if "card-mod.js" in r["url"]: + add_extra_js_url(hass, r["url"]) + if not frontend_added: + await resources.async_create_item( + { + "res_type": "module", + "url": FRONTEND_SCRIPT_URL + "?automatically-added", + } + ) diff --git a/custom_components/browser_mod/store.py b/custom_components/browser_mod/store.py index 5b4dbee..95cce54 100644 --- a/custom_components/browser_mod/store.py +++ b/custom_components/browser_mod/store.py @@ -10,7 +10,7 @@ _LOGGER = logging.getLogger(__name__) @attr.s -class Settings: +class SettingsStoreData: hideSidebar = attr.ib(type=bool, default=None) hideHeader = attr.ib(type=bool, default=None) defaultPanel = attr.ib(type=str, default=None) @@ -32,12 +32,12 @@ class BrowserStoreData: last_seen = attr.ib(type=int, default=0) enabled = attr.ib(type=bool, default=False) camera = attr.ib(type=bool, default=False) - settings = attr.ib(type=Settings, factory=Settings) + settings = attr.ib(type=SettingsStoreData, factory=SettingsStoreData) meta = attr.ib(type=str, default="default") @classmethod def from_dict(cls, data): - settings = Settings.from_dict(data.get("settings", {})) + settings = SettingsStoreData.from_dict(data.get("settings", {})) return cls( **( data @@ -55,8 +55,8 @@ class BrowserStoreData: class ConfigStoreData: browsers = attr.ib(type=dict[str:BrowserStoreData], factory=dict) version = attr.ib(type=str, default="2.0") - settings = attr.ib(type=Settings, factory=Settings) - user_settings = attr.ib(type=dict[str:Settings], factory=dict) + settings = attr.ib(type=SettingsStoreData, factory=SettingsStoreData) + user_settings = attr.ib(type=dict[str:SettingsStoreData], factory=dict) @classmethod def from_dict(cls, data={}): @@ -65,9 +65,10 @@ class ConfigStoreData: for k, v in data.get("browsers", {}).items() } user_settings = { - k: Settings.from_dict(v) for k, v in data.get("user_settings", {}).items() + k: SettingsStoreData.from_dict(v) + for k, v in data.get("user_settings", {}).items() } - settings = Settings.from_dict(data.get("settings", {})) + settings = SettingsStoreData.from_dict(data.get("settings", {})) return cls( **( data @@ -135,10 +136,10 @@ class BrowserModStore: await self.updated() def get_user_settings(self, name): - return self.data.user_settings.get(name, Settings()) + return self.data.user_settings.get(name, SettingsStoreData()) async def set_user_settings(self, name, **data): - settings = self.data.user_settings.get(name, Settings()) + settings = self.data.user_settings.get(name, SettingsStoreData()) settings.__dict__.update(data) self.data.user_settings[name] = settings await self.updated() diff --git a/js/config_panel/registered-browsers-card.ts b/js/config_panel/registered-browsers-card.ts index a60789a..30ce9c2 100644 --- a/js/config_panel/registered-browsers-card.ts +++ b/js/config_panel/registered-browsers-card.ts @@ -28,7 +28,7 @@ class BrowserModRegisteredBrowsersCard extends LitElement { window.browser_mod.showPopup( "Unregister browser", - `Are you sure you want to unregister browser ${browserID}?`, + `Are you sure you want to unregister Browser ${browserID}?`, { right_button: "Yes", right_button_action: unregisterCallback, @@ -37,6 +37,13 @@ class BrowserModRegisteredBrowsersCard extends LitElement { ); } + register_cast() { + window.browser_mod.connection.sendMessage({ + type: "browser_mod/register", + browserID: "CAST", + }); + } + render() { return html` @@ -57,6 +64,15 @@ class BrowserModRegisteredBrowsersCard extends LitElement { ` )} + ${window.browser_mod.browsers["CAST"] === undefined + ? html` +
+ + Register CAST Browser + +
+ ` + : ""}
`; } diff --git a/js/plugin/browserID.ts b/js/plugin/browserID.ts index a0333cc..f07cd67 100644 --- a/js/plugin/browserID.ts +++ b/js/plugin/browserID.ts @@ -21,6 +21,7 @@ export const BrowserIDMixin = (SuperClass) => { } get browserID() { + if (document.querySelector("hc-main")) return "CAST"; if (localStorage[ID_STORAGE_KEY]) return localStorage[ID_STORAGE_KEY]; this.browserID = ""; return this.browserID; diff --git a/js/plugin/connection.ts b/js/plugin/connection.ts index ebcfb1b..8267724 100644 --- a/js/plugin/connection.ts +++ b/js/plugin/connection.ts @@ -13,6 +13,7 @@ export const ConnectionMixin = (SuperClass) => { public browserEntities = {}; LOG(...args) { + return; const dt = new Date(); console.log(`${dt.toLocaleTimeString()}`, ...args); } diff --git a/js/plugin/main.ts b/js/plugin/main.ts index b5af80c..9a76216 100644 --- a/js/plugin/main.ts +++ b/js/plugin/main.ts @@ -60,12 +60,12 @@ import { BrowserIDMixin } from "./browserID"; x Title templates - Tweaks - Quickbar tweaks (ctrl+enter)? - - Card-mod preload + x Card-mod preload - Video player? - Media_seek - Screensavers - IMPORTANT: FIX DEFAULT HIDING OF ENTITIES - - Check functionality with CAST - may need to add frontend part as a lovelace resource + X Check functionality with CAST - may need to add frontend part as a lovelace resource */ export class BrowserMod extends ServicesMixin( PopupMixin( diff --git a/test/configuration.yaml b/test/configuration.yaml index 5b857e9..b9879cf 100644 --- a/test/configuration.yaml +++ b/test/configuration.yaml @@ -2,6 +2,12 @@ default_config: demo: +http: + use_x_forwarded_for: true + trusted_proxies: + - 172.17.0.4 + # Update this as needed for testing with Cast + logger: default: warning logs: