Add option to disable suspend connection when hidden (#6304)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Bram Kragten 2020-07-02 19:30:27 +02:00 committed by GitHub
parent d03c3ab713
commit 0f2e9f66b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 98 additions and 12 deletions

View File

@ -203,6 +203,7 @@ export const provideHass = (
translationMetadata: translationMetadata as any, translationMetadata: translationMetadata as any,
dockedSidebar: "auto", dockedSidebar: "auto",
vibrate: true, vibrate: true,
suspendWhenHidden: false,
moreInfoEntityId: null as any, moreInfoEntityId: null as any,
// @ts-ignore // @ts-ignore
async callService(domain, service, data) { async callService(domain, service, data) {

View File

@ -11,6 +11,7 @@ import {
} from "../util/register-service-worker"; } from "../util/register-service-worker";
import "./ha-init-page"; import "./ha-init-page";
import "./home-assistant-main"; import "./home-assistant-main";
import { storeState } from "../util/ha-pref-storage";
@customElement("home-assistant") @customElement("home-assistant")
export class HomeAssistantAppEl extends HassElement { export class HomeAssistantAppEl extends HassElement {
@ -55,6 +56,10 @@ export class HomeAssistantAppEl extends HassElement {
import( import(
/* webpackChunkName: "polyfill-web-animations-next" */ "web-animations-js/web-animations-next-lite.min" /* webpackChunkName: "polyfill-web-animations-next" */ "web-animations-js/web-animations-next-lite.min"
); );
this.addEventListener("hass-suspend-when-hidden", (ev) => {
this._updateHass({ suspendWhenHidden: ev.detail.suspend });
storeState(this.hass!);
});
} }
protected updated(changedProps: PropertyValues): void { protected updated(changedProps: PropertyValues): void {
@ -76,8 +81,6 @@ export class HomeAssistantAppEl extends HassElement {
// @ts-ignore // @ts-ignore
this._loadHassTranslations(this.hass!.language, "state"); this._loadHassTranslations(this.hass!.language, "state");
this.addEventListener("unsuspend-app", () => this._onVisible(), false);
document.addEventListener( document.addEventListener(
"visibilitychange", "visibilitychange",
() => this._checkVisibility(), () => this._checkVisibility(),
@ -170,15 +173,17 @@ export class HomeAssistantAppEl extends HassElement {
this._visiblePromiseResolve = resolve; this._visiblePromiseResolve = resolve;
}) })
); );
// We close the connection to Home Assistant after being hidden for 5 minutes if (this.hass!.suspendWhenHidden !== false) {
this._hiddenTimeout = window.setTimeout(() => { // We close the connection to Home Assistant after being hidden for 5 minutes
this._hiddenTimeout = undefined; this._hiddenTimeout = window.setTimeout(() => {
// setTimeout can be delayed in the background and only fire this._hiddenTimeout = undefined;
// when we switch to the tab or app again (Hey Android!) // setTimeout can be delayed in the background and only fire
if (!document.hidden) { // when we switch to the tab or app again (Hey Android!)
this._suspendApp(); if (!document.hidden) {
} this._suspendApp();
}, 300000); }
}, 300000);
}
window.addEventListener("focus", () => this._onVisible(), { once: true }); window.addEventListener("focus", () => this._onVisible(), { once: true });
} }

View File

@ -162,6 +162,10 @@ class PartialPanelResolver extends HassRouterPage {
} }
private _checkVisibility() { private _checkVisibility() {
if (this.hass.suspendWhenHidden === false) {
return;
}
if (document.hidden) { if (document.hidden) {
this._onHidden(); this._onHidden();
} else { } else {

View File

@ -35,6 +35,7 @@ import "./ha-pick-theme-row";
import "./ha-push-notifications-row"; import "./ha-push-notifications-row";
import "./ha-refresh-tokens-card"; import "./ha-refresh-tokens-card";
import "./ha-set-vibrate-row"; import "./ha-set-vibrate-row";
import "./ha-set-suspend-row";
class HaPanelProfile extends LitElement { class HaPanelProfile extends LitElement {
@property() public hass!: HomeAssistant; @property() public hass!: HomeAssistant;
@ -137,6 +138,10 @@ class HaPanelProfile extends LitElement {
></ha-advanced-mode-row> ></ha-advanced-mode-row>
` `
: ""} : ""}
<ha-set-suspend-row
.narrow=${this.narrow}
.hass=${this.hass}
></ha-set-suspend-row>
<div class="card-actions"> <div class="card-actions">
<mwc-button class="warning" @click=${this._handleLogOut}> <mwc-button class="warning" @click=${this._handleLogOut}>

View File

@ -0,0 +1,65 @@
import {
customElement,
html,
LitElement,
property,
TemplateResult,
} from "lit-element";
import { fireEvent, HASSDomEvent } from "../../common/dom/fire_event";
import "../../components/ha-switch";
import type { HaSwitch } from "../../components/ha-switch";
import type { HomeAssistant } from "../../types";
import "./ha-settings-row";
declare global {
// for fire event
interface HASSDomEvents {
"hass-suspend-when-hidden": { suspend: HomeAssistant["suspendWhenHidden"] };
}
// for add event listener
interface HTMLElementEventMap {
"hass-suspend-when-hidden": HASSDomEvent<{
suspend: HomeAssistant["suspendWhenHidden"];
}>;
}
}
@customElement("ha-set-suspend-row")
class HaSetSuspendRow extends LitElement {
@property() 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.profile.suspend.header")}
</span>
<span slot="description">
${this.hass.localize("ui.panel.profile.suspend.description")}
</span>
<ha-switch
.checked=${this.hass.suspendWhenHidden}
@change=${this._checkedChanged}
></ha-switch>
</ha-settings-row>
`;
}
private async _checkedChanged(ev: Event) {
const suspend = (ev.target as HaSwitch).checked;
if (suspend === this.hass.suspendWhenHidden) {
return;
}
fireEvent(this, "hass-suspend-when-hidden", {
suspend,
});
}
}
declare global {
interface HTMLElementTagNameMap {
"ha-set-suspend-row": HaSetSuspendRow;
}
}

View File

@ -47,6 +47,7 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
translationMetadata, translationMetadata,
dockedSidebar: "docked", dockedSidebar: "docked",
vibrate: true, vibrate: true,
suspendWhenHidden: true,
moreInfoEntityId: null, moreInfoEntityId: null,
hassUrl: (path = "") => new URL(path, auth.data.hassUrl).toString(), hassUrl: (path = "") => new URL(path, auth.data.hassUrl).toString(),
callService: async (domain, service, serviceData = {}) => { callService: async (domain, service, serviceData = {}) => {

View File

@ -2106,6 +2106,10 @@
"header": "Vibrate", "header": "Vibrate",
"description": "Enable or disable vibration on this device when controlling devices." "description": "Enable or disable vibration on this device when controlling devices."
}, },
"suspend": {
"header": "Automatically close connection",
"description": "Should we close the connection to the server after being hidden for 5 minutes?"
},
"push_notifications": { "push_notifications": {
"header": "Push Notifications", "header": "Push Notifications",
"description": "Send notifications to this device.", "description": "Send notifications to this device.",

View File

@ -209,7 +209,7 @@ export interface HomeAssistant {
resources: Resources; resources: Resources;
localize: LocalizeFunc; localize: LocalizeFunc;
translationMetadata: TranslationMetadata; translationMetadata: TranslationMetadata;
suspendWhenHidden: boolean;
vibrate: boolean; vibrate: boolean;
dockedSidebar: "docked" | "always_hidden" | "auto"; dockedSidebar: "docked" | "always_hidden" | "auto";
defaultPanel: string; defaultPanel: string;

View File

@ -5,6 +5,7 @@ const STORED_STATE = [
"selectedTheme", "selectedTheme",
"selectedLanguage", "selectedLanguage",
"vibrate", "vibrate",
"suspendWhenHidden",
"defaultPanel", "defaultPanel",
]; ];
const STORAGE = window.localStorage || {}; const STORAGE = window.localStorage || {};