thomasloven / hass-browser_mod

🔹 A Home Assistant integration to turn your browser into a controllable entity and media player
MIT License
1.31k stars 186 forks source link

Run browser_mod.javascript only on device from trigger #666

Open Rijswijker opened 9 months ago

Rijswijker commented 9 months ago

My Home Assistant version: 2023.12

What I am doing: I share a dashboard on multiple NSPanels containing the Home Assistan companion app. On it, I have hidden the title bar and want to return to the previous page (read subview) using a button in my grid layout.

Now I use a call-service action with the service browser_mod.javascript with the code history.back() This in itself works well, only the action is performed on all NSPanels with the corresponding dashboard. Instead of only on the device where I press the back button.

I have tried, for example, having the device_id filled in dynamically by using BrowserID, self, SELF, this or THIS, but all without the desired result. Leaving the target section away is also no option.

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: browser_mod.javascript
  target:
    device_id:
      - SELF
  data:
    code: history.back()
icon: mdi:arrow-left-bold
hold_action:
  action: none

If I enter the real device_id then it does only respond to the corresponding panel, but since I now have to create all dashboards 6 times, it is nicer to be able to share dashboards so that I no longer have repetitive work, and differences in dashboards.

Is there a way to do this?

What I expected to happen: That only the browser on which the action is being triggered responds and not all browsers.

What happened instead: See description

Minimal steps to reproduce: Open two browsers with the same dashboard and preform a javascript service call with history.back() as code.

Error messages from the browser console:

No errors

By replacing the space in the checkboxes ([ ]) with an X below, I indicate that I:

danielrosehill commented 6 months ago

Any luck? I'm trying to build an automation whereby an NFC tag is scanned and then that user gets a browsermod navigation to the chore logging page. But you can guess the issue, maybe. How do I get browsermod to only operate for that user!

Rijswijker commented 6 months ago

Unfortunately no, I still have to maintain 6 dashboards. 😢

pk802 commented 2 weeks ago

Unfortunately no, I still have to maintain 6 dashboards. 😢

I was recently looking for solution to this issue. I solved the issue by using fire-dom-event

  - type: icon
    icon: mdi:stop-circle
    tap_action:
      action: fire-dom-event
      browser_mod:
        service: script.pause_video
        data:
          browser_id: THIS

and in script I used browser_id and put java script.

data:
  browser_id: "{{ browser_id }}"
  code: |
    const videoElement = document
      .querySelector("body > home-assistant").shadowRoot
      .querySelector("home-assistant-main").shadowRoot
      .querySelector("ha-drawer > partial-panel-resolver > ha-panel-lovelace").shadowRoot
      .querySelector("hui-root").shadowRoot
      .querySelector("#view > hui-view > hui-panel-view").shadowRoot
      .querySelector("hui-card > hui-picture-elements-card").shadowRoot
      .querySelector("#root > button-card").shadowRoot
      .querySelector("#videoPlayer");

    if (videoElement) {
      videoElement.pause();  // Pause the video playback

      // Clear the reverse playback interval
      if (window.reversePlayback) {
        clearInterval(window.reversePlayback);
        window.reversePlayback = null;  // Reset the global variable
      }
    } else {
      console.error("Video element not found");
    }
action: browser_mod.javascript