Skip to content

Instantly share code, notes, and snippets.

@balloob
Created June 22, 2020 04:51
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save balloob/580deaf8c3fc76948559c5963ed4d436 to your computer and use it in GitHub Desktop.
Save balloob/580deaf8c3fc76948559c5963ed4d436 to your computer and use it in GitHub Desktop.
Add redirects to the Home Assistant sidebar to any place in Home Assistant
/*
Add a link to the sidebar to any path in Home Assistant
Put this file in <config>/www/panel-redirect.js
In configuration.yaml:
panel_custom:
- name: panel-redirect
# url_path needs to be unique for each panel_custom config
url_path: redirect-server-controls
sidebar_title: Server Controls
sidebar_icon: mdi:server
module_url: /local/panel-redirect.js
config:
# Where you want to redirect to
target: /config/server_control
*/
class PanelRedirect extends HTMLElement {
connectedCallback() {
if (this._info) {
this._navigate();
}
}
set panel(info) {
this._info = info;
if (this.isConnected) {
this._navigate();
}
}
_navigate() {
history.replaceState(null, "", this._info.config.target);
const event = new Event("location-changed", {
bubbles: true,
composed: true,
});
event.detail = { replace: true };
this.dispatchEvent(event);
}
}
customElements.define("panel-redirect", PanelRedirect);
@jonk999
Copy link

jonk999 commented Dec 15, 2021

Thanks for all the replies.
There was no www folder under config initially (I don't use HACS) and so needed to create it. I then clicked on 'raw' above and saved the file then put it in the www folder I created (not in a subfolder of www).
Once that was done, I added the following to my configuration.yaml file:
panel_custom:

  • name: panel-redirect
    url_path: redirect-supervisor
    sidebar_title: Supervisor
    sidebar_icon: mdi:home-assistant
    module_url: /local/panel-redirect.js
    config:
    target: /hassio/dashboard

I then restarted HA via the menu and when it loaded back up and I clicked on the side bar link for Supervisor received the error I originally posted.

For the time being I have rolled back to the previous version of HA I was running as on the latest for some reason some light entities disappeared/stopped working. I have some Tuya switches that are a fan controller and light switch. When I upgraded the fan switch/controller remained, but the light switch didn't. When I rolled back, the light switch was there again and worked fine. This is using the new Tuya integration so not sure what happened as part of the upgrade.

@danielbrunt57
Copy link

@jonk999 I took what you're trying to do and added it to my config and it works fine...

panel_custom:
  - name: insteon-frontend
    url_path: insteon
    sidebar_title: Insteon
    sidebar_icon: mdi:server
    module_url: /local/insteon_frontend/entrypoint.js
    embed_iframe: true
    require_admin: true

  - name: panel-redirect
    # url_path needs to be unique for each panel_custom config
    url_path: redirect-server-controls
    sidebar_title: Server Controls
    sidebar_icon: mdi:server
    module_url: /local/panel-redirect.js
    config:
      # Where you want to redirect to
      target: /config/server_control

  - name: panel-redirect
    # url_path needs to be unique for each panel_custom config
    url_path: redirect-supervisor
    sidebar_title: Supervisor
    sidebar_icon: mdi:home-assistant
    module_url: /local/panel-redirect.js
    config:
      # Where you want to redirect to
      target: /hassio/dashboard

@jonk999
Copy link

jonk999 commented Dec 16, 2021

@danielbrunt57 Thanks for checking. I tried again on current build I'm on. Created folder, make the JS file and copied in. This time though I started a new file in Notepad++ and only copied lines 19-45 rather than download the raw file and upload that. Then updated config yaml and restarted HA. This time it worked. So not sure what the issue was previously.
When I have some time I might look at upgrading to latest version again and try and figure out what's happening with the light entities and try this again.
Cheers.

@danielbrunt57
Copy link

I use LocalTuya and only have a Warning at startup, otherwise no issues with it:
Entity switch.plug_1 (<class 'custom_components.localtuya.switch.LocaltuyaSwitch'>) implements device_state_attributes. Please report it to the custom component author.

@Protoncek
Copy link

Well, if you uploaded whole downloaded file first time than this was the reason…. That file also contains configuration lines and i guess that lines did a mess…
But why not using HACS instead? Installing in there is sooooo easier…

@jonk999
Copy link

jonk999 commented Dec 16, 2021

@Protoncek I assumed the lines at the top with example config were in a comment and so wouldn't have had any effect. Guess I was wrong.
Never really looked into HACS as haven't really had a reason to.

@danielbrunt57
Copy link

danielbrunt57 commented Dec 16, 2021

My panel-redirect.js includes the comment and it works fine as provided above.

@danielbrunt57
Copy link

danielbrunt57 commented Dec 16, 2021

But why not using HACS instead? Installing in there is sooooo easier…

How is this panel-redirect.js installable via HACS??

@balloob
Copy link
Author

balloob commented Dec 17, 2021

The file in this gist is valid JavaScript and works when copied as-is.

@Protoncek
Copy link

Protoncek commented Dec 17, 2021

How is this panel-redirect.js installable via HACS??

You're correct. In this case plugin is not installable via HACS. But most are, and in that case it's so much easier...

My panel-redirect.js includes the comment and it works fine as provided above.

That's interesting... well, i can say that i did have problems once when i copied some files via samba plugin from windows explorer - that plugin didn't work that way. When i deleted those files and copied them via HA file editor it worked... (something with rights, i assume)

@danielbrunt57
Copy link

Yeah, might have been file rights but could possibly be LF translating to CRLF?
I often ssh into HA and use command line and vi editor, which I learned how to use in unix 30 yrs ago. Copy the raw code and paste it into vi. Have also used filezilla and ftp. If you have a web url to the file you can use linux wget. HA file editor is simple and straight forward though and hasn't failed me either...

@Protoncek
Copy link

Yeah, might have been file rights but could possibly be LF translating to CRLF?

Could be... i just remembered: in my company we have an old funky mail client and if our partner sends me some SQL code to execute then just copy/paste from my email client gives me errors in SQL developer. I must manually delete each line break and hit enter again (create all new line breaks) before i hit F8.
So, yes.. either Linux or File Editor. Studio Code Server in HA also works.

@ReenigneArcher
Copy link

Thank you! This should be added to HACS!

@KennethLavrsen
Copy link

KennethLavrsen commented Dec 22, 2021

Great solution to avoid the many clicks you otherwise need to do to do the most common of tasks.

Is there a way to control the sequence of the custome panels? Or panels in general. I have just added 2 (using solution from Daniel) but I cannot find a way to change where in the left panel they appear. I'd like to bundle them with the other admin type items in the bottom.

EDIT - I found that I can drag them down in the UI but not beyond the ones that are already hard coded to be at the bottom. Long click in the header of the left bar and then drag the items down.

@danielbrunt57
Copy link

Go to your profile...
image

and then Edit...
image

You can't move Developer Tools or Configuration but everything else just drag them to the order you desire.

@danielbrunt57
Copy link

I did not know about the long click!

@lallinger
Copy link

lallinger commented Jan 24, 2022

for anyon also having issues to getting this work: check with the visual studio code add on if the line endings are LF (bottom right corner), if they are CRLF change them to LF. Also it might be neccessary to hard refresh your home assistant tab if you had a not working config before (ctrl+r), but i'm not sure if this was neccessary, but the combination of both did the trick for me

Furthermore i just figured: you do not need this script to add a already existing panel to the sidebar. Just go to the page you want to add to the sidebar copy the path from the url and set this as name, for example for supervisor:

panel_custom:
  - name: hassio/dashboard
    sidebar_title: Supervisor
    sidebar_icon: mdi:home-assistant
    module_url: any text

this also has the advantage that your sidebar highlight will be correct as you are not being redirected

@schmurtzm
Copy link

Really useful trick thank you !
However it still a very strange idea to remove the Supervisor shortcut from the sidebar because this is the most useful shortcut and the biggest different between Home Assistant and Home Assistant Core...

It has relayed the add-on store to the abyss of the settings 😅
I think that these shortcuts (at least add-ons and server control) should be simple toggle switches in profile or configuration.

@danielbrunt57
Copy link

Supervisor is still available but is now under Configuration. I am slowly getting used to the new Configuration options but I still find myself using this 1-click Supervisor panel trick rather than the 2-click Configuration > Add-ons, Backups & Supervisor...

image

@Protoncek
Copy link

Yes, it is available under Configuration, but anyone who "tinker" a lot with HA use Supervisor quite often, so having it in sidebar is really a must (for me anyway). I also added server control. When programming HA restart is often required, so this shortcut is also a must.

I agree that this should be made as option to simple turn it on or off in settings (same like auto-hide sidebar).

@tomlut
Copy link

tomlut commented Jun 14, 2022

Is there any advantage over using this?

panel_custom:
  - name: server_logs
    sidebar_title: Logs
    sidebar_icon: mdi:cog-transfer
    js_url: /api/hassio/app/entrypoint.js
    url_path: "config/logs"
    embed_iframe: true
    require_admin: true
    config:
      ingress: core_configurator
  - name: automation
    sidebar_title: Automations
    sidebar_icon: mdi:cog-transfer
    js_url: /api/hassio/app/entrypoint.js
    url_path: "config/automation/dashboard"
    embed_iframe: true
    require_admin: true
    config:
      ingress: core_configurator
  - name: states
    sidebar_title: States
    sidebar_icon: mdi:cog-transfer
    js_url: /api/hassio/app/entrypoint.js
    url_path: "developer-tools/state"
    embed_iframe: true
    require_admin: true
    config:
      ingress: core_configurator

@ndbroadbent
Copy link

I figured out how to add a sidebar menu item to restart Home Assistant with one click. I wrote some custom JS that checks for ?shouldRestart=1 in the URL. If this is found, then it calls the homeassistant.restart service via the websocket connection (after clearing the query param so it doesn't keep restarting in a loop.) If the JS doesn't work for any reason, then you still end up on the developer-tools/yaml page where you can click the Restart button manually.

  • config/configuration.yaml:
frontend:
  extra_module_url:
    - /local/custom.js

panel_custom:
  - name: ha_restart
    sidebar_title: Restart
    sidebar_icon: mdi:restart
    js_url: /api/hassio/app/entrypoint.js
    url_path: 'developer-tools/yaml?shouldRestart=1'
    embed_iframe: true
    require_admin: true
    config:
      ingress: core_configurator
  • config/www/custom.js:
// We need a 'locationchange' event
(function () {
  const pushState = history.pushState;
  history.pushState = function () {
    pushState.apply(history, arguments);
    window.dispatchEvent(new Event("pushstate"));
    window.dispatchEvent(new Event("locationchange"));
  };
})();

function checkShouldRestart() {
  const urlSearchParams = new URLSearchParams(location.search);
  if (urlSearchParams.get("shouldRestart") !== "1") {
    return;
  }
  console.warn(
    "Found ?shouldRestart=1 in URL, restarting Home Assistant Core..."
  );
  // Clear shouldRestart parameter from URL
  const newUrl = new URL(document.location.href);
  newUrl.search = "";
  history.pushState({}, "", newUrl);

  hassConnection.then(({ conn }) => {
    console.warn("Sending restart command...");
    conn.socket.send(
      JSON.stringify({
        type: "execute_script",
        sequence: [
          {
            service: "homeassistant.restart",
            data: {},
          },
        ],
        id: ++conn.commandId,
      })
    );
  });
}

window.addEventListener("locationchange", checkShouldRestart);
checkShouldRestart();

@danielbrunt57
Copy link

The JS did not work for me...

@jareau
Copy link

jareau commented Mar 10, 2023

I have panel-redirect.js working well in Home Assistant 2023.3.3.

I noticed https://www.home-assistant.io/integrations/panel_iframe/ shows an optional require_admin: true parameter that limits the link to Admin Users.

Is require_admin: true possible in panel-redirect.js?

@ncodee
Copy link

ncodee commented Dec 28, 2023

Why isn't this implemented in the UI already?

@danielbrunt57
Copy link

danielbrunt57 commented Dec 29, 2023

Why isn't this implemented in the UI already?

Maybe one day but today is only Stardate 270457.6 BT (BT is Before Trek so that's -270457.6 before Stardate 00000.0)

@MikeGuest
Copy link

Hi - any clues anyone please? I'm lost.

I want to add an additional Settings menu to my sidebar. The existing one can't be moved to the top of the screen. I want all my most frequently used menu items on the first page to avoid too much scrolling. I'd like to do the same with the development tools too.

I'm set up like this, but the link persists in redirecting the url to /lovelace/0 or /lovelace/dashboard

panel_custom:
  - name: settings
    # url_path needs to be unique for each panel_custom config
    url_path: ***********.duckdns.org:8123/config
    sidebar_title: Settings
    sidebar_icon: mdi:cog
    module_url: /local/panel-redirect.js
    config:
      # Where you want to redirect to
      target: /config

@tomlut
Copy link

tomlut commented Mar 5, 2024

The existing one can't be moved to the top of the screen

All of them can be moved. Long click the menu title.

@MikeGuest
Copy link

The existing one can't be moved to the top of the screen

All of them can be moved. Long click the menu title.

Nope - settings and developer tools are pinned and cannot be moved - at least on my installation

@tomlut
Copy link

tomlut commented Mar 8, 2024

Ah, sorry. Yes you are correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment