54 lines
1.8 KiB
JavaScript
54 lines
1.8 KiB
JavaScript
export const HOME_ASSISTANT_ICON_URL = '/img/icons/home-assistant.png';
|
|
|
|
// The bundled mark is a white glyph on a transparent, padded background, so it
|
|
// is rendered on the Home Assistant blue.
|
|
export const HOME_ASSISTANT_ICON_BACKGROUND = '#18bcf2';
|
|
|
|
// Home Assistant is always self-hosted, so there is no default instance. The
|
|
// URL is normalized to a bare origin; installs under a subpath are not
|
|
// supported (the origin is all we keep).
|
|
export function normalizeHomeAssistantUrl(input) {
|
|
let value = (input ?? '').trim();
|
|
|
|
if (!value || /\s/.test(value)) {
|
|
return null;
|
|
}
|
|
|
|
if (!/^https?:\/\//i.test(value)) {
|
|
value = `https://${value}`;
|
|
}
|
|
|
|
try {
|
|
return new URL(value).origin;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// The WebSocket API endpoint. Home Assistant authenticates on the socket with
|
|
// the access token sent in the first message, so no headers are involved and
|
|
// the browser can open it directly.
|
|
export function websocketUrl(instanceUrl) {
|
|
const origin = normalizeHomeAssistantUrl(instanceUrl);
|
|
if (!origin) {
|
|
return null;
|
|
}
|
|
|
|
const socketOrigin = origin.replace(/^http/i, 'ws');
|
|
return `${socketOrigin}/api/websocket`;
|
|
}
|
|
|
|
// Where the user creates a long-lived access token for this instance. On
|
|
// current Home Assistant these live under the profile's Security section.
|
|
export function profileUrl(instanceUrl) {
|
|
const origin = normalizeHomeAssistantUrl(instanceUrl);
|
|
return origin ? `${origin}/profile/security` : null;
|
|
}
|
|
|
|
// Storage key for a connected instance, unique per host (including port) so
|
|
// several Home Assistant instances can be connected side by side.
|
|
export function integrationKey(instanceUrl) {
|
|
const origin = normalizeHomeAssistantUrl(instanceUrl);
|
|
return origin ? `homeassistant-${new URL(origin).host}` : null;
|
|
}
|