From a3641e28457f9b13f5a6fec2e66e364b6845204c Mon Sep 17 00:00:00 2001 From: Tom McKenzie Date: Thu, 25 Jan 2024 21:31:37 +1100 Subject: [PATCH] homebridge-automation forked from ai --- .vscode/settings.json | 6 ++++-- README.md | 12 +++--------- config.schema.json | 38 ++++++++++++++++++++++++++++++-------- package-lock.json | 4 ++-- package.json | 22 +++++++--------------- src/index.ts | 4 ++-- src/platform.ts | 39 +++++++++++++++++++++------------------ src/settings.ts | 2 +- src/test.ts | 4 ++-- tsconfig.json | 15 +++------------ 10 files changed, 75 insertions(+), 71 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 7bf0551..b9d35df 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,10 +1,12 @@ { "files.eol": "\n", "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "editor.rulers": [140], "eslint.enable": true, "editor.formatOnSave": true, - "editor.formatOnType": true + "editor.formatOnType": true, + "spellright.language": [], + "spellright.documentTypes": ["latex", "plaintext"] } diff --git a/README.md b/README.md index 8f4f9fa..ba89ba5 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,6 @@ -

+# homebridge-automation - + AI - -

- -# homebridge-ai - -Control your homebridge instance with AI. Check it out at https://homebridgeai.com/ +Control your homebridge instance with Javascript functions. ## Local testing @@ -17,7 +11,7 @@ Simply run `npm run watch` to start. By default it uses the config at `~/.homebr This runs segregated from your local network for more specific testing scenarios. ```shell -docker run --name=homebridge-ai -p 18581:8581 -v $(pwd)/homebridge:/homebridge -v $(pwd):/var/lib/homebridge-ai homebridge/homebridge:latest; docker rm homebridge-ai +docker run --name=homebridge-automation -p 18581:8581 -v $(pwd)/homebridge:/homebridge -v $(pwd):/var/lib/homebridge-automation homebridge/homebridge:latest; docker rm homebridge-automation ``` Then open homebridge UI: http://localhost:18581/ diff --git a/config.schema.json b/config.schema.json index 3d4c251..59af389 100644 --- a/config.schema.json +++ b/config.schema.json @@ -1,25 +1,47 @@ { - "pluginAlias": "HomebridgeAI", + "pluginAlias": "HomebridgeAutomation", "pluginType": "platform", "singular": true, - "headerDisplay": "Welcome to HomebridgeAI. You can learn more at https://homebridgeai.com", + "headerDisplay": "Homebridge Automation settings", "footerDisplay": "", "schema": { "type": "object", "properties": { - "apiKey": { - "title": "API Key", + "automationJs": { + "title": "Automation JS", "type": "string", - "required": true, - "default": "", - "description": "You can find this under \"Your Account\"" + "required": false, + "placeholder": "function (event, act) {}", + "default": "function (event, act) {}", + "description": "Function to run on every change" }, "pin": { "title": "Homebridge PIN", "type": "string", "required": false, "placeholder": "000-00-000", - "description": "Advanced setting. We'll use this instance's PIN by default" + "description": "We'll use this instance's PIN by default" + }, + "remoteEnabled": { + "title": "Remote Control enabled", + "type": "boolean", + "required": true, + "default": false, + "description": "Connect to websocket control server" + }, + "remoteHost": { + "title": "Remote Control host", + "type": "string", + "required": false, + "default": "127.0.0.1", + "description": "Can be overridden by env UPSTREAM_API (does not apply if enabled is not set)" + }, + "apiKey": { + "title": "Remote Control API Key", + "type": "string", + "required": false, + "default": "", + "description": "" }, "disabled_modes": { "condition": { diff --git a/package-lock.json b/package-lock.json index a11e905..02d3d94 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "homebridge-ai", + "name": "homebridge-automation", "version": "0.6.6", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "homebridge-ai", + "name": "homebridge-automation", "version": "0.6.6", "license": "Apache-2.0", "dependencies": { diff --git a/package.json b/package.json index 0f70e55..f31f17c 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,16 @@ { - "displayName": "HomebridgeAI", - "name": "homebridge-ai", - "version": "0.6.6", + "displayName": "Homebridge Automation", + "name": "homebridge-automation", + "version": "0.1.0", "description": "Command and automate your home using natural language from anywhere.", "license": "Apache-2.0", - "homepage": "https://homebridgeai.com/", + "homepage": "https://tommckenzie.dev/homebridge-automation", "repository": { "type": "git", - "url": "https://github.com/grrowl/homebridge-ai.git" + "url": "https://github.com/grrowl/homebridge-plugin-automation.git" }, "bugs": { - "url": "https://github.com/grrowl/homebridge-ai/issues" + "url": "https://github.com/grrowl/homebridge-plugin-automation/issues" }, "engines": { "node": ">=16.20.1", @@ -25,18 +25,10 @@ }, "keywords": [ "homebridge-plugin", - "homebridgeai", - "homekit", - "ai", + "automation", "gpt", "assistant", - "chat", - "remote", - "whatsapp", "shortcuts", - "ios", - "android", - "delay", "automation" ], "dependencies": { diff --git a/src/index.ts b/src/index.ts index 7d66f1a..cf45cb0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,11 @@ import { API } from "homebridge"; -import { HomebridgeAI } from "./platform"; +import { HomebridgeAutomation } from "./platform"; import { PLATFORM_NAME } from "./settings"; /** * This method registers the platform with Homebridge */ export = (api: API) => { - api.registerPlatform(PLATFORM_NAME, HomebridgeAI); + api.registerPlatform(PLATFORM_NAME, HomebridgeAutomation); }; diff --git a/src/platform.ts b/src/platform.ts index be65903..0059511 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -31,7 +31,7 @@ const METRIC_DEBOUNCE = 1_500; // only reset reconnection backoff once the connection is open for 5 seconds const CONNECTION_RESET_DELAY = 5_000; -export class HomebridgeAI implements DynamicPlatformPlugin { +export class HomebridgeAutomation implements DynamicPlatformPlugin { private socket?: WebSocket; private reconnectAttempts = 0; private messageBuffer: string[] = []; @@ -55,14 +55,7 @@ export class HomebridgeAI implements DynamicPlatformPlugin { const pin = config.pin || findConfigPin(this.api); if (!pin) { this.log.error( - "Homebridge-AI requires a PIN to be configured in the config.json", - ); - } - - const apiKey = config.apiKey; - if (!apiKey) { - this.log.error( - "Homebridge-AI requires an API Key to be configured in the config.json", + "HomebridgeAutomation requires a PIN to be configured in the config.json", ); } @@ -96,7 +89,9 @@ export class HomebridgeAI implements DynamicPlatformPlugin { // APIEvent.DID_FINISH_LAUNCHING this.api.on("didFinishLaunching", () => { this.log.info("Launching..."); - this.connectSocket(); + if (config.remoteEnabled) { + this.connectSocket(); + } }); // APIEvent.SHUTDOWN this.api.on("shutdown", () => { @@ -105,12 +100,16 @@ export class HomebridgeAI implements DynamicPlatformPlugin { }); setTimeout(async () => { - this.sendStateUpdate(); + if (!this.config.remoteEnabled) { + this.sendStateUpdate(); + } }, SEND_STATE_DELAY); } connectSocket(): void { - const wsAddress = new URL(`${UPSTREAM_API}`); + const wsAddress = new URL( + UPSTREAM_API ? String(UPSTREAM_API) : this.config.remoteHost, + ); this.log.debug(`Connecting to ${wsAddress}`); this.socket = new WebSocket(wsAddress, { headers: { @@ -229,12 +228,14 @@ export class HomebridgeAI implements DynamicPlatformPlugin { this.hapMonitor.on("service-update", (responses) => { // no need to update this.servicesCache as this is only characteristic updates -- cache will be out of date for characteristic state. - responses.forEach((response) => { - this.sendMessage({ - type: "deviceStatusChange", - data: response, + if (!this.config.remoteEnabled) { + responses.forEach((response) => { + this.sendMessage({ + type: "deviceStatusChange", + data: response, + }); }); - }); + } }); } @@ -374,7 +375,9 @@ export class HomebridgeAI implements DynamicPlatformPlugin { incrementMetric(metric: keyof MetricsData) { this.metrics[metric] = (this.metrics[metric] || 0) + 1; - this.sendMetrics(); + if (!this.config.remoteEnabled) { + this.sendMetrics(); + } } private debounceTimeout?: NodeJS.Timeout; diff --git a/src/settings.ts b/src/settings.ts index eb5c677..566d13e 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,6 +1,6 @@ export const PLATFORM_NAME = "HomebridgeAI"; -export const PLUGIN_NAME = "homebridge-ai"; +export const PLUGIN_NAME = "homebridge-automation"; export const UPSTREAM_API = process.env.AI_UPSTREAM_API || "wss://homebridgeai.com/hb-api"; diff --git a/src/test.ts b/src/test.ts index 0371fad..921f415 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,10 +1,10 @@ import EventEmitter from "node:events"; -import { HomebridgeAI } from "./platform"; +import { HomebridgeAutomation } from "./platform"; import { Logger } from "homebridge/lib/logger"; class FakeAPI extends EventEmitter {} -const instance = new HomebridgeAI( +const instance = new HomebridgeAutomation( new Logger(), {} as any, new FakeAPI() as any, diff --git a/tsconfig.json b/tsconfig.json index bf07099..97b06b2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,12 +2,7 @@ "compilerOptions": { "target": "ES2018", // ~node10 "module": "commonjs", - "lib": [ - "es2015", - "es2016", - "es2017", - "es2018" - ], + "lib": ["es2015", "es2016", "es2017", "es2018"], "declaration": true, "declarationMap": true, "sourceMap": true, @@ -17,10 +12,6 @@ "esModuleInterop": true, "noImplicitAny": false }, - "include": [ - "src/" - ], - "exclude": [ - "**/*.spec.ts" - ] + "include": ["src/"], + "exclude": ["**/*.spec.ts"] }