-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb5a0df
commit 4ba236e
Showing
16 changed files
with
3,093 additions
and
1,392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ | |
/.vscode | ||
/.env* | ||
!/.env*.dist | ||
|
||
# DigitalAlchemy | ||
/synapse_storage.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { TServiceParams } from '@digital-alchemy/core' | ||
|
||
export function RuntimePrecedence({ | ||
logger, | ||
context, | ||
config, | ||
synapse, | ||
hass, | ||
lifecycle, | ||
}: TServiceParams) { | ||
// Whether this runtime is in development mode or not | ||
const isDevelop = config.homeAutomation.NODE_ENV === 'development' | ||
|
||
// When developing locally, the production runtime will pause and the development runtime will take over | ||
const isDevelopmentActive = synapse.switch({ | ||
context, | ||
name: 'Whether or not local development takes over from production', | ||
}) | ||
|
||
// Block outgoing commands and most incoming messages in prod when dev overrides it. | ||
isDevelopmentActive.onUpdate(() => { | ||
if (isDevelopmentActive.is_on) { | ||
logger.info('Development runtime takes over') | ||
hass.socket.pauseMessages = !isDevelop | ||
} else { | ||
logger.info('Resuming production runtime') | ||
hass.socket.pauseMessages = isDevelop | ||
} | ||
}) | ||
|
||
// Update the state on startup | ||
isDevelopmentActive.is_on = isDevelop | ||
|
||
// Give the go ahead for production to take over again when shutting down | ||
lifecycle.onPreShutdown(() => { | ||
if (isDevelop) isDevelopmentActive.is_on = false | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { TServiceParams } from '@digital-alchemy/core' | ||
import { Database } from 'bun:sqlite' | ||
|
||
// This service will be loaded first. Use it to do any global setup. | ||
export function Setup({ synapse }: TServiceParams) { | ||
synapse.sqlite.setDriver(Database) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* eslint-disable unicorn/prefer-export-from */ | ||
import dayjs from 'dayjs' | ||
import advancedFormat from 'dayjs/plugin/advancedFormat' | ||
import isBetween from 'dayjs/plugin/isBetween' | ||
import timezone from 'dayjs/plugin/timezone' | ||
import utc from 'dayjs/plugin/utc' | ||
import weekOfYear from 'dayjs/plugin/weekOfYear' | ||
|
||
dayjs.extend(weekOfYear) | ||
dayjs.extend(advancedFormat) | ||
dayjs.extend(isBetween) | ||
dayjs.extend(utc) | ||
dayjs.extend(timezone) | ||
|
||
export { dayjs } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { TServiceParams } from '@digital-alchemy/core' | ||
|
||
export function Helpers({ logger, config, hass }: TServiceParams) { | ||
const theSun = hass.refBy.id('sun.sun') | ||
|
||
const doStuff = (): string => { | ||
logger.info('doStuff was called!') | ||
|
||
return config.homeAutomation.MY_CONFIG_SETTING | ||
} | ||
|
||
return { theSun, doStuff } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,58 @@ | ||
import { CreateApplication } from '@digital-alchemy/core' | ||
import { LIB_AUTOMATION } from '@digital-alchemy/automation' | ||
import { CreateApplication, StringConfig } from '@digital-alchemy/core' | ||
import { LIB_HASS } from '@digital-alchemy/hass' | ||
import { LIB_SYNAPSE } from '@digital-alchemy/synapse' | ||
|
||
import { EntityList } from './entity-list' | ||
import { HelperFile } from './helper' | ||
import { RuntimePrecedence } from './core/runtime-precedence' | ||
import { Setup } from './core/setup' | ||
import { Helpers } from './helpers' | ||
import { Office } from './office' | ||
|
||
type AutomationEnvironments = 'development' | 'production' | 'test' | ||
|
||
const HOME_AUTOMATION = CreateApplication({ | ||
/** | ||
* keep your secrets out of the code! | ||
* these variables will be loaded from your configuration file | ||
*/ | ||
name: 'homeAutomation', | ||
configuration: { | ||
EXAMPLE_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<AutomationEnvironments>, | ||
|
||
MY_CONFIG_SETTING: { | ||
default: 'foo', | ||
description: 'A configuration defined as an example', | ||
type: 'string', | ||
}, | ||
}, | ||
|
||
/** | ||
* Adding to this array will provide additional elements in TServiceParams | ||
* for your code to use | ||
*/ | ||
libraries: [ | ||
/** | ||
* LIB_HASS provides basic interactions for Home Assistant | ||
* | ||
* Will automatically start websocket as part of bootstrap | ||
*/ | ||
LIB_HASS, | ||
], | ||
|
||
/** | ||
* must match key used in LoadedModules | ||
* affects: | ||
* - import name in TServiceParams | ||
* - and files used for configuration | ||
* - log context | ||
*/ | ||
name: 'home_automation', | ||
|
||
/** | ||
* Need a service to be loaded first? Add to this list | ||
*/ | ||
priorityInit: ['helper'], | ||
// Plugins for TSServiceParams | ||
libraries: [LIB_HASS, LIB_SYNAPSE, LIB_AUTOMATION], | ||
|
||
/** | ||
* Add additional services here | ||
* No guaranteed loading order unless added to priority list | ||
* | ||
* context: ServiceFunction | ||
*/ | ||
// Service initialization order | ||
priorityInit: ['setup', 'runtimePrecedence', 'helpers'], | ||
services: { | ||
entity_list: EntityList, | ||
helper: HelperFile, | ||
setup: Setup, | ||
runtimePrecedence: RuntimePrecedence, | ||
helpers: Helpers, | ||
office: Office, | ||
}, | ||
}) | ||
|
||
// Load the type definitions | ||
// Do some magic to make all the types work | ||
declare module '@digital-alchemy/core' { | ||
export interface LoadedModules { | ||
home_automation: typeof HOME_AUTOMATION | ||
homeAutomation: typeof HOME_AUTOMATION | ||
} | ||
} | ||
|
||
// Kick off the application! | ||
// bootstrap application | ||
setImmediate( | ||
async () => | ||
await HOME_AUTOMATION.bootstrap({ | ||
/** | ||
* override library defined defaults | ||
* not a substitute for config files | ||
*/ | ||
configuration: { | ||
// default value: trace | ||
boilerplate: { LOG_LEVEL: 'debug' }, | ||
boilerplate: { LOG_LEVEL: 'info' }, | ||
}, | ||
}), | ||
) |
Oops, something went wrong.