Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
zoe-codez committed May 16, 2024
1 parent 027c486 commit c57c9b8
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 46 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ export function ExampleRoom({ automation, context }: TServiceParams) {

// check sun position
if (automation.solar.isBetween("dawn", "dusk")) {

// create some reference points with dayjs
const [PM530, NOW] = automation.utils.shortTime(["PM5:30", "NOW"]);
return NOW.isBefore(PM530);
return automation.time.isBefore("PM5:30")
}
return false;
},
Expand Down
49 changes: 26 additions & 23 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
},
"license": "MIT",
"dependencies": {
"@digital-alchemy/core": "^0.3.12",
"@digital-alchemy/hass": "^0.3.20",
"@digital-alchemy/core": "^0.3.15",
"@digital-alchemy/hass": "^0.3.24",
"@digital-alchemy/synapse": "^0.3.5",
"dayjs": "^1.11.10",
"prom-client": "^15.1.1"
Expand Down
6 changes: 3 additions & 3 deletions src/automation.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
Room,
SequenceWatcher,
SolarCalculator,
Time,
} from "./extensions";
import { Utils } from "./extensions/utils.extension";

export const LIB_AUTOMATION = CreateLibrary({
configuration: {
Expand Down Expand Up @@ -73,7 +73,7 @@ export const LIB_AUTOMATION = CreateLibrary({
depends: [LIB_HASS, LIB_SYNAPSE],
name: "automation",
// light depends circadian
priorityInit: ["utils", "circadian"],
priorityInit: ["time", "circadian"],
services: {
/**
* # Aggressive Scenes extension
Expand Down Expand Up @@ -117,7 +117,7 @@ export const LIB_AUTOMATION = CreateLibrary({
/**
* Helper functions
*/
utils: Utils,
time: Time,
},
});

Expand Down
1 change: 1 addition & 0 deletions src/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./managed-switch.extension";
export * from "./room.extension";
export * from "./sequence-matcher.extension";
export * from "./solar-calc.extension";
export * from "./time.extension";
19 changes: 9 additions & 10 deletions src/extensions/solar-calc.extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
CronExpression,
is,

Check warning on line 3 in src/extensions/solar-calc.extension.ts

View workflow job for this annotation

GitHub Actions / lint-and-build

'is' is defined but never used. Allowed unused vars must match /_|logger/u
NONE,
TBlackHole,
TContext,

Check warning on line 6 in src/extensions/solar-calc.extension.ts

View workflow job for this annotation

GitHub Actions / lint-and-build

'TContext' is defined but never used. Allowed unused vars must match /_|logger/u
TServiceParams,
Expand Down Expand Up @@ -191,28 +193,25 @@ export function SolarCalculator({
};

solarReference.onEvent = ({
context,
eventName,
label,
exec,
offset,
}: OnSolarEvent) => {
event.on(eventName, async () => {
await internal.safeExec({
duration: undefined,
errors: undefined,
exec: async () => await exec(),
executions: undefined,
labels: { context, label },
});
scheduler.sliding({
exec: async () => await exec(),
label,
next: () => solarReference[eventName].add(offset ?? NONE, "ms"),
reset: CronExpression.EVERY_DAY_AT_MIDNIGHT,
});
};

return solarReference as SolarReference;
}

type OnSolarEvent = {
context: TContext;
label?: string;
offset?: number;
eventName: SolarEvents;
exec: () => TBlackHole;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { NONE, START } from "@digital-alchemy/core";
import { NONE, sleep, START, TServiceParams } from "@digital-alchemy/core";
import dayjs, { Dayjs } from "dayjs";

type Digit = `${number}`;

type TimeString = Digit | `${Digit}:${Digit}` | `${Digit}:${Digit}:${Digit}`;

type ShortTime = `${AmPm}${ShortDigits}${ShortSuffix}` | "NOW" | "TOMORROW";
export type TShortTime = `${AmPm}${ShortDigits}${ShortSuffix}`;
type ShortTime = TShortTime | "NOW" | "TOMORROW";
type ShortDigits =
| "1"
| "2"
Expand Down Expand Up @@ -33,9 +34,29 @@ type ShortSuffix = "" | ":00" | ":15" | ":30" | ":45";

const SLICE_LENGTH = "AM".length;
const ROLLOVER = 12;

export function Utils() {
export function Time({ automation }: TServiceParams) {
return {
/**
* Fast time check
*/
isAfter(time: TShortTime) {
const [NOW, target] = automation.time.shortTime(["NOW", time]);
return NOW.isAfter(target);
},
/**
* Fast time check
*/
isBefore(time: TShortTime) {
const [NOW, target] = automation.time.shortTime(["NOW", time]);
return NOW.isBefore(target);
},
/**
* Fast time check
*/
isBetween(start: TShortTime, end: TShortTime) {
const [NOW, START, END] = automation.time.shortTime(["NOW", start, end]);
return NOW.isBetween(START, END);
},
/**
* Quickly calculate reference points in time.
* Times are in reference to 12AM/midnight this morning, and input in 24 hour format.
Expand All @@ -60,6 +81,7 @@ export function Utils() {
const today = dayjs().format("YYYY-MM-DD");
return times.map(i => dayjs(`${today} ${i}`).millisecond(NONE));
},

/**
* Quickly calculate reference points in time.
* Times are in reference to 12AM/midnight this morning.
Expand Down Expand Up @@ -97,5 +119,7 @@ export function Utils() {
return dayjs(`${today} ${hour}:${minute}`).millisecond(NONE);
});
},

wait: (ms: number | Date) => sleep(ms),
};
}

0 comments on commit c57c9b8

Please sign in to comment.