-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add HTTP server settings to the network panel #51981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MindFreeze
wants to merge
8
commits into
port-8123
Choose a base branch
from
http-config-ui
base: port-8123
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
40fa954
Fix focus loss in ha-input-multi when items change
MindFreeze 75a2331
Add HTTP server settings to the network panel
MindFreeze 18268c6
Surface fetch errors and validate before saving in HTTP config form
MindFreeze 94bc9d8
Update src/panels/config/network/ha-config-http-form.ts
MindFreeze 414d84b
Group HTTP form fields into collapsible sections
MindFreeze 770254b
Add bottom margin to ha-input-multi add button
MindFreeze 0c188a8
Only apply add-button margin when helper text is present
MindFreeze 4908936
Update HTTP config form to new WebSocket API
MindFreeze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import type { HomeAssistant } from "../types"; | ||
|
|
||
| export interface HttpConfig { | ||
| server_host?: string[]; | ||
| server_port?: number; | ||
| ssl_certificate?: string; | ||
| ssl_peer_certificate?: string; | ||
| ssl_key?: string; | ||
| cors_allowed_origins?: string[]; | ||
| use_x_forwarded_for?: boolean; | ||
| trusted_proxies?: string[]; | ||
| use_x_frame_options?: boolean; | ||
| ip_ban_enabled?: boolean; | ||
| login_attempts_threshold?: number; | ||
| ssl_profile?: "modern" | "intermediate"; | ||
| } | ||
|
|
||
| interface HttpConfigResponse { | ||
| config: HttpConfig; | ||
| } | ||
|
|
||
| export const fetchHttpConfig = (hass: HomeAssistant) => | ||
| hass.callWS<HttpConfigResponse>({ type: "http/config/get" }); | ||
|
|
||
| export const saveHttpConfig = (hass: HomeAssistant, config: HttpConfig) => | ||
| hass.callWS<HttpConfigResponse>({ | ||
| type: "http/config/update", | ||
| config, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| import type { CSSResultGroup, PropertyValues } from "lit"; | ||
| import { css, html, LitElement, nothing } from "lit"; | ||
| import { customElement, property, state } from "lit/decorators"; | ||
| import memoizeOne from "memoize-one"; | ||
| import type { LocalizeFunc } from "../../../common/translations/localize"; | ||
| import "../../../components/ha-alert"; | ||
| import "../../../components/ha-button"; | ||
| import "../../../components/ha-card"; | ||
| import "../../../components/ha-form/ha-form"; | ||
| import type { SchemaUnion } from "../../../components/ha-form/types"; | ||
| import { fetchHttpConfig, saveHttpConfig } from "../../../data/http"; | ||
| import type { HttpConfig } from "../../../data/http"; | ||
| import { showRestartDialog } from "../../../dialogs/restart/show-dialog-restart"; | ||
| import { haStyle } from "../../../resources/styles"; | ||
| import type { HomeAssistant } from "../../../types"; | ||
|
|
||
| const SCHEMA = memoizeOne( | ||
| (localize: LocalizeFunc) => | ||
| [ | ||
| { | ||
| name: "server_port", | ||
| required: true, | ||
| selector: { number: { min: 1, max: 65535, mode: "box" } }, | ||
| }, | ||
| { | ||
| name: "server_host", | ||
| selector: { text: { multiple: true } }, | ||
| }, | ||
| { | ||
| name: "ssl_certificate", | ||
| selector: { text: {} }, | ||
| }, | ||
| { | ||
| name: "ssl_key", | ||
| selector: { text: {} }, | ||
| }, | ||
| { | ||
| name: "ssl_peer_certificate", | ||
| selector: { text: {} }, | ||
| }, | ||
| { | ||
| name: "ssl_profile", | ||
| selector: { | ||
| select: { | ||
| options: [ | ||
| { | ||
| value: "modern", | ||
| label: localize( | ||
| "ui.panel.config.network.http.ssl_profile_modern" | ||
| ), | ||
| }, | ||
| { | ||
| value: "intermediate", | ||
| label: localize( | ||
| "ui.panel.config.network.http.ssl_profile_intermediate" | ||
| ), | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "cors_allowed_origins", | ||
| selector: { text: { multiple: true } }, | ||
| }, | ||
| { | ||
| name: "use_x_forwarded_for", | ||
| selector: { boolean: {} }, | ||
| }, | ||
| { | ||
| name: "trusted_proxies", | ||
| selector: { text: { multiple: true } }, | ||
| }, | ||
| { | ||
| name: "use_x_frame_options", | ||
| selector: { boolean: {} }, | ||
| }, | ||
| { | ||
| name: "ip_ban_enabled", | ||
| selector: { boolean: {} }, | ||
| }, | ||
| { | ||
| name: "login_attempts_threshold", | ||
| required: true, | ||
| selector: { number: { min: -1, max: 1000, mode: "box" } }, | ||
| }, | ||
| ] as const | ||
| ); | ||
|
|
||
| @customElement("ha-config-http-form") | ||
| class HaConfigHttpForm extends LitElement { | ||
| @property({ attribute: false }) public hass!: HomeAssistant; | ||
|
|
||
| @state() private _config?: HttpConfig; | ||
|
|
||
| @state() private _error?: string; | ||
|
|
||
| @state() private _fieldErrors: Record<string, string> = {}; | ||
|
|
||
| @state() private _saving = false; | ||
|
|
||
| @state() private _saved = false; | ||
|
|
||
| protected override firstUpdated(changedProps: PropertyValues<this>) { | ||
| super.firstUpdated(changedProps); | ||
| this._fetchConfig(); | ||
| } | ||
|
|
||
| protected render() { | ||
| if (!this._config) { | ||
| return nothing; | ||
| } | ||
|
|
||
| const schema = SCHEMA(this.hass.localize); | ||
|
|
||
| return html` | ||
| <ha-card | ||
| outlined | ||
| .header=${this.hass.localize("ui.panel.config.network.http.caption")} | ||
| > | ||
| <div class="card-content"> | ||
| <p class="description"> | ||
| ${this.hass.localize("ui.panel.config.network.http.description")} | ||
| </p> | ||
|
|
||
| ${this._error | ||
| ? html`<ha-alert alert-type="error">${this._error}</ha-alert>` | ||
| : nothing} | ||
| ${this._saved | ||
| ? html` | ||
| <ha-alert | ||
| alert-type="success" | ||
|
MindFreeze marked this conversation as resolved.
Outdated
|
||
| .title=${this.hass.localize( | ||
| "ui.panel.config.network.http.restart_required_title" | ||
| )} | ||
| > | ||
| ${this.hass.localize( | ||
| "ui.panel.config.network.http.restart_required_description" | ||
| )} | ||
| <ha-button slot="action" @click=${this._restart}> | ||
| ${this.hass.localize( | ||
| "ui.panel.config.network.http.restart" | ||
| )} | ||
| </ha-button> | ||
| </ha-alert> | ||
| ` | ||
| : nothing} | ||
|
|
||
| <ha-form | ||
| .hass=${this.hass} | ||
| .data=${this._config} | ||
| .schema=${schema} | ||
| .error=${this._fieldErrors} | ||
| .disabled=${this._saving} | ||
| .computeLabel=${this._computeLabel} | ||
| .computeHelper=${this._computeHelper} | ||
| @value-changed=${this._valueChanged} | ||
| ></ha-form> | ||
| </div> | ||
| <div class="card-actions"> | ||
| <ha-button @click=${this._save} .disabled=${this._saving}> | ||
| ${this.hass.localize("ui.panel.config.network.http.save")} | ||
| </ha-button> | ||
| </div> | ||
| </ha-card> | ||
| `; | ||
| } | ||
|
|
||
| private async _fetchConfig(): Promise<void> { | ||
| try { | ||
| const result = await fetchHttpConfig(this.hass); | ||
| this._config = result.config; | ||
| } catch (err: any) { | ||
| this._error = err.message; | ||
| } | ||
| } | ||
|
|
||
| private _computeLabel = ( | ||
| schema: SchemaUnion<ReturnType<typeof SCHEMA>> | ||
| ): string => | ||
| this.hass.localize(`ui.panel.config.network.http.fields.${schema.name}`); | ||
|
|
||
| private _computeHelper = ( | ||
| schema: SchemaUnion<ReturnType<typeof SCHEMA>> | ||
| ): string => | ||
| this.hass.localize(`ui.panel.config.network.http.helpers.${schema.name}`) || | ||
| ""; | ||
|
|
||
| private _valueChanged(ev: CustomEvent): void { | ||
| this._config = ev.detail.value; | ||
| this._saved = false; | ||
| this._error = undefined; | ||
| this._fieldErrors = {}; | ||
| } | ||
|
|
||
| private async _save(): Promise<void> { | ||
| if (!this._config) { | ||
| return; | ||
| } | ||
| this._saving = true; | ||
| this._error = undefined; | ||
| this._fieldErrors = {}; | ||
| try { | ||
| const result = await saveHttpConfig(this.hass, this._config); | ||
|
MindFreeze marked this conversation as resolved.
Outdated
|
||
| this._config = result.config; | ||
| this._saved = true; | ||
| } catch (err: any) { | ||
| // voluptuous formats errors as "<message> @ data['<field>']". | ||
| // If a field is identified, mark it inline; otherwise show a card-level | ||
| // alert. | ||
| const fieldMatch = err.message?.match(/\bdata\['([^']+)'\]/); | ||
| if (fieldMatch) { | ||
| this._fieldErrors = { [fieldMatch[1]]: err.message }; | ||
| } else { | ||
| this._error = err.message; | ||
| } | ||
| } finally { | ||
| this._saving = false; | ||
| } | ||
| await this.updateComplete; | ||
| const haForm = this.renderRoot.querySelector("ha-form"); | ||
| await haForm?.updateComplete; | ||
| // Inline field errors render inside ha-form's shadow root, so fall back to | ||
| // it when no top-level alert is present. | ||
| const target = | ||
| this.renderRoot.querySelector<HTMLElement>("ha-alert") ?? | ||
| haForm?.shadowRoot?.querySelector<HTMLElement>("ha-alert"); | ||
| target?.scrollIntoView({ behavior: "smooth", block: "center" }); | ||
| } | ||
|
|
||
| private _restart(): void { | ||
| showRestartDialog(this); | ||
| } | ||
|
|
||
| static get styles(): CSSResultGroup { | ||
| return [ | ||
| haStyle, | ||
| css` | ||
| .description { | ||
| margin-top: 0; | ||
| color: var(--secondary-text-color); | ||
| } | ||
| ha-alert { | ||
| display: block; | ||
| margin-bottom: var(--ha-space-4); | ||
| } | ||
| .card-actions { | ||
| display: flex; | ||
| gap: var(--ha-space-2); | ||
| justify-content: flex-end; | ||
| } | ||
| `, | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| "ha-config-http-form": HaConfigHttpForm; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.