Skip to content

Commit 2a2599d

Browse files
authored
Add sidebar default visible flag to panels (home-assistant#155506)
1 parent aac25fa commit 2a2599d

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

homeassistant/components/frontend/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ class Panel:
263263
# Title to show in the sidebar
264264
sidebar_title: str | None = None
265265

266+
# If the panel should be visible by default in the sidebar
267+
sidebar_default_visible: bool = True
268+
266269
# Url to show the panel in the frontend
267270
frontend_url_path: str
268271

@@ -280,6 +283,7 @@ def __init__(
280283
component_name: str,
281284
sidebar_title: str | None,
282285
sidebar_icon: str | None,
286+
sidebar_default_visible: bool,
283287
frontend_url_path: str | None,
284288
config: dict[str, Any] | None,
285289
require_admin: bool,
@@ -293,6 +297,7 @@ def __init__(
293297
self.config = config
294298
self.require_admin = require_admin
295299
self.config_panel_domain = config_panel_domain
300+
self.sidebar_default_visible = sidebar_default_visible
296301

297302
@callback
298303
def to_response(self) -> PanelResponse:
@@ -301,6 +306,7 @@ def to_response(self) -> PanelResponse:
301306
"component_name": self.component_name,
302307
"icon": self.sidebar_icon,
303308
"title": self.sidebar_title,
309+
"default_visible": self.sidebar_default_visible,
304310
"config": self.config,
305311
"url_path": self.frontend_url_path,
306312
"require_admin": self.require_admin,
@@ -315,6 +321,7 @@ def async_register_built_in_panel(
315321
component_name: str,
316322
sidebar_title: str | None = None,
317323
sidebar_icon: str | None = None,
324+
sidebar_default_visible: bool = True,
318325
frontend_url_path: str | None = None,
319326
config: dict[str, Any] | None = None,
320327
require_admin: bool = False,
@@ -327,6 +334,7 @@ def async_register_built_in_panel(
327334
component_name,
328335
sidebar_title,
329336
sidebar_icon,
337+
sidebar_default_visible,
330338
frontend_url_path,
331339
config,
332340
require_admin,
@@ -879,6 +887,7 @@ class PanelResponse(TypedDict):
879887
component_name: str
880888
icon: str | None
881889
title: str | None
890+
default_visible: bool
882891
config: dict[str, Any] | None
883892
url_path: str
884893
require_admin: bool

tests/components/frontend/test_init.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ async def test_get_panels(
645645
assert msg["result"]["map"]["icon"] == "mdi:tooltip-account"
646646
assert msg["result"]["map"]["title"] == "Map"
647647
assert msg["result"]["map"]["require_admin"] is True
648+
assert msg["result"]["map"]["default_visible"] is True
648649

649650
async_remove_panel(hass, "map")
650651

@@ -685,6 +686,45 @@ async def test_get_panels_non_admin(
685686
assert "map" not in msg["result"]
686687

687688

689+
async def test_panel_sidebar_default_visible(
690+
hass: HomeAssistant,
691+
hass_ws_client: WebSocketGenerator,
692+
mock_http_client: TestClient,
693+
) -> None:
694+
"""Test sidebar_default_visible property in panels."""
695+
async_register_built_in_panel(
696+
hass,
697+
"default_panel",
698+
"Default Panel",
699+
)
700+
async_register_built_in_panel(
701+
hass,
702+
"visible_panel",
703+
"Visible Panel",
704+
"mdi:eye",
705+
sidebar_default_visible=True,
706+
)
707+
async_register_built_in_panel(
708+
hass,
709+
"hidden_panel",
710+
"Hidden Panel",
711+
"mdi:eye-off",
712+
sidebar_default_visible=False,
713+
)
714+
715+
client = await hass_ws_client(hass)
716+
await client.send_json({"id": 5, "type": "get_panels"})
717+
718+
msg = await client.receive_json()
719+
720+
assert msg["id"] == 5
721+
assert msg["type"] == TYPE_RESULT
722+
assert msg["success"]
723+
assert msg["result"]["default_panel"]["default_visible"] is True
724+
assert msg["result"]["visible_panel"]["default_visible"] is True
725+
assert msg["result"]["hidden_panel"]["default_visible"] is False
726+
727+
688728
async def test_get_translations(ws_client: MockHAClientWebSocket) -> None:
689729
"""Test get_translations command."""
690730
with patch(

tests/components/hassio/test_init.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,7 @@ async def test_setup_api_panel(
253253
"component_name": "custom",
254254
"icon": None,
255255
"title": None,
256-
"url_path": "hassio",
257-
"require_admin": True,
258-
"config_panel_domain": None,
256+
"default_visible": True,
259257
"config": {
260258
"_panel_custom": {
261259
"embed_iframe": True,
@@ -264,6 +262,9 @@ async def test_setup_api_panel(
264262
"trust_external": False,
265263
}
266264
},
265+
"url_path": "hassio",
266+
"require_admin": True,
267+
"config_panel_domain": None,
267268
}
268269

269270

0 commit comments

Comments
 (0)