From 2c17d2fead2e6a47360dacb279cc483f9b8355e2 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 16 Aug 2023 11:52:42 +0200 Subject: [PATCH] Allow to set default router for thread network (#17584) * Allow to set default router for thread network * Update thread-config-panel.ts --- src/data/thread.ts | 11 ++ .../thread/thread-config-panel.ts | 175 ++++++++++++------ src/translations/en.json | 2 + 3 files changed, 131 insertions(+), 57 deletions(-) diff --git a/src/data/thread.ts b/src/data/thread.ts index 2ff0e8b145..cb1c1fa1da 100644 --- a/src/data/thread.ts +++ b/src/data/thread.ts @@ -103,3 +103,14 @@ export const setPreferredThreadDataSet = ( type: "thread/set_preferred_dataset", dataset_id, }); + +export const setPreferredBorderAgent = ( + hass: HomeAssistant, + dataset_id: string, + border_agent_id: string +): Promise => + hass.callWS({ + type: "thread/set_preferred_border_agent_id", + dataset_id, + border_agent_id, + }); diff --git a/src/panels/config/integrations/integration-panels/thread/thread-config-panel.ts b/src/panels/config/integrations/integration-panels/thread/thread-config-panel.ts index ef6ede8662..8faae7bc79 100644 --- a/src/panels/config/integrations/integration-panels/thread/thread-config-panel.ts +++ b/src/panels/config/integrations/integration-panels/thread/thread-config-panel.ts @@ -5,31 +5,35 @@ import { mdiDevices, mdiDotsVertical, mdiInformationOutline, + mdiStar, } from "@mdi/js"; -import { css, html, LitElement, PropertyValues, TemplateResult } from "lit"; +import { LitElement, PropertyValues, TemplateResult, css, html } from "lit"; import { customElement, property, state } from "lit/decorators"; import memoizeOne from "memoize-one"; import { isComponentLoaded } from "../../../../../common/config/is_component_loaded"; import { stringCompare } from "../../../../../common/string/compare"; import { extractSearchParam } from "../../../../../common/url/search-params"; +import "../../../../../components/ha-button-menu"; +import "../../../../../components/ha-list-item"; import "../../../../../components/ha-card"; import { getSignedPath } from "../../../../../data/auth"; import { getConfigEntryDiagnosticsDownloadUrl } from "../../../../../data/diagnostics"; import { - getOTBRInfo, OTBRCreateNetwork, OTBRInfo, OTBRSetChannel, OTBRSetNetwork, + getOTBRInfo, } from "../../../../../data/otbr"; import { + ThreadDataSet, + ThreadRouter, addThreadDataSet, listThreadDataSets, removeThreadDataSet, + setPreferredBorderAgent, setPreferredThreadDataSet, subscribeDiscoverThreadRouters, - ThreadDataSet, - ThreadRouter, } from "../../../../../data/thread"; import { showConfigFlowDialog } from "../../../../../dialogs/config-flow/show-dialog-config-flow"; import { @@ -160,35 +164,47 @@ export class ThreadConfigPanel extends SubscribeMixin(LitElement) { })} - ${network.routers.map( - (router) => - html` - ${router.brand} - ${router.model_name || - router.server?.replace(".local.", "") || - ""} - ${router.server} - ${router.extended_address === this._otbrInfo?.extended_address - ? html` { + const showOverflow = + ("dataset" in network && router.border_agent_id) || + router.extended_address === this._otbrInfo?.extended_address; + return html` + ${router.brand} + ${router.model_name || + router.server?.replace(".local.", "") || + ""} + ${router.server} + ${showOverflow + ? html`${network.dataset && + router.border_agent_id === + network.dataset.preferred_border_agent_id + ? html` + ${this.hass.localize( + "ui.panel.config.thread.default_router" + )} + ` + : ""} + ${this.hass.localize( - "ui.panel.config.thread.reset_border_router" - )}${this.hass.localize( - "ui.panel.config.thread.change_channel" - )}${network.dataset?.preferred - ? "" - : html`${this.hass.localize( - "ui.panel.config.thread.add_to_my_network" - )}`}` - : ""} - ` - )}` + > + ${network.dataset && router.border_agent_id + ? html` + ${router.border_agent_id === + network.dataset.preferred_border_agent_id + ? this.hass.localize( + "ui.panel.config.thread.default_router" + ) + : this.hass.localize( + "ui.panel.config.thread.set_default_router" + )} + ` + : ""} + ${router.extended_address === + this._otbrInfo?.extended_address + ? html` + ${this.hass.localize( + "ui.panel.config.thread.reset_border_router" + )} + + ${this.hass.localize( + "ui.panel.config.thread.change_channel" + )} + ${network.dataset?.preferred + ? "" + : html` + ${this.hass.localize( + "ui.panel.config.thread.add_to_my_network" + )} + `}` + : ""} + ` + : ""} + `; + })}` : html`
${network.dataset?.extended_pan_id && @@ -382,14 +417,23 @@ export class ThreadConfigPanel extends SubscribeMixin(LitElement) { } private _handleRouterAction(ev: CustomEvent) { - switch (ev.detail.index) { + const network = (ev.currentTarget as any).network as ThreadNetwork; + const router = (ev.currentTarget as any).router as ThreadRouter; + const index = + network.dataset && router.border_agent_id + ? Number(ev.detail.index) + : Number(ev.detail.index) + 1; + switch (index) { case 0: - this._resetBorderRouter(); + this._setPreferredBorderAgent(network.dataset!, router); break; case 1: - this._changeChannel(); + this._resetBorderRouter(); break; case 2: + this._changeChannel(); + break; + case 3: this._setDataset(); break; } @@ -452,6 +496,19 @@ export class ThreadConfigPanel extends SubscribeMixin(LitElement) { this._refresh(); } + private async _setPreferredBorderAgent( + dataset: ThreadDataSet, + router: ThreadRouter + ) { + const datasetId = dataset.dataset_id; + const borderAgentId = router.border_agent_id; + if (!borderAgentId) { + return; + } + await setPreferredBorderAgent(this.hass, datasetId, borderAgentId); + this._refresh(); + } + private async _addTLV() { const tlv = await showPromptDialog(this, { title: this.hass.localize("ui.panel.config.thread.add_dataset"), @@ -571,11 +628,15 @@ export class ThreadConfigPanel extends SubscribeMixin(LitElement) { direction: ltr; } ha-list-item.router { + --mdc-list-item-meta-size: auto; + --mdc-list-item-meta-display: flex; --mdc-list-side-padding: 16px; - --mdc-list-item-meta-size: 48px; cursor: default; overflow: visible; } + ha-svg-icon[slot="meta"] { + width: 24px; + } ha-button-menu a { text-decoration: none; } diff --git a/src/translations/en.json b/src/translations/en.json index 116d8f3238..83e28fc48c 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -3707,6 +3707,8 @@ "add_open_thread_border_router": "Add an OpenThread border router", "reset_border_router": "Reset border router", "add_to_my_network": "Add to preferred network", + "default_router": "Default router for network", + "set_default_router": "Set as default router for network", "no_routers_otbr_network": "No border routers where found, maybe the border router is not configured correctly. You can try to reset it to the factory settings.", "add_dataset_from_tlv": "Add dataset from TLV", "add_dataset": "Add Thread dataset",