@digital-alchemy/hass 24.10.1 → 24.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/dynamic.d.ts +3 -1
  2. package/dist/extensions/events.extension.d.ts +1 -1
  3. package/dist/extensions/events.extension.js +26 -7
  4. package/dist/extensions/events.extension.js.map +1 -1
  5. package/dist/extensions/internal.extension.js +1 -1
  6. package/dist/extensions/internal.extension.js.map +1 -1
  7. package/dist/extensions/reference.extension.d.ts +51 -1
  8. package/dist/extensions/reference.extension.js +293 -181
  9. package/dist/extensions/reference.extension.js.map +1 -1
  10. package/dist/extensions/websocket-api.extension.js +4 -3
  11. package/dist/extensions/websocket-api.extension.js.map +1 -1
  12. package/dist/hass.module.d.ts +3 -3
  13. package/dist/hass.module.js +3 -3
  14. package/dist/hass.module.js.map +1 -1
  15. package/dist/helpers/entity-state.helper.d.ts +11 -7
  16. package/dist/helpers/interfaces.helper.d.ts +10 -10
  17. package/dist/helpers/interfaces.helper.js.map +1 -1
  18. package/dist/helpers/utility.helper.d.ts +5 -0
  19. package/dist/helpers/utility.helper.js +5 -0
  20. package/dist/helpers/utility.helper.js.map +1 -1
  21. package/dist/mock_assistant/mock-assistant.module.d.ts +4 -4
  22. package/dist/mock_assistant/mock-assistant.module.js +7 -0
  23. package/dist/mock_assistant/mock-assistant.module.js.map +1 -1
  24. package/dist/testing/fetch-api.spec.js +13 -13
  25. package/dist/testing/fetch-api.spec.js.map +1 -1
  26. package/package.json +27 -28
  27. package/scripts/test.sh +1 -1
  28. package/src/dynamic.ts +82 -77
  29. package/src/extensions/events.extension.ts +26 -10
  30. package/src/extensions/internal.extension.ts +1 -1
  31. package/src/extensions/reference.extension.ts +323 -201
  32. package/src/extensions/websocket-api.extension.ts +4 -3
  33. package/src/hass.module.ts +4 -4
  34. package/src/helpers/entity-state.helper.ts +11 -6
  35. package/src/helpers/interfaces.helper.ts +10 -9
  36. package/src/helpers/utility.helper.ts +8 -0
  37. package/src/mock_assistant/mock-assistant.module.ts +7 -0
  38. package/src/testing/fetch-api.spec.ts +13 -13
@@ -11,6 +11,7 @@ import {
11
11
  ENTITY_STATE,
12
12
  GetDomain,
13
13
  PICK_ENTITY,
14
+ RemoveCallback,
14
15
  } from "./utility.helper";
15
16
 
16
17
  export interface HassEntityContext {
@@ -40,9 +41,7 @@ export type TEntityUpdateCallback<ENTITY_ID extends ANY_ENTITY> = (
40
41
 
41
42
  export type RemovableCallback<ENTITY_ID extends ANY_ENTITY> = (
42
43
  callback: TEntityUpdateCallback<ENTITY_ID>,
43
- ) => {
44
- remove: () => void;
45
- };
44
+ ) => RemoveCallback;
46
45
 
47
46
  export type ByIdProxy<ENTITY_ID extends ANY_ENTITY> = ENTITY_STATE<ENTITY_ID> & {
48
47
  entity_id: ENTITY_ID;
@@ -57,7 +56,7 @@ export type ByIdProxy<ENTITY_ID extends ANY_ENTITY> = ENTITY_STATE<ENTITY_ID> &
57
56
  /**
58
57
  * Run callback once, for next update
59
58
  */
60
- once: (callback: TEntityUpdateCallback<ENTITY_ID>) => void;
59
+ once: (callback: TEntityUpdateCallback<ENTITY_ID>) => RemoveCallback;
61
60
  /**
62
61
  * Will resolve with the next state of the next value. No time limit
63
62
  */
@@ -71,9 +70,15 @@ export type ByIdProxy<ENTITY_ID extends ANY_ENTITY> = ENTITY_STATE<ENTITY_ID> &
71
70
  */
72
71
  previous: ENTITY_STATE<ENTITY_ID>;
73
72
  /**
74
- * Remove all `.onUpdate` listeners for this entity
73
+ * add a listener that can be removed with the removeAllListeners call
74
+ *
75
+ * for use by other libraries
76
+ */
77
+ addListener: (remove: RemoveCallback) => void;
78
+ /**
79
+ * Remove all resources related to this particular proxy
75
80
  *
76
- * If you want to remove a particular one, use use the return value of the `.onUpdate` call instead
81
+ * Will interrupt methods like "nextState", causing them to never return
77
82
  */
78
83
  removeAllListeners: () => void;
79
84
  } & (GetDomain<ENTITY_ID> extends ALL_SERVICE_DOMAINS
@@ -43,6 +43,7 @@ import {
43
43
  PICK_FROM_FLOOR,
44
44
  PICK_FROM_LABEL,
45
45
  PICK_FROM_PLATFORM,
46
+ RemoveCallback,
46
47
  } from "./utility.helper";
47
48
  import {
48
49
  EntityHistoryDTO,
@@ -124,7 +125,7 @@ export type HassWebsocketAPI = {
124
125
  /**
125
126
  * run a callback when the socket finishes (re)connecting
126
127
  */
127
- onConnect: (callback: () => TBlackHole) => void;
128
+ onConnect: (callback: () => TBlackHole) => RemoveCallback;
128
129
  /**
129
130
  * Attach to the incoming stream of socket events. Do your own filtering and processing from there
130
131
  *
@@ -135,7 +136,7 @@ export type HassWebsocketAPI = {
135
136
  event,
136
137
  once,
137
138
  exec,
138
- }: OnHassEventOptions<DATA>) => () => void;
139
+ }: OnHassEventOptions<DATA>) => RemoveCallback;
139
140
  /**
140
141
  * @internal
141
142
  *
@@ -179,7 +180,7 @@ export type HassWebsocketAPI = {
179
180
  event_type,
180
181
  context,
181
182
  exec,
182
- }: SocketSubscribeOptions<EVENT>) => Promise<void>;
183
+ }: SocketSubscribeOptions<EVENT>) => Promise<RemoveCallback>;
183
184
  /**
184
185
  * remove the current socket connection to home assistant
185
186
  *
@@ -280,12 +281,12 @@ export type HassEntityManager = {
280
281
  export type SimpleCallback = () => TBlackHole;
281
282
 
282
283
  export type HassEventsService = {
283
- onAreaRegistryUpdate: (callback: SimpleCallback) => void;
284
- onDeviceRegistryUpdate: (callback: SimpleCallback) => void;
285
- onEntityRegistryUpdate: (callback: SimpleCallback) => void;
286
- onFloorRegistryUpdate: (callback: SimpleCallback) => void;
287
- onLabelRegistryUpdate: (callback: SimpleCallback) => void;
288
- onZoneRegistryUpdate: (callback: SimpleCallback) => void;
284
+ onAreaRegistryUpdate: (callback: SimpleCallback) => RemoveCallback;
285
+ onDeviceRegistryUpdate: (callback: SimpleCallback) => RemoveCallback;
286
+ onEntityRegistryUpdate: (callback: SimpleCallback) => RemoveCallback;
287
+ onFloorRegistryUpdate: (callback: SimpleCallback) => RemoveCallback;
288
+ onLabelRegistryUpdate: (callback: SimpleCallback) => RemoveCallback;
289
+ onZoneRegistryUpdate: (callback: SimpleCallback) => RemoveCallback;
289
290
  };
290
291
 
291
292
  export type HassFloorService = {
@@ -106,6 +106,13 @@ function isDomain<DOMAIN extends ALL_DOMAINS>(
106
106
  }
107
107
 
108
108
  is.domain = isDomain;
109
+ is.removeFn = (remove: () => void) => {
110
+ const out = remove as RemoveCallback;
111
+ out.remove = remove;
112
+ return out;
113
+ };
114
+
115
+ export type RemoveCallback = { remove: () => void; (): void };
109
116
 
110
117
  declare module "@digital-alchemy/core" {
111
118
  export interface IsIt {
@@ -113,6 +120,7 @@ declare module "@digital-alchemy/core" {
113
120
  * Check to see if an entity matches
114
121
  */
115
122
  domain: typeof isDomain;
123
+ removeFn: (remove: () => void) => RemoveCallback;
116
124
  }
117
125
  }
118
126
 
@@ -85,6 +85,13 @@ export const createTestRunner = () =>
85
85
  .configure({
86
86
  boilerplate: { IS_TEST: true },
87
87
  })
88
+ .setOptions({
89
+ configSources: {
90
+ argv: false,
91
+ env: false,
92
+ file: false,
93
+ },
94
+ })
88
95
  .appendLibrary(LIB_MOCK_ASSISTANT);
89
96
 
90
97
  export const hassTestRunner = createTestRunner();
@@ -28,7 +28,7 @@ describe("FetchAPI", () => {
28
28
  expect.assertions(1);
29
29
  await hassTestRunner.run(({ lifecycle, hass }) => {
30
30
  lifecycle.onReady(async () => {
31
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
31
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
32
32
  return {
33
33
  text: () => "[]",
34
34
  } as unknown as Response;
@@ -54,7 +54,7 @@ describe("FetchAPI", () => {
54
54
  it("should format calendarSearch properly", async () => {
55
55
  expect.assertions(1);
56
56
  await hassTestRunner.run(({ lifecycle, hass }) => {
57
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
57
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
58
58
  return {
59
59
  text: () => "[]",
60
60
  } as unknown as Response;
@@ -78,7 +78,7 @@ describe("FetchAPI", () => {
78
78
  it("should format callService properly", async () => {
79
79
  expect.assertions(1);
80
80
  await hassTestRunner.run(({ lifecycle, hass }) => {
81
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
81
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
82
82
  return {
83
83
  text: () => "{}",
84
84
  } as unknown as Response;
@@ -103,7 +103,7 @@ describe("FetchAPI", () => {
103
103
  it("should format checkConfig properly", async () => {
104
104
  expect.assertions(1);
105
105
  await hassTestRunner.run(({ lifecycle, hass }) => {
106
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
106
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
107
107
  return {
108
108
  text: () => "{}",
109
109
  } as unknown as Response;
@@ -166,7 +166,7 @@ describe("FetchAPI", () => {
166
166
  it("should format fetchEntityCustomizations properly", async () => {
167
167
  expect.assertions(1);
168
168
  await hassTestRunner.run(({ lifecycle, hass }) => {
169
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
169
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
170
170
  return {
171
171
  text: () => "{}",
172
172
  } as unknown as Response;
@@ -186,7 +186,7 @@ describe("FetchAPI", () => {
186
186
  it("should format fetchEntityHistory properly", async () => {
187
187
  expect.assertions(1);
188
188
  await hassTestRunner.run(({ lifecycle, hass }) => {
189
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
189
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
190
190
  return {
191
191
  text: () => "{}",
192
192
  } as unknown as Response;
@@ -207,7 +207,7 @@ describe("FetchAPI", () => {
207
207
  expect.assertions(1);
208
208
  const body = { magic: true };
209
209
  await hassTestRunner.run(({ lifecycle, hass }) => {
210
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
210
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
211
211
  return {
212
212
  text: () => "{}",
213
213
  } as unknown as Response;
@@ -276,7 +276,7 @@ describe("FetchAPI", () => {
276
276
  it("should format getLogs properly", async () => {
277
277
  expect.assertions(1);
278
278
  await hassTestRunner.run(({ lifecycle, hass }) => {
279
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
279
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
280
280
  return {
281
281
  text: () => "[]",
282
282
  } as unknown as Response;
@@ -296,7 +296,7 @@ describe("FetchAPI", () => {
296
296
  it("should format getRawLogs properly", async () => {
297
297
  expect.assertions(1);
298
298
  await hassTestRunner.run(({ lifecycle, hass }) => {
299
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
299
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
300
300
  return {
301
301
  text: () => "{}",
302
302
  } as unknown as Response;
@@ -317,7 +317,7 @@ describe("FetchAPI", () => {
317
317
  expect.assertions(1);
318
318
  await hassTestRunner.run(({ lifecycle, hass, mock_assistant }) => {
319
319
  lifecycle.onReady(async () => {
320
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
320
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
321
321
  return {
322
322
  text: () => "{}",
323
323
  } as unknown as Response;
@@ -337,7 +337,7 @@ describe("FetchAPI", () => {
337
337
  it("should format updateEntity properly", async () => {
338
338
  expect.assertions(1);
339
339
  await hassTestRunner.run(({ lifecycle, hass }) => {
340
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
340
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
341
341
  return {
342
342
  text: () => "{}",
343
343
  } as unknown as Response;
@@ -369,7 +369,7 @@ describe("FetchAPI", () => {
369
369
  magic: true,
370
370
  };
371
371
  await hassTestRunner.run(({ lifecycle, hass }) => {
372
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
372
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
373
373
  return {
374
374
  text: () => "{}",
375
375
  } as unknown as Response;
@@ -390,7 +390,7 @@ describe("FetchAPI", () => {
390
390
  it("should format checkCredentials properly", async () => {
391
391
  expect.assertions(1);
392
392
  await hassTestRunner.run(({ lifecycle, hass }) => {
393
- const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
393
+ const spy = jest.spyOn(globalThis, "fetch").mockImplementation(async () => {
394
394
  return {
395
395
  text: () => "{}",
396
396
  } as unknown as Response;