41
src/tools/server/sdk/homeassistant/HomeAssistant.ts
Normal file
41
src/tools/server/sdk/homeassistant/HomeAssistant.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import Consola from 'consola';
|
||||
import { appendPath } from '~/tools/shared/strings';
|
||||
import { entityStateSchema } from './models/EntityState';
|
||||
|
||||
export class HomeAssistant {
|
||||
public readonly basePath: URL;
|
||||
private readonly token: string;
|
||||
|
||||
constructor(url: URL, token: string) {
|
||||
if (!url.pathname.endsWith('/')) {
|
||||
url.pathname += "/";
|
||||
}
|
||||
url.pathname += 'api';
|
||||
this.basePath = url;
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
async getEntityState(entityId: string) {
|
||||
try {
|
||||
const response = await fetch(appendPath(this.basePath, `/states/${entityId}`), {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${this.token}`
|
||||
}
|
||||
});
|
||||
const body = await response.json();
|
||||
if (!response.ok) {
|
||||
return {
|
||||
success: false as const,
|
||||
error: body
|
||||
};
|
||||
}
|
||||
return entityStateSchema.safeParseAsync(body);
|
||||
} catch (err) {
|
||||
Consola.error(`Failed to fetch from '${this.basePath}': ${err}`);
|
||||
return {
|
||||
success: false as const,
|
||||
error: err
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
12
src/tools/server/sdk/homeassistant/models/EntityState.ts
Normal file
12
src/tools/server/sdk/homeassistant/models/EntityState.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
|
||||
export const entityStateSchema = z.object({
|
||||
attributes: z.record(z.union([z.string(), z.number(), z.boolean()])),
|
||||
entity_id: z.string(),
|
||||
last_changed: z.string().pipe(z.coerce.date()),
|
||||
last_updated: z.string().pipe(z.coerce.date()),
|
||||
state: z.string(),
|
||||
});
|
||||
|
||||
export type EntityState = z.infer<typeof entityStateSchema>;
|
||||
@@ -29,6 +29,7 @@ export const boardNamespaces = [
|
||||
'modules/dns-hole-controls',
|
||||
'modules/bookmark',
|
||||
'modules/notebook',
|
||||
'modules/smart-home/entity-state',
|
||||
'widgets/error-boundary',
|
||||
'widgets/draggable-list',
|
||||
'widgets/location',
|
||||
|
||||
@@ -12,3 +12,9 @@ export const trimStringEnding = (original: string, toTrimIfExists: string[]) =>
|
||||
export const firstUpperCase = (str: string) => {
|
||||
return str.charAt(0).toUpperCase() + str.slice(1);
|
||||
};
|
||||
|
||||
export const appendPath = (url: URL, path: string) => {
|
||||
const newUrl = new URL(url);
|
||||
newUrl.pathname += path;
|
||||
return newUrl;
|
||||
}
|
||||
|
||||
20
src/tools/singleton/HomeAssistantSingleton.ts
Normal file
20
src/tools/singleton/HomeAssistantSingleton.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { HomeAssistant } from '../server/sdk/homeassistant/HomeAssistant';
|
||||
|
||||
export class HomeAssistantSingleton {
|
||||
private static _instances: HomeAssistant[] = [];
|
||||
|
||||
public static getOrSet(url: URL, token: string): HomeAssistant {
|
||||
const match = this._instances.find(
|
||||
(instance) =>
|
||||
instance.basePath.hostname === url.hostname && instance.basePath.port === url.port
|
||||
);
|
||||
|
||||
if (!match) {
|
||||
const instance = new HomeAssistant(url, token);
|
||||
this._instances.push(instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user