Debian Codegraph @

tests/test_plugin_homeassistant.py

raw symbols SCIP
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2026, Chris Caron <[email protected]>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 import json
29
30 # Disable logging for a cleaner testing output
31 import logging
32 from unittest import mock
33
34 from helpers import AppriseURLTester
35 import requests
36
37 from apprise import Apprise
38 from apprise.plugins.home_assistant import NotifyHomeAssistant
39
40 logging.disable(logging.CRITICAL)
41
42 # Our Testing URLs
43 apprise_url_tests = (
44 (
45 "hassio://:@/",
46 {
47 "instance": TypeError,
48 },
49 ),
50 (
51 "hassio://",
52 {
53 "instance": TypeError,
54 },
55 ),
56 (
57 "hassios://",
58 {
59 "instance": TypeError,
60 },
61 ),
62 # No Long Lived Access Token specified
63 (
64 "hassio://user@localhost",
65 {
66 "instance": TypeError,
67 },
68 ),
69 (
70 "hassio://localhost/long.lived.token",
71 {
72 "instance": NotifyHomeAssistant,
73 },
74 ),
75 (
76 "hassio://localhost/prefix/path/long.lived.token",
77 {
78 "instance": NotifyHomeAssistant,
79 },
80 ),
81 (
82 "hassio://localhost/long.lived.token?prefix=/ha",
83 {
84 "instance": NotifyHomeAssistant,
85 },
86 ),
87 (
88 "hassio://localhost/service/?token=long.lived.token&prefix=/ha",
89 {
90 "instance": NotifyHomeAssistant,
91 },
92 ),
93 (
94 "hassio://localhost/?token=long.lived.token&prefix=/ha&to=service",
95 {
96 "instance": NotifyHomeAssistant,
97 },
98 ),
99 (
100 "hassio://localhost/service/$%/?token=long.lived.token&prefix=/ha",
101 {
102 # Tests an invalid service entry
103 "instance": NotifyHomeAssistant,
104 },
105 ),
106 (
107 "hassio://localhost/%only%/%invalid%/?token=lng.lived.token&prefix=/ha",
108 {
109 # Tests an invalid service entry
110 "instance": NotifyHomeAssistant,
111 # we'll have a notify response failure in this case
112 "notify_response": False,
113 },
114 ),
115 (
116 "hassio://localhost/service/?accesstoken=long.lived.token",
117 {
118 # accesstoken via query string (?accesstoken=)
119 "instance": NotifyHomeAssistant,
120 },
121 ),
122 (
123 "hassio://user:pass@localhost/long.lived.token/",
124 {
125 "instance": NotifyHomeAssistant,
126 # Our expected url(privacy=True) startswith() response:
127 "privacy_url": "hassio://user:****@localhost/l...n",
128 },
129 ),
130 (
131 "hassio://localhost:80/long.lived.token",
132 {
133 "instance": NotifyHomeAssistant,
134 },
135 ),
136 (
137 "hassio://user@localhost:8123/long.lived.token",
138 {
139 "instance": NotifyHomeAssistant,
140 "privacy_url": "hassio://user@localhost/l...n",
141 },
142 ),
143 (
144 "hassios://localhost/long.lived.token?nid=!%",
145 {
146 # Invalid notification_id
147 "instance": TypeError,
148 },
149 ),
150 (
151 "hassios://localhost/long.lived.token?nid=abcd",
152 {
153 # Valid notification_id
154 "instance": NotifyHomeAssistant,
155 },
156 ),
157 (
158 "hassios://user:pass@localhost/long.lived.token",
159 {
160 "instance": NotifyHomeAssistant,
161 "privacy_url": "hassios://user:****@localhost/l...n",
162 },
163 ),
164 (
165 "hassios://localhost:8443/path/long.lived.token/",
166 {
167 "instance": NotifyHomeAssistant,
168 "privacy_url": "hassios://localhost:8443/l...n",
169 },
170 ),
171 (
172 "hassio://localhost:8123/a/path?token=long.lived.token",
173 {
174 "instance": NotifyHomeAssistant,
175 # Default port; so it's stripped off
176 # token was specified as kwarg
177 "privacy_url": "hassio://localhost/l...n",
178 },
179 ),
180 (
181 "hassios://user:password@localhost:80/long.lived.token/",
182 {
183 "instance": NotifyHomeAssistant,
184 "privacy_url": "hassios://user:****@localhost:80",
185 },
186 ),
187 (
188 "hassio://user:pass@localhost:8123/long.lived.token",
189 {
190 "instance": NotifyHomeAssistant,
191 # force a failure
192 "response": False,
193 "requests_response_code": requests.codes.internal_server_error,
194 },
195 ),
196 (
197 "hassio://user:pass@localhost/long.lived.token",
198 {
199 "instance": NotifyHomeAssistant,
200 # throw a bizzare code forcing us to fail to look it up
201 "response": False,
202 "requests_response_code": 999,
203 },
204 ),
205 (
206 "hassio://user:pass@localhost/long.lived.token",
207 {
208 "instance": NotifyHomeAssistant,
209 # Throws a series of connection and transfer exceptions
210 # when this flag is set and tests that we gracfully handle them
211 "test_requests_exceptions": True,
212 },
213 ),
214 )
215
216
217 def test_plugin_homeassistant_urls():
218 """NotifyHomeAssistant() Apprise URLs."""
219
220 # Run our general tests
221 AppriseURLTester(tests=apprise_url_tests).run_all()
222
223
224 @mock.patch("requests.post")
225 def test_plugin_homeassistant_general(mock_post):
226 """NotifyHomeAssistant() General Checks."""
227
228 response = mock.Mock()
229 response.content = ""
230 response.status_code = requests.codes.ok
231
232 # Prepare Mock
233 mock_post.return_value = response
234
235 # Variation Initializations
236 obj = Apprise.instantiate("hassio://localhost/accesstoken")
237 assert isinstance(obj, NotifyHomeAssistant) is True
238 assert isinstance(obj.url(), str) is True
239
240 # Send Notification
241 assert obj.send(body="test") is True
242
243 assert mock_post.call_count == 1
244 assert (
245 mock_post.call_args_list[0][0][0]
246 == "http://localhost:8123/api/services/persistent_notification/create"
247 )
248
249 # Reset our mock object
250 mock_post.reset_mock()
251
252 # Now let's notify an object
253 obj = Apprise.instantiate("hassio://localhost/long.lived.token/service")
254 assert isinstance(obj, NotifyHomeAssistant) is True
255 assert isinstance(obj.url(), str) is True
256
257 # Send Notification
258 assert obj.send(body="test") is True
259
260 assert (
261 mock_post.call_args_list[0][0][0]
262 == "http://localhost:8123/api/services/notify/service"
263 )
264 posted_json = json.loads(mock_post.call_args_list[0][1]["data"])
265 assert "notification_id" not in posted_json
266 assert "targets" not in posted_json
267 assert "message" in posted_json
268 assert posted_json["message"] == "test"
269 assert "title" in posted_json
270 assert posted_json["title"] == ""
271
272 # Reset our mock object
273 mock_post.reset_mock()
274
275 #
276 # No Batch Processing
277 #
278
279 # Now let's notify an object
280 obj = Apprise.instantiate(
281 "hassio://localhost/long.lived.token/serviceA:target1,target2/"
282 "service2/domain1.service3?batch=no"
283 )
284 assert isinstance(obj, NotifyHomeAssistant) is True
285 assert isinstance(obj.url(), str) is True
286
287 # Send Notification
288 assert obj.send(body="test-body", title="title") is True
289
290 # Entries are split apart
291 assert len(obj) == 4
292 assert mock_post.call_count == 4
293
294 assert (
295 mock_post.call_args_list[0][0][0]
296 == "http://localhost:8123/api/services/domain1/service3"
297 )
298 posted_json = json.loads(mock_post.call_args_list[0][1]["data"])
299 assert "notification_id" not in posted_json
300 assert "targets" not in posted_json
301 assert "message" in posted_json
302 assert posted_json["message"] == "test-body"
303 assert "title" in posted_json
304 assert posted_json["title"] == "title"
305
306 assert (
307 mock_post.call_args_list[1][0][0]
308 == "http://localhost:8123/api/services/notify/service2"
309 )
310 posted_json = json.loads(mock_post.call_args_list[1][1]["data"])
311 assert "notification_id" not in posted_json
312 assert "targets" not in posted_json
313 assert "message" in posted_json
314 assert posted_json["message"] == "test-body"
315 assert "title" in posted_json
316 assert posted_json["title"] == "title"
317
318 assert (
319 mock_post.call_args_list[2][0][0]
320 == "http://localhost:8123/api/services/notify/serviceA"
321 )
322 posted_json = json.loads(mock_post.call_args_list[2][1]["data"])
323 assert "notification_id" not in posted_json
324 assert "targets" in posted_json
325 assert isinstance(posted_json["targets"], list)
326 assert len(posted_json["targets"]) == 1
327 assert "target1" in posted_json["targets"]
328 assert "message" in posted_json
329 assert posted_json["message"] == "test-body"
330 assert "title" in posted_json
331 assert posted_json["title"] == "title"
332
333 assert (
334 mock_post.call_args_list[3][0][0]
335 == "http://localhost:8123/api/services/notify/serviceA"
336 )
337 posted_json = json.loads(mock_post.call_args_list[3][1]["data"])
338 assert "notification_id" not in posted_json
339 assert "targets" in posted_json
340 assert isinstance(posted_json["targets"], list)
341 assert len(posted_json["targets"]) == 1
342 assert "target2" in posted_json["targets"]
343 assert "message" in posted_json
344 assert posted_json["message"] == "test-body"
345 assert "title" in posted_json
346 assert posted_json["title"] == "title"
347
348 # Reset our mock object
349 mock_post.reset_mock()
350
351 #
352 # Batch Processing
353 #
354
355 # Now let's notify an object
356 obj = Apprise.instantiate(
357 "hassio://localhost/long.lived.token/serviceA:target1,target2/"
358 "service2/domain1.service3?batch=yes"
359 )
360 assert isinstance(obj, NotifyHomeAssistant) is True
361 assert isinstance(obj.url(), str) is True
362
363 # Send Notification
364 assert obj.send(body="test-body", title="title") is True
365
366 # Entries targets can be grouped
367 assert len(obj) == 3
368 assert mock_post.call_count == 3
369
370 assert (
371 mock_post.call_args_list[0][0][0]
372 == "http://localhost:8123/api/services/domain1/service3"
373 )
374 posted_json = json.loads(mock_post.call_args_list[0][1]["data"])
375 assert "notification_id" not in posted_json
376 assert "targets" not in posted_json
377 assert "message" in posted_json
378 assert posted_json["message"] == "test-body"
379 assert "title" in posted_json
380 assert posted_json["title"] == "title"
381
382 assert (
383 mock_post.call_args_list[1][0][0]
384 == "http://localhost:8123/api/services/notify/service2"
385 )
386 posted_json = json.loads(mock_post.call_args_list[1][1]["data"])
387 assert "notification_id" not in posted_json
388 assert "targets" not in posted_json
389 assert "message" in posted_json
390 assert posted_json["message"] == "test-body"
391 assert "title" in posted_json
392 assert posted_json["title"] == "title"
393
394 assert (
395 mock_post.call_args_list[2][0][0]
396 == "http://localhost:8123/api/services/notify/serviceA"
397 )
398 posted_json = json.loads(mock_post.call_args_list[2][1]["data"])
399 assert "notification_id" not in posted_json
400 assert "targets" in posted_json
401 assert isinstance(posted_json["targets"], list)
402 # Our batch groups our targets
403 assert len(posted_json["targets"]) == 2
404 assert "target1" in posted_json["targets"]
405 assert "target2" in posted_json["targets"]
406 assert "message" in posted_json
407 assert posted_json["message"] == "test-body"
408 assert "title" in posted_json
409 assert posted_json["title"] == "title"
410
411 # Reset our mock object
412 mock_post.reset_mock()
413
414 #
415 # Test error handling on multi-query request
416 #
417
418 # Now let's notify an object
419 obj = Apprise.instantiate(
420 "hassio://localhost/long.lived.token/serviceA:target1,target2/"
421 "service2:target3,target4,target5,target6?batch=no"
422 )
423
424 assert isinstance(obj, NotifyHomeAssistant) is True
425 assert isinstance(obj.url(), str) is True
426
427 bad_response = mock.Mock()
428 bad_response.content = ""
429 bad_response.status_code = requests.codes.not_found
430
431 mock_post.side_effect = (response, bad_response)
432
433 # We will fail on our second message sent
434 assert obj.send(body="test-body", title="title") is False