@digital-alchemy/hass 24.9.3 → 24.9.4

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 (197) hide show
  1. package/README.md +4 -1
  2. package/dist/dynamic.d.ts +1 -1
  3. package/dist/dynamic.js +7 -2
  4. package/dist/dynamic.js.map +1 -1
  5. package/dist/extensions/area.extension.d.ts +2 -12
  6. package/dist/extensions/area.extension.js +20 -26
  7. package/dist/extensions/area.extension.js.map +1 -1
  8. package/dist/extensions/backup.extension.d.ts +2 -7
  9. package/dist/extensions/backup.extension.js +5 -8
  10. package/dist/extensions/backup.extension.js.map +1 -1
  11. package/dist/extensions/call-proxy.extension.d.ts +1 -1
  12. package/dist/extensions/call-proxy.extension.js +3 -22
  13. package/dist/extensions/call-proxy.extension.js.map +1 -1
  14. package/dist/extensions/config.extension.d.ts +2 -6
  15. package/dist/extensions/config.extension.js +21 -25
  16. package/dist/extensions/config.extension.js.map +1 -1
  17. package/dist/extensions/conversation.extension.d.ts +2 -6
  18. package/dist/extensions/conversation.extension.js +5 -8
  19. package/dist/extensions/conversation.extension.js.map +1 -1
  20. package/dist/extensions/device.extension.d.ts +2 -5
  21. package/dist/extensions/device.extension.js +16 -22
  22. package/dist/extensions/device.extension.js.map +1 -1
  23. package/dist/extensions/entity.extension.d.ts +2 -61
  24. package/dist/extensions/entity.extension.js +42 -83
  25. package/dist/extensions/entity.extension.js.map +1 -1
  26. package/dist/extensions/events.extension.d.ts +3 -11
  27. package/dist/extensions/events.extension.js +8 -11
  28. package/dist/extensions/events.extension.js.map +1 -1
  29. package/dist/extensions/fetch-api.extension.d.ts +12 -4
  30. package/dist/extensions/fetch-api.extension.js +23 -35
  31. package/dist/extensions/fetch-api.extension.js.map +1 -1
  32. package/dist/extensions/floor.extension.d.ts +2 -9
  33. package/dist/extensions/floor.extension.js +17 -23
  34. package/dist/extensions/floor.extension.js.map +1 -1
  35. package/dist/extensions/id-by.extension.js +15 -20
  36. package/dist/extensions/id-by.extension.js.map +1 -1
  37. package/dist/extensions/index.d.ts +1 -0
  38. package/dist/extensions/index.js +16 -18
  39. package/dist/extensions/index.js.map +1 -1
  40. package/dist/extensions/internal.extension.d.ts +18 -0
  41. package/dist/extensions/internal.extension.js +102 -0
  42. package/dist/extensions/internal.extension.js.map +1 -0
  43. package/dist/extensions/label.extension.d.ts +2 -9
  44. package/dist/extensions/label.extension.js +17 -23
  45. package/dist/extensions/label.extension.js.map +1 -1
  46. package/dist/extensions/reference.extension.d.ts +2 -12
  47. package/dist/extensions/reference.extension.js +19 -25
  48. package/dist/extensions/reference.extension.js.map +1 -1
  49. package/dist/extensions/registry.extension.d.ts +2 -7
  50. package/dist/extensions/registry.extension.js +1 -4
  51. package/dist/extensions/registry.extension.js.map +1 -1
  52. package/dist/extensions/websocket-api.extension.d.ts +3 -78
  53. package/dist/extensions/websocket-api.extension.js +82 -165
  54. package/dist/extensions/websocket-api.extension.js.map +1 -1
  55. package/dist/extensions/zone.extension.d.ts +2 -7
  56. package/dist/extensions/zone.extension.js +15 -21
  57. package/dist/extensions/zone.extension.js.map +1 -1
  58. package/dist/hass.module.d.ts +47 -36
  59. package/dist/hass.module.js +70 -70
  60. package/dist/hass.module.js.map +1 -1
  61. package/dist/helpers/backup.helper.js +1 -2
  62. package/dist/helpers/constants.helper.js +15 -18
  63. package/dist/helpers/constants.helper.js.map +1 -1
  64. package/dist/helpers/device.helper.js +2 -5
  65. package/dist/helpers/device.helper.js.map +1 -1
  66. package/dist/helpers/entity-state.helper.d.ts +3 -8
  67. package/dist/helpers/entity-state.helper.js +1 -8
  68. package/dist/helpers/entity-state.helper.js.map +1 -1
  69. package/dist/helpers/features.helper.js +79 -85
  70. package/dist/helpers/features.helper.js.map +1 -1
  71. package/dist/helpers/fetch/calendar.js +1 -2
  72. package/dist/helpers/fetch/configuration.js +2 -5
  73. package/dist/helpers/fetch/configuration.js.map +1 -1
  74. package/dist/helpers/fetch/index.js +5 -8
  75. package/dist/helpers/fetch/index.js.map +1 -1
  76. package/dist/helpers/fetch/server-log.js +1 -2
  77. package/dist/helpers/fetch/server-log.js.map +1 -1
  78. package/dist/helpers/fetch/service-list.js +1 -2
  79. package/dist/helpers/fetch/weather-forecasts.js +1 -2
  80. package/dist/helpers/fetch/weather-forecasts.js.map +1 -1
  81. package/dist/helpers/fetch.helper.d.ts +162 -0
  82. package/dist/helpers/fetch.helper.js +161 -0
  83. package/dist/helpers/fetch.helper.js.map +1 -0
  84. package/dist/helpers/id-by.helper.js +1 -2
  85. package/dist/helpers/index.d.ts +2 -1
  86. package/dist/helpers/index.js +13 -15
  87. package/dist/helpers/index.js.map +1 -1
  88. package/dist/helpers/interfaces.helper.d.ts +228 -0
  89. package/dist/helpers/interfaces.helper.js +10 -0
  90. package/dist/helpers/interfaces.helper.js.map +1 -0
  91. package/dist/helpers/manifest.helper.d.ts +0 -1
  92. package/dist/helpers/manifest.helper.js +0 -1
  93. package/dist/helpers/notify.helper.d.ts +11 -3
  94. package/dist/helpers/notify.helper.js +1 -2
  95. package/dist/helpers/registry.js +7 -10
  96. package/dist/helpers/registry.js.map +1 -1
  97. package/dist/helpers/utility.helper.d.ts +6 -1
  98. package/dist/helpers/utility.helper.js +9 -13
  99. package/dist/helpers/utility.helper.js.map +1 -1
  100. package/dist/helpers/websocket.helper.d.ts +1 -2
  101. package/dist/helpers/websocket.helper.js +1 -2
  102. package/dist/index.js +5 -8
  103. package/dist/index.js.map +1 -1
  104. package/dist/mock_assistant/extensions/area.extension.d.ts +8 -0
  105. package/dist/mock_assistant/extensions/area.extension.js +51 -0
  106. package/dist/mock_assistant/extensions/area.extension.js.map +1 -0
  107. package/dist/mock_assistant/extensions/config.extension.d.ts +14 -0
  108. package/dist/mock_assistant/extensions/config.extension.js +29 -0
  109. package/dist/mock_assistant/extensions/config.extension.js.map +1 -0
  110. package/dist/mock_assistant/extensions/device.extension.d.ts +8 -0
  111. package/dist/mock_assistant/extensions/device.extension.js +33 -0
  112. package/dist/mock_assistant/extensions/device.extension.js.map +1 -0
  113. package/dist/mock_assistant/extensions/entity-registry.extension.d.ts +13 -0
  114. package/dist/mock_assistant/extensions/entity-registry.extension.js +28 -0
  115. package/dist/mock_assistant/extensions/entity-registry.extension.js.map +1 -0
  116. package/dist/mock_assistant/extensions/entity.extension.d.ts +30 -0
  117. package/dist/mock_assistant/extensions/entity.extension.js +77 -0
  118. package/dist/mock_assistant/extensions/entity.extension.js.map +1 -0
  119. package/dist/mock_assistant/extensions/events.extension.d.ts +1 -1
  120. package/dist/mock_assistant/extensions/events.extension.js +3 -6
  121. package/dist/mock_assistant/extensions/events.extension.js.map +1 -1
  122. package/dist/mock_assistant/extensions/fetch.extension.d.ts +1 -0
  123. package/dist/mock_assistant/extensions/fetch.extension.js +4 -0
  124. package/dist/mock_assistant/extensions/fetch.extension.js.map +1 -0
  125. package/dist/mock_assistant/extensions/fixtures.extension.d.ts +1 -1
  126. package/dist/mock_assistant/extensions/fixtures.extension.js +29 -33
  127. package/dist/mock_assistant/extensions/fixtures.extension.js.map +1 -1
  128. package/dist/mock_assistant/extensions/floor.extension.d.ts +8 -0
  129. package/dist/mock_assistant/extensions/floor.extension.js +51 -0
  130. package/dist/mock_assistant/extensions/floor.extension.js.map +1 -0
  131. package/dist/mock_assistant/extensions/index.d.ts +10 -0
  132. package/dist/mock_assistant/extensions/index.js +12 -5
  133. package/dist/mock_assistant/extensions/index.js.map +1 -1
  134. package/dist/mock_assistant/extensions/label.extension.d.ts +8 -0
  135. package/dist/mock_assistant/extensions/label.extension.js +51 -0
  136. package/dist/mock_assistant/extensions/label.extension.js.map +1 -0
  137. package/dist/mock_assistant/extensions/services.extension.d.ts +12 -0
  138. package/dist/mock_assistant/extensions/services.extension.js +20 -0
  139. package/dist/mock_assistant/extensions/services.extension.js.map +1 -0
  140. package/dist/mock_assistant/extensions/websocket-api.extension.d.ts +15 -0
  141. package/dist/mock_assistant/extensions/websocket-api.extension.js +68 -0
  142. package/dist/mock_assistant/extensions/websocket-api.extension.js.map +1 -0
  143. package/dist/mock_assistant/extensions/zone.extension.d.ts +8 -0
  144. package/dist/mock_assistant/extensions/zone.extension.js +51 -0
  145. package/dist/mock_assistant/extensions/zone.extension.js.map +1 -0
  146. package/dist/mock_assistant/helpers/fixtures.js +1 -2
  147. package/dist/mock_assistant/helpers/index.d.ts +0 -1
  148. package/dist/mock_assistant/helpers/index.js +1 -5
  149. package/dist/mock_assistant/helpers/index.js.map +1 -1
  150. package/dist/mock_assistant/index.js +3 -6
  151. package/dist/mock_assistant/index.js.map +1 -1
  152. package/dist/mock_assistant/main.js +11 -15
  153. package/dist/mock_assistant/main.js.map +1 -1
  154. package/dist/mock_assistant/mock-assistant.module.d.ts +156 -3
  155. package/dist/mock_assistant/mock-assistant.module.js +56 -11
  156. package/dist/mock_assistant/mock-assistant.module.js.map +1 -1
  157. package/dist/quickboot.module.js +5 -8
  158. package/dist/quickboot.module.js.map +1 -1
  159. package/dist/testing/area.spec.js +106 -194
  160. package/dist/testing/area.spec.js.map +1 -1
  161. package/dist/testing/backup.spec.js +97 -139
  162. package/dist/testing/backup.spec.js.map +1 -1
  163. package/dist/testing/config.spec.js +79 -153
  164. package/dist/testing/config.spec.js.map +1 -1
  165. package/dist/testing/device.spec.js +35 -69
  166. package/dist/testing/device.spec.js.map +1 -1
  167. package/dist/testing/entity.spec.js +94 -149
  168. package/dist/testing/entity.spec.js.map +1 -1
  169. package/dist/testing/events.spec.js +33 -57
  170. package/dist/testing/events.spec.js.map +1 -1
  171. package/dist/testing/fetch-api.spec.js +242 -427
  172. package/dist/testing/fetch-api.spec.js.map +1 -1
  173. package/dist/testing/fixtures.spec.d.ts +1 -0
  174. package/dist/testing/fixtures.spec.js +150 -0
  175. package/dist/testing/fixtures.spec.js.map +1 -0
  176. package/dist/testing/floor.spec.js +106 -194
  177. package/dist/testing/floor.spec.js.map +1 -1
  178. package/dist/testing/id-by.spec.js +68 -107
  179. package/dist/testing/id-by.spec.js.map +1 -1
  180. package/dist/testing/label.spec.js +106 -194
  181. package/dist/testing/label.spec.js.map +1 -1
  182. package/dist/testing/ref-by.spec.js +155 -219
  183. package/dist/testing/ref-by.spec.js.map +1 -1
  184. package/dist/testing/websocket.spec.d.ts +1 -8
  185. package/dist/testing/websocket.spec.js +35 -50
  186. package/dist/testing/websocket.spec.js.map +1 -1
  187. package/dist/testing/workflow.spec.js +82 -81
  188. package/dist/testing/workflow.spec.js.map +1 -1
  189. package/dist/testing/zone.spec.js +61 -113
  190. package/dist/testing/zone.spec.js.map +1 -1
  191. package/package.json +56 -40
  192. package/dist/helpers/metrics.helper.d.ts +0 -29
  193. package/dist/helpers/metrics.helper.js +0 -62
  194. package/dist/helpers/metrics.helper.js.map +0 -1
  195. package/dist/mock_assistant/helpers/utils.d.ts +0 -4
  196. package/dist/mock_assistant/helpers/utils.js +0 -57
  197. package/dist/mock_assistant/helpers/utils.js.map +0 -1
@@ -1,207 +1,139 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
5
- const utils_1 = require("../mock_assistant/helpers/utils");
1
+ import dayjs from "dayjs";
2
+ import { hassTestRunner } from "../mock_assistant";
6
3
  describe("FetchAPI", () => {
7
- let application;
8
4
  const BASE_URL = "http://homeassistant.some.domain:9123";
9
5
  const TOKEN =
6
+ // TODO: Replace hard coded token w/ faker when avail
7
+ // https://github.com/faker-js/faker/pull/2936
10
8
  // eslint-disable-next-line @cspell/spellchecker
11
9
  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ2aWRlbyI6Imh0dHBzOi8vd3d3LnlvdXR1YmUuY29tL3dhdGNoP3Y9ZFF3NHc5V2dYY1EifQ.gPIttZEaLZgov3VZziu3LovcgtDbj8H0-XfBg4f08Y0";
10
+ hassTestRunner.configure({
11
+ hass: { BASE_URL, TOKEN },
12
+ });
12
13
  // values are hard coded into tests, update carefully
13
- const start = (0, dayjs_1.default)("2024-01-01 00:00:00:00");
14
+ const start = dayjs("2024-01-01 00:00:00:00");
14
15
  const end = start.add(1, "day");
15
16
  afterEach(async () => {
16
- if (application) {
17
- await application.teardown();
18
- application = undefined;
19
- }
17
+ await hassTestRunner.teardown();
20
18
  jest.restoreAllMocks();
21
19
  });
22
20
  describe("Meta", () => {
23
21
  it("Should send the correct headers", async () => {
24
22
  expect.assertions(1);
25
- application = (0, utils_1.CreateTestingApplication)({
26
- Test({ lifecycle, hass }) {
27
- const spy = jest
28
- .spyOn(global, "fetch")
29
- .mockImplementation(async () => {
23
+ await hassTestRunner.run(({ lifecycle, hass }) => {
24
+ lifecycle.onReady(async () => {
25
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
30
26
  return {
31
27
  text: () => "[]",
32
28
  };
33
29
  });
34
- lifecycle.onReady(async () => {
35
- // Calling the base level fetch provided by service
36
- // The same call is wrapped internally to power everything else
37
- await hass.fetch.fetch({ url: "/api/" });
38
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/`, expect.objectContaining({
39
- headers: {
40
- Authorization: "Bearer " + TOKEN,
41
- },
42
- method: "get",
43
- }));
44
- });
45
- },
30
+ // Calling the base level fetch provided by service
31
+ // The same call is wrapped internally to power everything else
32
+ await hass.fetch.fetch({ url: "/api/" });
33
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/`, expect.objectContaining({
34
+ headers: {
35
+ Authorization: "Bearer " + TOKEN,
36
+ },
37
+ method: "get",
38
+ }));
39
+ });
46
40
  });
47
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
48
- hass: {
49
- AUTO_CONNECT_SOCKET: false,
50
- AUTO_SCAN_CALL_PROXY: false,
51
- BASE_URL,
52
- TOKEN,
53
- },
54
- }));
55
41
  });
56
42
  });
57
43
  describe("API Operations", () => {
58
44
  it("should format calendarSearch properly", async () => {
59
45
  expect.assertions(1);
60
- application = (0, utils_1.CreateTestingApplication)({
61
- Test({ lifecycle, hass }) {
62
- const spy = jest
63
- .spyOn(global, "fetch")
64
- .mockImplementation(async () => {
65
- return {
66
- text: () => "[]",
67
- };
68
- });
69
- lifecycle.onReady(async () => {
70
- await hass.fetch.calendarSearch({
71
- calendar: "calendar.united_states_tx",
72
- end,
73
- start,
74
- });
75
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/calendars/calendar.united_states_tx?end=${encodeURIComponent(end.toISOString())}&start=${encodeURIComponent(start.toISOString())}`, expect.objectContaining({
76
- method: "get",
77
- }));
78
- });
79
- },
46
+ await hassTestRunner.run(({ lifecycle, hass }) => {
47
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
48
+ return {
49
+ text: () => "[]",
50
+ };
51
+ });
52
+ lifecycle.onReady(async () => {
53
+ await hass.fetch.calendarSearch({
54
+ calendar: "calendar.united_states_tx",
55
+ end,
56
+ start,
57
+ });
58
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/calendars/calendar.united_states_tx?end=${encodeURIComponent(end.toISOString())}&start=${encodeURIComponent(start.toISOString())}`, expect.objectContaining({
59
+ method: "get",
60
+ }));
61
+ });
80
62
  });
81
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
82
- hass: {
83
- AUTO_CONNECT_SOCKET: false,
84
- AUTO_SCAN_CALL_PROXY: false,
85
- BASE_URL,
86
- TOKEN,
87
- },
88
- }));
89
63
  });
90
64
  it("should format callService properly", async () => {
91
65
  expect.assertions(1);
92
- application = (0, utils_1.CreateTestingApplication)({
93
- Test({ lifecycle, hass }) {
94
- const spy = jest
95
- .spyOn(global, "fetch")
96
- .mockImplementation(async () => {
97
- return {
98
- text: () => "{}",
99
- };
100
- });
101
- lifecycle.onReady(async () => {
102
- await hass.fetch.callService("switch.toggle", {
66
+ await hassTestRunner.run(({ lifecycle, hass }) => {
67
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
68
+ return {
69
+ text: () => "{}",
70
+ };
71
+ });
72
+ lifecycle.onReady(async () => {
73
+ await hass.fetch.callService("switch.toggle", {
74
+ entity_id: "switch.porch_light",
75
+ });
76
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/services/switch/toggle`, expect.objectContaining({
77
+ body: JSON.stringify({
103
78
  entity_id: "switch.porch_light",
104
- });
105
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/services/switch/toggle`, expect.objectContaining({
106
- body: JSON.stringify({
107
- entity_id: "switch.porch_light",
108
- }),
109
- method: "post",
110
- }));
111
- });
112
- },
79
+ }),
80
+ method: "post",
81
+ }));
82
+ });
113
83
  });
114
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
115
- hass: {
116
- AUTO_CONNECT_SOCKET: false,
117
- AUTO_SCAN_CALL_PROXY: false,
118
- BASE_URL,
119
- TOKEN,
120
- },
121
- }));
122
84
  });
123
85
  it("should format checkConfig properly", async () => {
124
86
  expect.assertions(1);
125
- application = (0, utils_1.CreateTestingApplication)({
126
- Test({ lifecycle, hass }) {
127
- const spy = jest
128
- .spyOn(global, "fetch")
129
- .mockImplementation(async () => {
130
- return {
131
- text: () => "{}",
132
- };
133
- });
134
- lifecycle.onReady(async () => {
135
- await hass.fetch.checkConfig();
136
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/config/core/check_config`, expect.objectContaining({
137
- method: "post",
138
- }));
139
- });
140
- },
87
+ await hassTestRunner.run(({ lifecycle, hass }) => {
88
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
89
+ return {
90
+ text: () => "{}",
91
+ };
92
+ });
93
+ lifecycle.onReady(async () => {
94
+ await hass.fetch.checkConfig();
95
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/config/core/check_config`, expect.objectContaining({
96
+ method: "post",
97
+ }));
98
+ });
141
99
  });
142
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
143
- hass: {
144
- AUTO_CONNECT_SOCKET: false,
145
- AUTO_SCAN_CALL_PROXY: false,
146
- BASE_URL,
147
- TOKEN,
148
- },
149
- }));
150
100
  });
151
- // TODO: Need a way to make this pass without breaking all other tests
152
- it.skip("exits for low version at boot", async () => {
101
+ // TODO: this results in an open handle
102
+ // not sure why
103
+ xit("exits for low version at boot", async () => {
153
104
  expect.assertions(2);
154
- let mock;
155
- let exitSpy;
156
- application = (0, utils_1.CreateTestingApplication)({
157
- Test({ hass }) {
158
- mock = jest
159
- .spyOn(hass.fetch, "getConfig")
160
- .mockImplementation(async () => ({ version: "2024.1.0" }));
161
- exitSpy = jest.spyOn(process, "exit").mockImplementation(() => {
162
- throw new Error("EXPECTED TESTING ERROR");
163
- });
164
- },
165
- });
105
+ const exitSpy = jest.spyOn(process, "exit");
106
+ // .mockImplementation(() => {
107
+ // throw new Error("EXPECTED TESTING ERROR");
108
+ // });
109
+ jest.spyOn(console, "error").mockImplementation(() => undefined);
166
110
  try {
167
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
168
- hass: {
169
- AUTO_CONNECT_SOCKET: false,
170
- AUTO_SCAN_CALL_PROXY: false,
171
- BASE_URL,
172
- TOKEN,
173
- },
174
- }));
111
+ await hassTestRunner.run(({ lifecycle, mock_assistant }) => {
112
+ lifecycle.onPreInit(() => {
113
+ mock_assistant.config.loadFixtures({ version: "2024.1.0" });
114
+ }, -1000);
115
+ });
175
116
  }
176
- finally {
177
- expect(mock).toHaveBeenCalled();
178
- expect(exitSpy).toHaveBeenCalled();
117
+ catch (error) {
118
+ expect(error).toBeDefined();
179
119
  }
120
+ await hassTestRunner.teardown();
121
+ expect(exitSpy).toHaveBeenCalled();
180
122
  });
181
123
  // TODO: Need a way to make this pass without breaking all other tests
182
124
  it.skip("passes for valid version", async () => {
183
125
  expect.assertions(2);
184
126
  let mock;
185
127
  let exitSpy;
186
- application = (0, utils_1.CreateTestingApplication)({
187
- Test({ hass }) {
128
+ try {
129
+ await hassTestRunner.run(({ hass }) => {
188
130
  mock = jest
189
131
  .spyOn(hass.fetch, "getConfig")
190
132
  .mockImplementation(async () => ({ version: "2024.4.1" }));
191
133
  exitSpy = jest.spyOn(process, "exit").mockImplementation(() => {
192
134
  throw new Error("EXPECTED TESTING ERROR");
193
135
  });
194
- },
195
- });
196
- try {
197
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
198
- hass: {
199
- AUTO_CONNECT_SOCKET: false,
200
- AUTO_SCAN_CALL_PROXY: false,
201
- BASE_URL,
202
- TOKEN,
203
- },
204
- }));
136
+ });
205
137
  }
206
138
  finally {
207
139
  expect(mock).toHaveBeenCalled();
@@ -210,326 +142,209 @@ describe("FetchAPI", () => {
210
142
  });
211
143
  it("should format fetchEntityCustomizations properly", async () => {
212
144
  expect.assertions(1);
213
- application = (0, utils_1.CreateTestingApplication)({
214
- Test({ lifecycle, hass }) {
215
- const spy = jest
216
- .spyOn(global, "fetch")
217
- .mockImplementation(async () => {
218
- return {
219
- text: () => "{}",
220
- };
221
- });
222
- lifecycle.onReady(async () => {
223
- await hass.fetch.fetchEntityCustomizations("switch.porch_light");
224
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/config/customize/config/switch.porch_light`, expect.objectContaining({
225
- method: "get",
226
- }));
227
- });
228
- },
145
+ await hassTestRunner.run(({ lifecycle, hass }) => {
146
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
147
+ return {
148
+ text: () => "{}",
149
+ };
150
+ });
151
+ lifecycle.onReady(async () => {
152
+ await hass.fetch.fetchEntityCustomizations("switch.porch_light");
153
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/config/customize/config/switch.porch_light`, expect.objectContaining({
154
+ method: "get",
155
+ }));
156
+ });
229
157
  });
230
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
231
- hass: {
232
- AUTO_CONNECT_SOCKET: false,
233
- AUTO_SCAN_CALL_PROXY: false,
234
- BASE_URL,
235
- TOKEN,
236
- },
237
- }));
238
158
  });
239
159
  it("should format fetchEntityHistory properly", async () => {
240
160
  expect.assertions(1);
241
- application = (0, utils_1.CreateTestingApplication)({
242
- Test({ lifecycle, hass }) {
243
- const spy = jest
244
- .spyOn(global, "fetch")
245
- .mockImplementation(async () => {
246
- return {
247
- text: () => "{}",
248
- };
249
- });
250
- lifecycle.onReady(async () => {
251
- await hass.fetch.fetchEntityHistory("switch.porch_light", start, end);
252
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/history/period/${encodeURIComponent(start.toISOString())}?end_time=${encodeURIComponent(end.toISOString())}&filter_entity_id=switch.porch_light`, expect.objectContaining({
253
- method: "get",
254
- }));
255
- });
256
- },
161
+ await hassTestRunner.run(({ lifecycle, hass }) => {
162
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
163
+ return {
164
+ text: () => "{}",
165
+ };
166
+ });
167
+ lifecycle.onReady(async () => {
168
+ await hass.fetch.fetchEntityHistory("switch.porch_light", start, end);
169
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/history/period/${encodeURIComponent(start.toISOString())}?end_time=${encodeURIComponent(end.toISOString())}&filter_entity_id=switch.porch_light`, expect.objectContaining({
170
+ method: "get",
171
+ }));
172
+ });
257
173
  });
258
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
259
- hass: {
260
- AUTO_CONNECT_SOCKET: false,
261
- AUTO_SCAN_CALL_PROXY: false,
262
- BASE_URL,
263
- TOKEN,
264
- },
265
- }));
266
174
  });
267
175
  it("should format fireEvent properly", async () => {
268
176
  expect.assertions(1);
269
177
  const body = { magic: true };
270
- application = (0, utils_1.CreateTestingApplication)({
271
- Test({ lifecycle, hass }) {
272
- const spy = jest
273
- .spyOn(global, "fetch")
274
- .mockImplementation(async () => {
275
- return {
276
- text: () => "{}",
277
- };
278
- });
279
- lifecycle.onReady(async () => {
280
- await hass.fetch.fireEvent("testing_event", body);
281
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/events/testing_event`, expect.objectContaining({
282
- body: JSON.stringify(body),
283
- method: "post",
284
- }));
285
- });
286
- },
178
+ await hassTestRunner.run(({ lifecycle, hass }) => {
179
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
180
+ return {
181
+ text: () => "{}",
182
+ };
183
+ });
184
+ lifecycle.onReady(async () => {
185
+ await hass.fetch.fireEvent("testing_event", body);
186
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/events/testing_event`, expect.objectContaining({
187
+ body: JSON.stringify(body),
188
+ method: "post",
189
+ }));
190
+ });
287
191
  });
288
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
289
- hass: {
290
- AUTO_CONNECT_SOCKET: false,
291
- AUTO_SCAN_CALL_PROXY: false,
292
- BASE_URL,
293
- TOKEN,
294
- },
295
- }));
296
192
  });
297
193
  it("should format getAllEntities properly", async () => {
298
194
  expect.assertions(1);
299
- application = (0, utils_1.CreateTestingApplication)({
300
- Test({ lifecycle, hass }) {
301
- const spy = jest
302
- .spyOn(global, "fetch")
303
- .mockImplementation(async () => {
304
- return {
305
- text: () => "{}",
306
- };
307
- });
308
- lifecycle.onReady(async () => {
309
- await hass.fetch.getAllEntities();
310
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/states`, expect.objectContaining({
311
- method: "get",
312
- }));
313
- });
314
- },
195
+ await hassTestRunner.run(({ lifecycle, hass, mock_assistant }) => {
196
+ lifecycle.onReady(async () => {
197
+ mock_assistant.entity.monkeyReset();
198
+ const spy = jest.spyOn(hass.fetch, "fetch").mockImplementation(async () => []);
199
+ await hass.fetch.getAllEntities();
200
+ expect(spy).toHaveBeenCalledWith({ url: "/api/states" });
201
+ });
315
202
  });
316
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
317
- hass: {
318
- AUTO_CONNECT_SOCKET: false,
319
- AUTO_SCAN_CALL_PROXY: false,
320
- BASE_URL,
321
- TOKEN,
322
- },
323
- }));
324
- });
325
- it("should format getHassConfig properly", async () => {
326
- expect.assertions(1);
327
- application = (0, utils_1.CreateTestingApplication)({
328
- Test({ lifecycle, hass }) {
329
- const spy = jest
330
- .spyOn(global, "fetch")
331
- .mockImplementation(async () => {
332
- return {
333
- text: () => "{}",
334
- };
335
- });
336
- lifecycle.onReady(async () => {
337
- await hass.fetch.getConfig();
338
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/config`, expect.objectContaining({
339
- method: "get",
340
- }));
341
- });
342
- },
343
- });
344
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
345
- hass: {
346
- AUTO_CONNECT_SOCKET: false,
347
- AUTO_SCAN_CALL_PROXY: false,
348
- BASE_URL,
349
- TOKEN,
350
- },
351
- }, false, false));
352
203
  });
204
+ // it("should format getHassConfig properly", async () => {
205
+ // expect.assertions(1);
206
+ // application = CreateTestingApplication({
207
+ // Test({ lifecycle, hass }: TServiceParams) {
208
+ // const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
209
+ // return {
210
+ // text: () => "{}",
211
+ // } as unknown as Response;
212
+ // });
213
+ // lifecycle.onReady(async () => {
214
+ // await hass.fetch.getConfig();
215
+ // expect(spy).toHaveBeenCalledWith(
216
+ // `${BASE_URL}/api/config`,
217
+ // expect.objectContaining({
218
+ // method: "get",
219
+ // }),
220
+ // );
221
+ // });
222
+ // },
223
+ // });
224
+ // await application.bootstrap(
225
+ // SILENT_BOOT(
226
+ // {
227
+ // hass: {
228
+ // AUTO_CONNECT_SOCKET: false,
229
+ // AUTO_SCAN_CALL_PROXY: false,
230
+ // BASE_URL,
231
+ // TOKEN,
232
+ // },
233
+ // },
234
+ // false,
235
+ // false,
236
+ // ),
237
+ // );
238
+ // });
353
239
  it("should format getLogs properly", async () => {
354
240
  expect.assertions(1);
355
- application = (0, utils_1.CreateTestingApplication)({
356
- Test({ lifecycle, hass }) {
357
- const spy = jest
358
- .spyOn(global, "fetch")
359
- .mockImplementation(async () => {
360
- return {
361
- text: () => "[]",
362
- };
363
- });
364
- lifecycle.onReady(async () => {
365
- await hass.fetch.getLogs();
366
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/error/all`, expect.objectContaining({
367
- method: "get",
368
- }));
369
- });
370
- },
241
+ await hassTestRunner.run(({ lifecycle, hass }) => {
242
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
243
+ return {
244
+ text: () => "[]",
245
+ };
246
+ });
247
+ lifecycle.onReady(async () => {
248
+ await hass.fetch.getLogs();
249
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/error/all`, expect.objectContaining({
250
+ method: "get",
251
+ }));
252
+ });
371
253
  });
372
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
373
- hass: {
374
- AUTO_CONNECT_SOCKET: false,
375
- AUTO_SCAN_CALL_PROXY: false,
376
- BASE_URL,
377
- TOKEN,
378
- },
379
- }));
380
254
  });
381
255
  it("should format getRawLogs properly", async () => {
382
256
  expect.assertions(1);
383
- application = (0, utils_1.CreateTestingApplication)({
384
- Test({ lifecycle, hass }) {
385
- const spy = jest
386
- .spyOn(global, "fetch")
387
- .mockImplementation(async () => {
388
- return {
389
- text: () => "{}",
390
- };
391
- });
392
- lifecycle.onReady(async () => {
393
- await hass.fetch.getRawLogs();
394
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/error_log`, expect.objectContaining({
395
- method: "get",
396
- }));
397
- });
398
- },
257
+ await hassTestRunner.run(({ lifecycle, hass }) => {
258
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
259
+ return {
260
+ text: () => "{}",
261
+ };
262
+ });
263
+ lifecycle.onReady(async () => {
264
+ await hass.fetch.getRawLogs();
265
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/error_log`, expect.objectContaining({
266
+ method: "get",
267
+ }));
268
+ });
399
269
  });
400
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
401
- hass: {
402
- AUTO_CONNECT_SOCKET: false,
403
- AUTO_SCAN_CALL_PROXY: false,
404
- BASE_URL,
405
- TOKEN,
406
- },
407
- }));
408
270
  });
409
271
  it("should format listServices properly", async () => {
410
272
  expect.assertions(1);
411
- application = (0, utils_1.CreateTestingApplication)({
412
- Test({ lifecycle, hass }) {
413
- const spy = jest
414
- .spyOn(global, "fetch")
415
- .mockImplementation(async () => {
273
+ await hassTestRunner.run(({ lifecycle, hass, mock_assistant }) => {
274
+ lifecycle.onReady(async () => {
275
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
416
276
  return {
417
277
  text: () => "{}",
418
278
  };
419
279
  });
420
- lifecycle.onReady(async () => {
421
- await hass.fetch.listServices();
422
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/services`, expect.objectContaining({
423
- method: "get",
424
- }));
425
- });
426
- },
280
+ mock_assistant.services.monkeyReset();
281
+ await hass.fetch.listServices();
282
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/services`, expect.objectContaining({
283
+ method: "get",
284
+ }));
285
+ });
427
286
  });
428
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
429
- hass: {
430
- AUTO_CONNECT_SOCKET: false,
431
- AUTO_SCAN_CALL_PROXY: false,
432
- BASE_URL,
433
- TOKEN,
434
- },
435
- }));
436
287
  });
437
288
  it("should format updateEntity properly", async () => {
438
289
  expect.assertions(1);
439
- application = (0, utils_1.CreateTestingApplication)({
440
- Test({ lifecycle, hass }) {
441
- const spy = jest
442
- .spyOn(global, "fetch")
443
- .mockImplementation(async () => {
444
- return {
445
- text: () => "{}",
446
- };
447
- });
448
- lifecycle.onReady(async () => {
449
- await hass.fetch.updateEntity("switch.porch_light", {
290
+ await hassTestRunner.run(({ lifecycle, hass }) => {
291
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
292
+ return {
293
+ text: () => "{}",
294
+ };
295
+ });
296
+ lifecycle.onReady(async () => {
297
+ await hass.fetch.updateEntity("switch.porch_light", {
298
+ attributes: { something: "special" },
299
+ state: "off",
300
+ });
301
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/states/switch.porch_light`, expect.objectContaining({
302
+ body: JSON.stringify({
303
+ // ! DO NOT CHANGE ORDER OF KEYS
304
+ // It changes the resulting JSON, breaking the test
450
305
  attributes: { something: "special" },
451
306
  state: "off",
452
- });
453
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/states/switch.porch_light`, expect.objectContaining({
454
- body: JSON.stringify({
455
- // ! DO NOT CHANGE ORDER OF KEYS
456
- // It changes the resulting JSON, breaking the test
457
- attributes: { something: "special" },
458
- state: "off",
459
- }),
460
- method: "post",
461
- }));
462
- });
463
- },
307
+ }),
308
+ method: "post",
309
+ }));
310
+ });
464
311
  });
465
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
466
- hass: {
467
- AUTO_CONNECT_SOCKET: false,
468
- AUTO_SCAN_CALL_PROXY: false,
469
- BASE_URL,
470
- TOKEN,
471
- },
472
- }));
473
312
  });
474
313
  it("should format webhook properly", async () => {
475
314
  expect.assertions(1);
476
315
  const body = {
477
316
  magic: true,
478
317
  };
479
- application = (0, utils_1.CreateTestingApplication)({
480
- Test({ lifecycle, hass }) {
481
- const spy = jest
482
- .spyOn(global, "fetch")
483
- .mockImplementation(async () => {
484
- return {
485
- text: () => "{}",
486
- };
487
- });
488
- lifecycle.onReady(async () => {
489
- await hass.fetch.webhook("magic_webhook", body);
490
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/webhook/magic_webhook`, expect.objectContaining({
491
- body: JSON.stringify(body),
492
- method: "post",
493
- }));
494
- });
495
- },
318
+ await hassTestRunner.run(({ lifecycle, hass }) => {
319
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
320
+ return {
321
+ text: () => "{}",
322
+ };
323
+ });
324
+ lifecycle.onReady(async () => {
325
+ await hass.fetch.webhook("magic_webhook", body);
326
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/webhook/magic_webhook`, expect.objectContaining({
327
+ body: JSON.stringify(body),
328
+ method: "post",
329
+ }));
330
+ });
496
331
  });
497
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
498
- hass: {
499
- AUTO_CONNECT_SOCKET: false,
500
- AUTO_SCAN_CALL_PROXY: false,
501
- BASE_URL,
502
- TOKEN,
503
- },
504
- }));
505
332
  });
506
333
  it("should format checkCredentials properly", async () => {
507
334
  expect.assertions(1);
508
- application = (0, utils_1.CreateTestingApplication)({
509
- Test({ lifecycle, hass }) {
510
- const spy = jest
511
- .spyOn(global, "fetch")
512
- .mockImplementation(async () => {
513
- return {
514
- text: () => "{}",
515
- };
516
- });
517
- lifecycle.onReady(async () => {
518
- await hass.fetch.checkCredentials();
519
- expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/`, expect.objectContaining({
520
- method: "get",
521
- }));
522
- });
523
- },
335
+ await hassTestRunner.run(({ lifecycle, hass }) => {
336
+ const spy = jest.spyOn(global, "fetch").mockImplementation(async () => {
337
+ return {
338
+ text: () => "{}",
339
+ };
340
+ });
341
+ lifecycle.onReady(async () => {
342
+ await hass.fetch.checkCredentials();
343
+ expect(spy).toHaveBeenCalledWith(`${BASE_URL}/api/`, expect.objectContaining({
344
+ method: "get",
345
+ }));
346
+ });
524
347
  });
525
- await application.bootstrap((0, utils_1.SILENT_BOOT)({
526
- hass: {
527
- AUTO_CONNECT_SOCKET: false,
528
- AUTO_SCAN_CALL_PROXY: false,
529
- BASE_URL,
530
- TOKEN,
531
- },
532
- }));
533
348
  });
534
349
  });
535
350
  });