-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathentity-registry-settings.ts
More file actions
308 lines (284 loc) · 9.6 KB
/
entity-registry-settings.ts
File metadata and controls
308 lines (284 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import type { HassEntity } from "home-assistant-js-websocket";
import type { CSSResultGroup, PropertyValues } from "lit";
import { css, html, LitElement } from "lit";
import { customElement, property, query, state } from "lit/decorators";
import { fireEvent } from "../../../common/dom/fire_event";
import { computeDeviceName } from "../../../common/entity/compute_device_name";
import { computeEntityEntryName } from "../../../common/entity/compute_entity_name";
import { getEntityEntryContext } from "../../../common/entity/context/get_entity_context";
import "../../../components/ha-alert";
import "../../../components/ha-button";
import type { ConfigEntry } from "../../../data/config_entries";
import {
deleteConfigEntry,
getConfigEntry,
} from "../../../data/config_entries";
import { updateDeviceRegistryEntry } from "../../../data/device/device_registry";
import type { ExtEntityRegistryEntry } from "../../../data/entity/entity_registry";
import {
removeEntityRegistryEntry,
updateEntityRegistryEntry,
} from "../../../data/entity/entity_registry";
import { fetchIntegrationManifest } from "../../../data/integration";
import {
showAlertDialog,
showConfirmationDialog,
} from "../../../dialogs/generic/show-dialog-box";
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
import { haStyle } from "../../../resources/styles";
import type { HomeAssistant } from "../../../types";
import { showDeviceRegistryDetailDialog } from "../devices/device-registry-detail/show-dialog-device-registry-detail";
import "./entity-registry-settings-editor";
import type { EntityRegistrySettingsEditor } from "./entity-registry-settings-editor";
import { getDeleteConfirmationText } from "./get-delete-confirmation-text";
@customElement("entity-registry-settings")
export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
@property({ attribute: false }) public hass!: HomeAssistant;
@property({ type: Object }) public entry!: ExtEntityRegistryEntry;
@state() private _helperConfigEntry?: ConfigEntry;
@state() private _error?: string;
@state() private _submitting?: boolean;
@state() private _dirty = false;
@query("entity-registry-settings-editor")
private _registryEditor?: EntityRegistrySettingsEditor;
protected willUpdate(changedProps: PropertyValues<this>): void {
super.willUpdate(changedProps);
if (changedProps.has("entry")) {
this._fetchHelperConfigEntry();
}
}
private async _fetchHelperConfigEntry() {
this._helperConfigEntry = undefined;
if (!this.entry?.config_entry_id) {
return;
}
try {
const configEntry = (
await getConfigEntry(this.hass, this.entry.config_entry_id)
).config_entry;
const manifest = await fetchIntegrationManifest(
this.hass,
configEntry.domain
);
if (manifest.integration_type === "helper") {
this._helperConfigEntry = configEntry;
}
// eslint-disable-next-line no-empty
} catch (_err) {}
}
protected render() {
const stateObj: HassEntity | undefined =
this.hass.states[this.entry.entity_id];
const device = this.entry.device_id
? this.hass.devices[this.entry.device_id]
: undefined;
return html`
${!stateObj
? html`
<ha-alert alert-type="warning">
${device?.disabled_by
? html`${this.hass!.localize(
"ui.dialogs.entity_registry.editor.device_disabled"
)}<ha-button
size="small"
variant="warning"
@click=${this._openDeviceSettings}
slot="action"
>
${this.hass!.localize(
"ui.dialogs.entity_registry.editor.open_device_settings"
)}
</ha-button>`
: this.entry.disabled_by
? html`${this.hass!.localize(
"ui.dialogs.entity_registry.editor.entity_disabled"
)}${["user", "integration"].includes(
this.entry.disabled_by!
)
? html`<ha-button
size="small"
variant="warning"
slot="action"
@click=${this._enableEntry}
>
${this.hass!.localize(
"ui.dialogs.entity_registry.editor.enable_entity"
)}</ha-button
>`
: ""}`
: this.hass!.localize(
"ui.dialogs.entity_registry.editor.unavailable"
)}
</ha-alert>
`
: ""}
${this._error
? html`<ha-alert alert-type="error">${this._error}</ha-alert>`
: ""}
<div class="form container">
<entity-registry-settings-editor
.hass=${this.hass}
.entry=${this.entry}
.helperConfigEntry=${this._helperConfigEntry}
.disabled=${!!this._submitting}
@change=${this._entityRegistryChanged}
></entity-registry-settings-editor>
</div>
<div class="buttons">
<ha-button
variant="danger"
appearance="plain"
@click=${this._confirmDeleteEntry}
.disabled=${this._submitting ||
(!this._helperConfigEntry && !stateObj?.attributes.restored)}
>
${this.hass.localize("ui.dialogs.entity_registry.editor.delete")}
</ha-button>
<ha-button
@click=${this._updateEntry}
.disabled=${!this._dirty || !!this._submitting}
.loading=${!!this._submitting}
>
${this.hass.localize("ui.dialogs.entity_registry.editor.update")}
</ha-button>
</div>
`;
}
private _entityRegistryChanged() {
this._error = undefined;
this._dirty = this._registryEditor?.dirty ?? false;
}
private _openDeviceSettings() {
const device = this.hass.devices[this.entry.device_id!];
showDeviceRegistryDetailDialog(this, {
device: device,
updateEntry: async (updates) => {
await updateDeviceRegistryEntry(this.hass, device.id, updates);
},
});
}
private async _enableEntry() {
this._error = undefined;
this._submitting = true;
try {
const result = await updateEntityRegistryEntry(
this.hass!,
this.entry.entity_id,
{ disabled_by: null }
);
fireEvent(this, "entity-entry-updated", result.entity_entry);
if (result.require_restart) {
showAlertDialog(this, {
text: this.hass.localize(
"ui.dialogs.entity_registry.editor.enabled_restart_confirm"
),
});
}
if (result.reload_delay) {
showAlertDialog(this, {
text: this.hass.localize(
"ui.dialogs.entity_registry.editor.enabled_delay_confirm",
{ delay: result.reload_delay }
),
});
}
} catch (err: any) {
this._error = err.message;
} finally {
this._submitting = false;
}
}
private async _updateEntry(): Promise<void> {
this._submitting = true;
try {
const result = await this._registryEditor!.updateEntry();
if (result.close) {
fireEvent(this, "close-dialog");
}
} catch (err: any) {
this._error = err.message || "Unknown error";
} finally {
this._submitting = false;
}
}
private async _confirmDeleteEntry(): Promise<void> {
let name = computeEntityEntryName(this.entry, this.hass.devices);
if (!name) {
const { device } = getEntityEntryContext(
this.entry,
this.hass.entities,
this.hass.devices,
this.hass.areas,
this.hass.floors
);
if (device) {
name = computeDeviceName(device);
}
}
const confirmationText = await getDeleteConfirmationText(
this.hass,
this.entry,
name
);
if (
!(await showConfirmationDialog(this, {
title: this.hass.localize(
"ui.dialogs.entity_registry.editor.confirm_delete_title"
),
text: confirmationText,
confirmText: this.hass.localize("ui.common.delete"),
dismissText: this.hass.localize("ui.common.cancel"),
destructive: true,
}))
) {
return;
}
this._submitting = true;
try {
if (this._helperConfigEntry) {
await deleteConfigEntry(this.hass, this._helperConfigEntry.entry_id);
} else {
await removeEntityRegistryEntry(this.hass!, this.entry.entity_id);
}
fireEvent(this, "close-dialog");
} finally {
this._submitting = false;
}
}
static get styles(): CSSResultGroup {
return [
haStyle,
css`
:host {
display: block;
}
.container {
padding: var(--ha-space-2) var(--ha-space-6) var(--ha-space-5)
var(--ha-space-6);
}
.buttons {
box-sizing: border-box;
display: flex;
padding: var(--ha-space-4);
justify-content: space-between;
padding-bottom: max(var(--safe-area-inset-bottom), var(--ha-space-4));
background-color: var(--mdc-theme-surface, #fff);
border-top: 1px solid var(--divider-color);
position: sticky;
bottom: 0px;
}
ha-alert ha-button {
width: max-content;
}
`,
];
}
}
declare global {
interface HTMLElementTagNameMap {
"entity-registry-settings": EntityRegistrySettings;
}
interface HASSDomEvents {
"entity-entry-updated": ExtEntityRegistryEntry;
}
}