-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathsip-user-card.ts
More file actions
153 lines (131 loc) · 4.84 KB
/
Copy pathsip-user-card.ts
File metadata and controls
153 lines (131 loc) · 4.84 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
import { LitElement, html, css } from "lit";
import { customElement, state } from "lit/decorators.js";
import { sipCore } from "./sip-core";
declare global {
interface Window {
customCards?: Array<{ type: string; name: string; preview: boolean; description: string }>;
}
}
interface SIPUserCardConfig {
extension: string;
password: string;
display_name?: string;
}
/**
* A zero-height Lovelace card that forces SIP Core to register with a specific
* Asterisk extension on this device, regardless of which HA user is logged in.
*
* Add to a dashboard's YAML to configure a tablet or kiosk:
*
* @example
* type: custom:sip-user-card
* extension: "100"
* password: "mypassword"
* display_name: "Front Door Panel" # optional
*
* Removing the card from the dashboard clears the override and reverts to
* normal Home Assistant user matching.
*/
@customElement("sip-user-card")
class SIPUserCard extends LitElement {
@state()
private config: SIPUserCardConfig | undefined;
static get styles() {
return css`
:host {
display: block;
/* Zero height — this card is purely a config carrier */
height: 0;
overflow: hidden;
}
.status {
display: flex;
align-items: center;
gap: 6px;
padding: 4px 8px;
font-size: 12px;
color: var(--secondary-text-color);
font-family: var(--paper-font-body1_-_font-family);
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
flex-shrink: 0;
}
.dot.registered {
background-color: var(--label-badge-green, #4caf50);
}
.dot.unregistered {
background-color: var(--label-badge-red, #f44336);
}
`;
}
// Called by HA with the YAML config whenever the dashboard loads or config changes.
setConfig(config: SIPUserCardConfig) {
if (!config.extension) throw new Error("sip-user-card: 'extension' is required");
if (!config.password) throw new Error("sip-user-card: 'password' is required");
// GUARD: Only trigger a re-registration if the credentials actually changed.
// This prevents HA's aggressive Lovelace rendering from spamming the Asterisk server.
const currentForceUser = sipCore.ForceUser;
const configChanged =
!currentForceUser ||
currentForceUser.extension !== config.extension ||
currentForceUser.password !== config.password;
if (configChanged) {
console.info(`sip-user-card: New credentials detected for ${config.extension}. Updating localStorage...`);
// Write to localStorage
sipCore.ForceUser = {
extension: config.extension,
password: config.password,
display_name: config.display_name,
};
// Tell SIP Core to drop the old connection and log in with the new one
sipCore.applyForceUser();
}
this.config = config;
}
static getStubConfig(): SIPUserCardConfig {
return {
extension: "100",
password: "mypassword",
display_name: "Front Door Panel",
};
}
connectedCallback() {
super.connectedCallback();
window.addEventListener("sipcore-update", this.updateHandler);
}
disconnectedCallback() {
super.disconnectedCallback();
window.removeEventListener("sipcore-update", this.updateHandler);
// Credentials remain in localStorage — this device stays registered as
// the forced extension until manually cleared via:
// localStorage.removeItem('sipcore-force-user')
}
private updateHandler = () => {
this.requestUpdate();
};
render() {
const registered = sipCore.registered;
const ext = this.config?.extension ?? "?";
const name = this.config?.display_name ?? ext;
return html`
<div class="status">
<span class="dot ${registered ? "registered" : "unregistered"}"></span>
<span>${name} (${ext}) — ${registered ? "Registered" : "Unregistered"}</span>
</div>
`;
}
// Card height hint for HA layout engine — report 0 so it takes no grid space.
getCardSize() {
return 0;
}
}
window.customCards = window.customCards || [];
window.customCards.push({
type: "sip-user-card",
name: "SIP User Card",
preview: false,
description: "Forces SIP Core to register as a specific Asterisk extension on this device, regardless of the logged-in HA user. Useful for shared tablets and kiosk dashboards.",
});