Skip to content

Script and button to set Browser Mod 'Set Clear' Settings

dcapslock edited this page May 1, 2025 · 4 revisions

This Wiki article is in response to a feature request (https://github.com/thomasloven/hass-browser_mod/issues/806)

Script and button to set Browser Mod 'Set/Clear' Settings

The script takes advantage of the setSettings(type, target, settings) javascript function that is part of the mechanism used by Browser Mod Frontend settings panel. This script has been tested to make sure correct javascript code is run. If incorrect code or parameters are sent to the function it may not work or even have unintended consequences.

The script below allows for setting and clearnig the following Browser Mod settings.

  • Hide sidebar
  • Hide header
  • Hide interaction icon
  • Save screen state (available v2.3.6 and onwards)

Setting and clearing can be targetted to Browsers, Users or Globally. Calling the script via a Browser Mod browser call allows to target the current browser passing THIS as browser_id. See the examples at the end of this Wiki article for actions using this method.

The script takes the following parameters. Home Assistant UI selectors are available when setting up with UI. Note:

  • Full description in table below
  • When run, the script will send a notification to each target with an action button to Reload. Reload is not automatic. Generally hide settings will not need a reload, but clear will for sidebar and header as these DOM elements are actually deleted when hidden so cannot be reinstatetd without a reload of the browser.
  • Square brackets [ ] indicate that a parameter is optional and can be omitted.
action: script.<script_name>
  service_browser: <Browser ID>
  [browser_id: <Browser ID>]
  settings: <list>
  value: <"true" / undefined>
  [target_globally: <true / FALSE>]
  [target_browsers: <list>]
  [target_users: <list>]
  [target_service_browser: <true / FALSE>]
service_browser The browser to run the javascript on. This is required in UI but can be omitted if calling with browser_id via a browser call
browser_id The browser to run the javascript on. This is required if service_browser is not set. Allowing for browser_id as a parameter makes it possible to use a Browser Mod browser call with browser_id: THIS
settings One or more of the following to set to the value passed. hideSidebar, hideHeader, hideInteractionIcon, saveScreenState
value Value to be set or cleared. IMPORTANT: If setting then you need to pass "true", including quotes. If clearing you need to pass the string undefined
target_globally Set to true to target the setting globally.
target_browsers A list of Browser IDs for which to target the setting. This can be the Browser ID string or the Home Assistant device_id, the latter being passed when using UI selectors. The script will handle both.
target_users A list of Users for which to target the setting. This can be a person entity (e.g. person.bob) or the Home Assistant user_id, the former being passed when using UI selectors. The script will handle both. NOTE: If a user is not associated with a person entity, they will not be shown in UI selectors, but are still supported.
target_service_browser To target service_browser set to true. When true then all other targets are ignored. This can be used when using a browser call with browser_id: THIS thus targetting the browser where the call is made.

The script

Create a script and edit in yaml mode and paste all the code below. If you keep the alias as is, the script will be available via script.browser_mod_settings_script.

sequence:
  - alias: Check for any targets
    if:
      - condition: not
        conditions:
          - condition: template
            value_template: >-
              {{ target_service_browser or target_globally or
              target_browsers|length|bool or target_users|length|bool }}
    then:
      - stop: No targets set
        error: true
  - alias: >-
      If browser_id is passed in then use it instead of service_browser.  This
      will happen when calling from fire_dom_event browser_mod action  and
      passing browser_id as THIS which browser mod allows
    variables:
      service_browser: |
        {{ iif(service_browser, service_browser, browser_id) }}
      settingsDict: >
        { {%- for settingKey in settings %}{{settingKey}}: {{value}}, {% endfor
        -%} }
  - alias: >-
      If targetting Service Browser clear out other targets and seet
      browser_targets to service_browser.
    if:
      - condition: template
        value_template: "{{ target_service_browser }}"
    then:
      - variables:
          target_browsers:
            - "{{ service_browser }}"
          target_users: []
          target_globally: false
        enabled: true
  - alias: If Browser Targets
    if:
      - condition: template
        value_template: "{{ target_browsers|length|bool }}"
    then:
      - variables:
          command: >
            window.browser_mod.setSetting('browser', 'browser_id', {{
            settingsDict }})
          commandList: []
      - alias: Construct command sequence
        repeat:
          sequence:
            - alias: >-
                Append to commandList. Handles if is device_id or browser_id and
                converts device_id to browser_id
              variables:
                commandList: >
                  {{ commandList + [ command | regex_replace(find='browser_id',
                  replace=iif(device_attr(repeat.item, 'name'),
                  device_attr(repeat.item, 'name'), repeat.item)) ] }}
          for_each: |
            {{ target_browsers }}
      - variables:
          javascriptCommand: |
            {{ commandList | join(';') }}           
      - action: browser_mod.javascript
        data:
          browser_id: |
            {{ service_browser }}
          code: |
            {{ javascriptCommand }}
      - action: browser_mod.notification
        metadata: {}
        data:
          browser_id: |
            {{ target_browsers }}
          message: Browser Mod settings have changed. You may need to reload.
          action_text: Reload
          action:
            - action: browser_mod.javascript
              data:
                browser_id: THIS
                code: window.location.reload()
    enabled: true
  - alias: If User Targets
    if:
      - condition: template
        value_template: "{{ target_users|length|bool }}"
    then:
      - variables:
          command: |
            window.browser_mod.setSetting('user', 'user_id', {{ settingsDict }})
          commandList: []
      - alias: Construct command sequence
        repeat:
          sequence:
            - alias: >-
                Append to commandList. Handles if is entity_id or user_id and
                converts entity_id to user_id
              variables:
                commandList: >
                  {{ commandList + [ command | regex_replace(find='user_id',
                  replace=iif(state_attr(repeat.item, 'user_id'),
                  state_attr(repeat.item, 'user_id'), repeat.item)) ] }}
          for_each: |
            {{ target_users }}
      - variables:
          javascriptCommand: |
            {{ commandList | join(';') }}                      
      - action: browser_mod.javascript
        data:
          browser_id:
            - e37643b8cd031cfb006c04181ac31758
          code: |
            {{ javascriptCommand }}
      - action: browser_mod.notification
        metadata: {}
        data:
          user_id: |
            {{ target_users }}
          message: Browser Mod settings have changed. You may need to reload.
          action_text: Reload
          action:
            - action: browser_mod.javascript
              data:
                browser_id: THIS
                code: window.location.reload()
    enabled: true
  - alias: If Global Target
    if:
      - condition: template
        value_template: "{{ target_globally }}"
    then:
      - action: browser_mod.javascript
        metadata: {}
        data:
          browser_id: |
            {{ service_browser }}
          code: |
            window.browser_mod.setSetting('global', '', {{ settingsDict }}) 
      - action: browser_mod.notification
        metadata: {}
        data:
          message: Browser Mod settings have changed. You may need to reload.
          action_text: Reload
          action:
            - action: browser_mod.javascript
              data:
                browser_id: THIS
                code: window.location.reload()
fields:
  service_browser:
    selector:
      device:
        filter:
          integration: browser_mod
    name: Service Browser
    description: >-
      Browser to run code on. if you wish this to be the browser calling the
      action as a script, make it a browser call. Read browser_mod service
      target documentation for more information.
    required: true
  settings:
    selector:
      select:
        options:
          - label: Hide sidebar
            value: hideSidebar
          - label: Hide header
            value: hideHeader
          - label: Hide interaction icon
            value: hideInteractIcon
          - label: Save screen state
            value: saveScreenState
        multiple: true
    name: Settings
    description: "Setting to change. "
    required: true
  value:
    selector:
      select:
        options:
          - label: Set (true)
            value: "true"
          - label: Clear (undefined)
            value: undefined
    name: Value
    required: true
  target_globally:
    selector:
      boolean: {}
    name: Target Globally
    description: Setting will be saved globally
  target_browsers:
    selector:
      device:
        multiple: true
        filter:
          integration: browser_mod
    name: Target Browsers
    description: Select Browsers to target setting change
  target_users:
    selector:
      entity:
        multiple: true
        filter:
          domain: person
    name: Target Users
    description: Select Users to target setting change
  target_service_browser:
    selector:
      boolean: {}
    name: Target Service Browser
    description: >-
      Target setting change for Service Browser only. Any other targets will be
      IGNORED.
alias: Browser Mod Settings Script
description: ""

Example buttons

The example buttons use a Browser Mod browser call passing browser_id: THIS which will set/clear settings for the current browser. Example buttons will run using script.browser_mod_settings_script. If you changed the alias of the script you will need to update accordingly. The action: can be used whether actions are supported, including Browser Mod popups.

Hide sidebar and header

show_name: true
show_icon: true
name: Hide Sidebar and Header for THIS Browser
type: button
tap_action:
  action: fire-dom-event
  browser_mod:
    service: script.browser_mod_settings_script
    data:
      settings:
        - hideSidebar
        - hideHeader
      value: "true"
      target_service_browser: true
      browser_id: THIS

Show sidebar and header

show_name: true
show_icon: true
name: Show Sidebar and Header for THIS Browser
type: button
tap_action:
  action: fire-dom-event
  browser_mod:
    service: script.browser_mod_settings_script
    data:
      settings:
        - hideSidebar
        - hideHeader
      value: undefined
      target_service_browser: true
      browser_id: THIS
Clone this wiki locally