Files
raucao 11f8d3f049 WIP: Add calendar settings and a nested modal stack
Add a 'days with events' preference (default 3, range 1-7) that drives the upcoming events widget, and extend the fetch window to 8 days so the largest window can populate.

Add a Calendar settings pane (moved under Integrations) that lists the connected CalDAV/ICS sources in a two-column grid with add/edit/remove, and remove calendars from Manage Integrations.

Turn the modal service into a stack so add/edit forms open on top of the settings dialog, with one dialog per level, per-entry title ids, and a softened stacked backdrop.

Move CALENDAR_ACTOR into its own config module to break the calendar/caldav/ics import cycle.
2026-11-06 15:48:08 +04:00

263 lines
8.0 KiB
Plaintext

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { on } from '@ember/modifier';
import {
HomeAssistantAuthError,
HomeAssistantConnectionError,
} from '../../../services/home-assistant';
import {
normalizeHomeAssistantUrl,
profileUrl,
integrationKey,
} from '../../../config/home-assistant';
export default class SettingsIntegrationsHomeAssistantForm extends Component {
@service homeAssistant;
@service notifications;
@service sites;
@service storage;
@tracked urlDraft = null;
@tracked nameDraft = null;
@tracked token = '';
@tracked error = null;
@tracked submitting = false;
get integration() {
return this.args.props?.integration;
}
get isEdit() {
return this.args.props?.mode === 'edit';
}
get url() {
return this.urlDraft ?? this.integration?.url ?? '';
}
get name() {
return this.nameDraft ?? this.integration?.name ?? '';
}
get profileUrl() {
return profileUrl(this.url);
}
get title() {
return this.isEdit ? 'Edit Home Assistant' : 'Add Home Assistant';
}
get submitLabel() {
return this.isEdit ? 'Save' : 'Connect';
}
@action
updateUrl(event) {
this.urlDraft = event.target.value;
this.error = null;
}
@action
updateName(event) {
this.nameDraft = event.target.value;
this.error = null;
}
@action
updateToken(event) {
this.token = event.target.value;
this.error = null;
}
@action
async handleSubmit(event) {
event.preventDefault();
this.error = null;
const instanceUrl = normalizeHomeAssistantUrl(this.url);
if (!instanceUrl) {
this.error = 'Please enter a valid Home Assistant URL';
return;
}
// On edit a blank token keeps the stored one, so users can update the host
// without re-entering the token.
const token = this.token.trim() || this.integration?.token;
if (!token) {
this.error = 'Please enter a long-lived access token';
return;
}
this.submitting = true;
try {
// Opening the socket and reading the counts doubles as a check that the
// token is valid and that the instance serves the WebSocket API.
await this.homeAssistant.fetchSummary({ url: instanceUrl, token });
} catch (err) {
this.error = this.classifyError(err);
return;
} finally {
this.submitting = false;
}
// Key each instance by host so several instances can be connected. Editing
// keeps the existing key even if the host changes.
const key = this.integration?.id ?? integrationKey(instanceUrl);
// A blank name clears the custom label, unlike the token which is kept.
const name = this.name.trim() || undefined;
// Only the credential fields are persisted; the edit row also carries
// display-only fields (avatar, labels) that must not be stored.
await this.storage.set('integrations', key, {
type: 'homeassistant',
status: 'active',
url: instanceUrl,
token,
name,
createdAt: this.integration?.createdAt ?? Date.now(),
});
await this.sites.reload();
this.notifications.startPolling();
this.args.close?.();
}
classifyError(err) {
if (err instanceof HomeAssistantAuthError) {
return 'Invalid access token';
}
if (err instanceof HomeAssistantConnectionError) {
return err.message;
}
return 'Unable to verify your Home Assistant instance';
}
<template>
<div class="p-6">
<h3
id={{if @titleId @titleId "modal-title"}}
class="text-base font-semibold leading-6 text-gray-900"
>{{this.title}}</h3>
<div class="mt-2">
<p class="text-sm text-gray-500">
Connect a Home Assistant instance to see your notification count on
the dashboard.
</p>
</div>
<form {{on "submit" this.handleSubmit}} class="mt-5">
<div class="space-y-5">
<div>
<label
for="homeassistant-url"
class="block text-sm font-medium leading-6 text-gray-900"
>
Server URL
</label>
<div class="mt-2">
<input
type="text"
inputmode="url"
id="homeassistant-url"
name="url"
value={{this.url}}
{{on "input" this.updateUrl}}
placeholder="homeassistant.example.com:8123"
class="block w-full rounded-md border-0 px-3 py-2 text-sm text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"
/>
</div>
<p class="mt-2 text-sm text-gray-500">
The URL of your Home Assistant instance.
</p>
</div>
<div>
<label
for="homeassistant-token"
class="block text-sm font-medium leading-6 text-gray-900"
>
Long-lived access token
</label>
<div class="mt-2">
<input
type="password"
id="homeassistant-token"
name="token"
autocomplete="off"
value={{this.token}}
{{on "input" this.updateToken}}
placeholder={{if this.isEdit "Leave blank to keep current" ""}}
class="block w-full rounded-md border-0 px-3 py-2 text-sm text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"
/>
</div>
<p class="mt-2 text-sm text-gray-500">
Create a token in
{{#if this.profileUrl}}
<a
href={{this.profileUrl}}
target="_blank"
rel="noopener noreferrer"
class="text-indigo-600 hover:text-indigo-500"
>your instance's profile settings</a>.
{{else}}
your instance's profile settings.
{{/if}}
</p>
</div>
<div>
<label
for="homeassistant-name"
class="block text-sm font-medium leading-6 text-gray-900"
>
Name
</label>
<div class="mt-2">
<input
type="text"
id="homeassistant-name"
name="name"
autocomplete="off"
value={{this.name}}
{{on "input" this.updateName}}
placeholder="Optional"
class="block w-full rounded-md border-0 px-3 py-2 text-sm text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"
/>
</div>
<p class="mt-2 text-sm text-gray-500">
Optional. Shown on the dashboard tile instead of the host name.
</p>
</div>
{{#if this.error}}
<p class="text-sm text-red-600">{{this.error}}</p>
{{/if}}
</div>
<div class="mt-5 sm:mt-6 flex gap-3">
<button
type="submit"
disabled={{this.submitting}}
class="inline-flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:opacity-50"
>
{{this.submitLabel}}
</button>
<button
type="button"
{{on "click" @close}}
class="inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
>
Cancel
</button>
</div>
</form>
</div>
</template>
}