Enhance project structure and testing capabilities
- Updated .dockerignore to include additional logs and IDE files, improving Docker build efficiency. - Added .eslintrc.json for TypeScript linting configuration, ensuring code quality and consistency. - Refactored Dockerfile to streamline the build process and utilize a slimmer Node.js image. - Introduced jest-resolver.cjs and jest.setup.js for improved Jest testing configuration and setup. - Updated jest.config.js to support ESM and added new test patterns for better test organization. - Enhanced TypeScript schemas to include new device types (media_player, fan, lock, vacuum, scene, script, camera) for comprehensive validation. - Added unit tests for device schemas and Home Assistant connection, improving test coverage and reliability. - Updated README.md with new testing instructions and device control examples, enhancing user guidance.
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
import { CreateApplication, TServiceParams, StringConfig } from "@digital-alchemy/core";
|
||||
import { LIB_HASS, PICK_ENTITY } from "@digital-alchemy/hass";
|
||||
import { CreateApplication, TServiceParams, ServiceFunction } from "@digital-alchemy/core";
|
||||
import { LIB_HASS } from "@digital-alchemy/hass";
|
||||
import { DomainSchema } from "../schemas.js";
|
||||
import { HASS_CONFIG } from "../config/hass.config.js";
|
||||
|
||||
type Environments = "development" | "production" | "test";
|
||||
|
||||
// Define the type for Home Assistant services
|
||||
type HassServiceMethod = (data: Record<string, unknown>) => Promise<void>;
|
||||
|
||||
type HassServices = {
|
||||
[K in keyof typeof DomainSchema.Values]: {
|
||||
[service: string]: (data: Record<string, any>) => Promise<void>;
|
||||
[service: string]: HassServiceMethod;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -17,17 +19,55 @@ interface HassInstance extends TServiceParams {
|
||||
services: HassServices;
|
||||
}
|
||||
|
||||
// Configuration type for application with more specific constraints
|
||||
type ApplicationConfiguration = {
|
||||
NODE_ENV: ServiceFunction<Environments>;
|
||||
};
|
||||
|
||||
// Strict configuration type for Home Assistant
|
||||
type HassConfiguration = {
|
||||
BASE_URL: {
|
||||
type: "string";
|
||||
description: string;
|
||||
required: true;
|
||||
default: string;
|
||||
};
|
||||
TOKEN: {
|
||||
type: "string";
|
||||
description: string;
|
||||
required: true;
|
||||
default: string;
|
||||
};
|
||||
SOCKET_URL: {
|
||||
type: "string";
|
||||
description: string;
|
||||
required: true;
|
||||
default: string;
|
||||
};
|
||||
SOCKET_TOKEN: {
|
||||
type: "string";
|
||||
description: string;
|
||||
required: true;
|
||||
default: string;
|
||||
};
|
||||
};
|
||||
|
||||
// application
|
||||
const MY_APP = CreateApplication({
|
||||
const MY_APP = CreateApplication<ApplicationConfiguration, {}>({
|
||||
configuration: {
|
||||
NODE_ENV: {
|
||||
type: "string",
|
||||
default: "development",
|
||||
enum: ["development", "production", "test"],
|
||||
description: "Code runner addon can set with it's own NODE_ENV",
|
||||
} satisfies StringConfig<Environments>,
|
||||
},
|
||||
},
|
||||
services: {
|
||||
NODE_ENV: () => {
|
||||
// Directly return the default value or use process.env
|
||||
return (process.env.NODE_ENV as Environments) || "development";
|
||||
}
|
||||
},
|
||||
services: {},
|
||||
libraries: [
|
||||
{
|
||||
...LIB_HASS,
|
||||
@@ -62,12 +102,15 @@ const MY_APP = CreateApplication({
|
||||
name: 'hass' as const
|
||||
});
|
||||
|
||||
let hassInstance: HassInstance;
|
||||
let hassInstance: HassInstance | null = null;
|
||||
|
||||
export async function get_hass(): Promise<HassInstance> {
|
||||
if (!hassInstance) {
|
||||
// Safely get configuration keys, providing an empty object as fallback
|
||||
const _sortedConfigKeys = Object.keys(MY_APP.configuration ?? {}).sort();
|
||||
|
||||
const instance = await MY_APP.bootstrap();
|
||||
hassInstance = instance as unknown as HassInstance;
|
||||
hassInstance = instance as HassInstance;
|
||||
}
|
||||
return hassInstance;
|
||||
}
|
||||
Reference in New Issue
Block a user