Skip to content

Commit

Permalink
homebridge-automation forked from ai
Browse files Browse the repository at this point in the history
  • Loading branch information
grrowl committed Jan 25, 2024
1 parent afe274d commit a3641e2
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 71 deletions.
6 changes: 4 additions & 2 deletions .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"]
}
12 changes: 3 additions & 9 deletions README.md
@@ -1,12 +1,6 @@
<p align="center" style="vertial-align:middle;">
# homebridge-automation

<img src="https://github.com/homebridge/branding/raw/master/logos/homebridge-wordmark-logo-vertical.png" width="150"> + <strong>AI</strong>

</p>

# homebridge-ai

Control your homebridge instance with AI. Check it out at https://homebridgeai.com/
Control your homebridge instance with Javascript functions.

## Local testing

Expand All @@ -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/
Expand Down
38 changes: 30 additions & 8 deletions 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": {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 7 additions & 15 deletions 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",
Expand All @@ -25,18 +25,10 @@
},
"keywords": [
"homebridge-plugin",
"homebridgeai",
"homekit",
"ai",
"automation",
"gpt",
"assistant",
"chat",
"remote",
"whatsapp",
"shortcuts",
"ios",
"android",
"delay",
"automation"
],
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions 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);
};
39 changes: 21 additions & 18 deletions src/platform.ts
Expand Up @@ -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[] = [];
Expand All @@ -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",
);
}

Expand Down Expand Up @@ -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", () => {
Expand All @@ -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: {
Expand Down Expand Up @@ -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,
});
});
});
}
});
}

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion 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";
4 changes: 2 additions & 2 deletions 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,
Expand Down
15 changes: 3 additions & 12 deletions tsconfig.json
Expand Up @@ -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,
Expand All @@ -17,10 +12,6 @@
"esModuleInterop": true,
"noImplicitAny": false
},
"include": [
"src/"
],
"exclude": [
"**/*.spec.ts"
]
"include": ["src/"],
"exclude": ["**/*.spec.ts"]
}

0 comments on commit a3641e2

Please sign in to comment.