Skip to content
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

Debug option to enable logging server calls #17956

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/fake_data/provide_hass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export const provideHass = (
},
dockedSidebar: "auto",
vibrate: true,
debugConnection: false,
suspendWhenHidden: false,
moreInfoEntityId: null as any,
// @ts-ignore
Expand Down
52 changes: 52 additions & 0 deletions src/panels/developer-tools/debug/developer-tools-debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { CSSResultGroup, LitElement, css, html } from "lit";
import { customElement, property } from "lit/decorators";
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
import { haStyle } from "../../../resources/styles";
import { HomeAssistant } from "../../../types";
import "./ha-debug-connection-row";

@customElement("developer-tools-debug")
class HaPanelDevDebug extends SubscribeMixin(LitElement) {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ type: Boolean }) public narrow!: boolean;

protected render() {
return html`
<div class="content">
<ha-card
.header=${this.hass.localize(
"ui.panel.developer-tools.tabs.debug.title"
)}
class="form"
>
<div class="card-content">
<ha-debug-connection-row
.hass=${this.hass}
.narrow=${this.narrow}
></ha-debug-connection-row>
</ha-card>
</div>
`;
}

static get styles(): CSSResultGroup {
return [
haStyle,
css`
.content {
padding: 28px 20px 16px;
display: block;
max-width: 600px;
margin: 0 auto;
}
`,
];
}
}

declare global {
interface HTMLElementTagNameMap {
"developer-tools-debug": HaPanelDevDebug;
}
}
50 changes: 50 additions & 0 deletions src/panels/developer-tools/debug/ha-debug-connection-row.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { html, LitElement, TemplateResult } from "lit";
import { customElement, property } from "lit/decorators";
import "../../../components/ha-settings-row";
import "../../../components/ha-switch";
import type { HaSwitch } from "../../../components/ha-switch";
import type { HomeAssistant } from "../../../types";
import { storeState } from "../../../util/ha-pref-storage";

@customElement("ha-debug-connection-row")
class HaDebugConnectionRow extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property() public narrow!: boolean;

protected render(): TemplateResult {
return html`
<ha-settings-row .narrow=${this.narrow}>
<span slot="heading">
${this.hass.localize(
"ui.panel.developer-tools.tabs.debug.debug_connection.title"
)}
</span>
<span slot="description">
${this.hass.localize(
"ui.panel.developer-tools.tabs.debug.debug_connection.description"
)}
</span>
<ha-switch
.checked=${this.hass.debugConnection}
@change=${this._checkedChanged}
></ha-switch>
</ha-settings-row>
`;
}

private async _checkedChanged(ev: Event) {
const debugConnection = (ev.target as HaSwitch).checked;
if (debugConnection === this.hass.debugConnection) {
return;
}
this.hass.debugConnection = debugConnection;
storeState(this.hass);
}
}

declare global {
interface HTMLElementTagNameMap {
"ha-debug-connection-row": HaDebugConnectionRow;
}
}
4 changes: 4 additions & 0 deletions src/panels/developer-tools/developer-tools-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class DeveloperToolsRouter extends HassRouterPage {
tag: "developer-tools-assist",
load: () => import("./assist/developer-tools-assist"),
},
debug: {
tag: "developer-tools-debug",
load: () => import("./debug/developer-tools-debug"),
},
},
};

Expand Down
23 changes: 23 additions & 0 deletions src/panels/developer-tools/ha-panel-developer-tools.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { mdiDotsVertical } from "@mdi/js";
import "@polymer/paper-tabs/paper-tab";
import "@polymer/paper-tabs/paper-tabs";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property } from "lit/decorators";
import type { ActionDetail } from "@material/mwc-list";
import { navigate } from "../../common/navigate";
import "../../components/ha-menu-button";
import "../../components/ha-button-menu";
import "../../components/ha-icon-button";
import "../../components/ha-list-item";
import { haStyle } from "../../resources/styles";
import { HomeAssistant, Route } from "../../types";
import "./developer-tools-router";
Expand Down Expand Up @@ -34,6 +39,16 @@ class PanelDeveloperTools extends LitElement {
<div class="main-title">
${this.hass.localize("panel.developer_tools")}
</div>
<ha-button-menu slot="actionItems" @action=${this._handleMenuAction}>
<ha-icon-button
slot="trigger"
.label=${this.hass.localize("ui.common.menu")}
.path=${mdiDotsVertical}
></ha-icon-button>
<ha-list-item>
${this.hass.localize("ui.panel.developer-tools.tabs.debug.title")}
</ha-list-item>
</ha-button-menu>
</div>
<paper-tabs
scrollable
Expand Down Expand Up @@ -85,6 +100,14 @@ class PanelDeveloperTools extends LitElement {
}
}

private async _handleMenuAction(ev: CustomEvent<ActionDetail>) {
switch (ev.detail.index) {
case 0:
navigate(`/developer-tools/debug`);
break;
}
}

private get _page() {
return this.route.path.substr(1);
}
Expand Down
11 changes: 6 additions & 5 deletions src/state/connection-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
translationMetadata,
dockedSidebar: "docked",
vibrate: true,
debugConnection: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should store this in localStorage, now you will never be able to catch things when the page is refreshed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does including this in ha-pref-storage take care of that? I thought it was working when I tested it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like I'm getting everything I expect to see when hitting refresh with this toggle enabled (and I did remove the DEV part of the equation just to test and be sure).

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, missed that 🙈 yeah pref storage stores that part in local storage!

suspendWhenHidden: true,
enableShortcuts: true,
moreInfoEntityId: null,
Expand All @@ -84,7 +85,7 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
target,
notifyOnError = true
) => {
if (__DEV__) {
if (__DEV__ || this.hass?.debugConnection) {
// eslint-disable-next-line no-console
console.log(
"Calling service",
Expand All @@ -109,7 +110,7 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
) {
return { context: { id: "" } };
}
if (__DEV__) {
if (__DEV__ || this.hass?.debugConnection) {
// eslint-disable-next-line no-console
console.error(
"Error calling service",
Expand Down Expand Up @@ -146,22 +147,22 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
) => fetchWithAuth(auth, `${auth.data.hassUrl}${path}`, init),
// For messages that do not get a response
sendWS: (msg) => {
if (__DEV__) {
if (__DEV__ || this.hass?.debugConnection) {
// eslint-disable-next-line no-console
console.log("Sending", msg);
}
conn.sendMessage(msg);
},
// For messages that expect a response
callWS: <R>(msg) => {
if (__DEV__) {
if (__DEV__ || this.hass?.debugConnection) {
// eslint-disable-next-line no-console
console.log("Sending", msg);
}

const resp = conn.sendMessagePromise<R>(msg);

if (__DEV__) {
if (__DEV__ || this.hass?.debugConnection) {
resp.then(
// eslint-disable-next-line no-console
(result) => console.log("Received", result),
Expand Down
7 changes: 7 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5562,6 +5562,13 @@
"no_match": "No intent matched",
"language": "[%key:ui::components::language-picker::language%]"
},
"debug": {
"title": "Debug tools",
"debug_connection": {
"title": "Debug connection",
"description": "Observe requests to the server and responses from the server in browser console."
}
},
"events": {
"title": "Events",
"description": "Fire an event on the event bus.",
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export interface HomeAssistant {
suspendWhenHidden: boolean;
enableShortcuts: boolean;
vibrate: boolean;
debugConnection: boolean;
dockedSidebar: "docked" | "always_hidden" | "auto";
defaultPanel: string;
moreInfoEntityId: string | null;
Expand Down
1 change: 1 addition & 0 deletions src/util/ha-pref-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const STORED_STATE = [
"selectedTheme",
"selectedLanguage",
"vibrate",
"debugConnection",
"suspendWhenHidden",
"enableShortcuts",
"defaultPanel",
Expand Down
Loading