| 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.exception import AppriseImproperlyConfigured |
| 39 |
from apprise.plugins.home_assistant import NotifyHomeAssistant |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
# Our Testing URLs |
| 44 |
apprise_url_tests = ( |
| 45 |
( |
| 46 |
"hassio://:@/", |
| 47 |
{ |
| 48 |
"instance": AppriseImproperlyConfigured, |
| 49 |
}, |
| 50 |
), |
| 51 |
( |
| 52 |
"hassio://", |
| 53 |
{ |
| 54 |
"instance": AppriseImproperlyConfigured, |
| 55 |
}, |
| 56 |
), |
| 57 |
( |
| 58 |
"hassios://", |
| 59 |
{ |
| 60 |
"instance": AppriseImproperlyConfigured, |
| 61 |
}, |
| 62 |
), |
| 63 |
# No Long Lived Access Token specified |
| 64 |
( |
| 65 |
"hassio://user@localhost", |
| 66 |
{ |
| 67 |
"instance": AppriseImproperlyConfigured, |
| 68 |
}, |
| 69 |
), |
| 70 |
( |
| 71 |
"hassio://localhost/long.lived.token", |
| 72 |
{ |
| 73 |
"instance": NotifyHomeAssistant, |
| 74 |
}, |
| 75 |
), |
| 76 |
( |
| 77 |
"hassio://localhost/prefix/path/long.lived.token", |
| 78 |
{ |
| 79 |
"instance": NotifyHomeAssistant, |
| 80 |
}, |
| 81 |
), |
| 82 |
( |
| 83 |
"hassio://localhost/long.lived.token?prefix=/ha", |
| 84 |
{ |
| 85 |
"instance": NotifyHomeAssistant, |
| 86 |
}, |
| 87 |
), |
| 88 |
( |
| 89 |
"hassio://localhost/service/?token=long.lived.token&prefix=/ha", |
| 90 |
{ |
| 91 |
"instance": NotifyHomeAssistant, |
| 92 |
}, |
| 93 |
), |
| 94 |
( |
| 95 |
"hassio://localhost/?token=long.lived.token&prefix=/ha&to=service", |
| 96 |
{ |
| 97 |
"instance": NotifyHomeAssistant, |
| 98 |
}, |
| 99 |
), |
| 100 |
( |
| 101 |
"hassio://localhost/service/$%/?token=long.lived.token&prefix=/ha", |
| 102 |
{ |
| 103 |
# Tests an invalid service entry |
| 104 |
"instance": NotifyHomeAssistant, |
| 105 |
}, |
| 106 |
), |
| 107 |
( |
| 108 |
"hassio://localhost/%only%/%invalid%/?token=lng.lived.token&prefix=/ha", |
| 109 |
{ |
| 110 |
# Tests an invalid service entry |
| 111 |
"instance": NotifyHomeAssistant, |
| 112 |
# we'll have a notify response failure in this case |
| 113 |
"notify_response": False, |
| 114 |
}, |
| 115 |
), |
| 116 |
( |
| 117 |
"hassio://localhost/service/?accesstoken=long.lived.token", |
| 118 |
{ |
| 119 |
# accesstoken via query string (?accesstoken=) |
| 120 |
"instance": NotifyHomeAssistant, |
| 121 |
}, |
| 122 |
), |
| 123 |
( |
| 124 |
"hassio://user:pass@localhost/long.lived.token/", |
| 125 |
{ |
| 126 |
"instance": NotifyHomeAssistant, |
| 127 |
# Our expected url(privacy=True) startswith() response: |
| 128 |
"privacy_url": "hassio://user:****@localhost/l...n", |
| 129 |
}, |
| 130 |
), |
| 131 |
( |
| 132 |
"hassio://localhost:80/long.lived.token", |
| 133 |
{ |
| 134 |
"instance": NotifyHomeAssistant, |
| 135 |
}, |
| 136 |
), |
| 137 |
( |
| 138 |
"hassio://user@localhost:8123/long.lived.token", |
| 139 |
{ |
| 140 |
"instance": NotifyHomeAssistant, |
| 141 |
"privacy_url": "hassio://user@localhost/l...n", |
| 142 |
}, |
| 143 |
), |
| 144 |
( |
| 145 |
"hassios://localhost/long.lived.token?nid=!%", |
| 146 |
{ |
| 147 |
# Invalid notification_id |
| 148 |
"instance": AppriseImproperlyConfigured, |
| 149 |
}, |
| 150 |
), |
| 151 |
( |
| 152 |
"hassios://localhost/long.lived.token?nid=abcd", |
| 153 |
{ |
| 154 |
# Valid notification_id |
| 155 |
"instance": NotifyHomeAssistant, |
| 156 |
}, |
| 157 |
), |
| 158 |
( |
| 159 |
"hassios://user:pass@localhost/long.lived.token", |
| 160 |
{ |
| 161 |
"instance": NotifyHomeAssistant, |
| 162 |
"privacy_url": "hassios://user:****@localhost/l...n", |
| 163 |
}, |
| 164 |
), |
| 165 |
( |
| 166 |
"hassios://localhost:8443/path/long.lived.token/", |
| 167 |
{ |
| 168 |
"instance": NotifyHomeAssistant, |
| 169 |
"privacy_url": "hassios://localhost:8443/l...n", |
| 170 |
}, |
| 171 |
), |
| 172 |
( |
| 173 |
"hassio://localhost:8123/a/path?token=long.lived.token", |
| 174 |
{ |
| 175 |
"instance": NotifyHomeAssistant, |
| 176 |
# Default port; so it's stripped off |
| 177 |
# token was specified as kwarg |
| 178 |
"privacy_url": "hassio://localhost/l...n", |
| 179 |
}, |
| 180 |
), |
| 181 |
( |
| 182 |
"hassios://user:password@localhost:80/long.lived.token/", |
| 183 |
{ |
| 184 |
"instance": NotifyHomeAssistant, |
| 185 |
"privacy_url": "hassios://user:****@localhost:80", |
| 186 |
}, |
| 187 |
), |
| 188 |
( |
| 189 |
"hassio://user:pass@localhost:8123/long.lived.token", |
| 190 |
{ |
| 191 |
"instance": NotifyHomeAssistant, |
| 192 |
# force a failure |
| 193 |
"response": False, |
| 194 |
"requests_response_code": requests.codes.internal_server_error, |
| 195 |
}, |
| 196 |
), |
| 197 |
( |
| 198 |
"hassio://user:pass@localhost/long.lived.token", |
| 199 |
{ |
| 200 |
"instance": NotifyHomeAssistant, |
| 201 |
# throw a bizzare code forcing us to fail to look it up |
| 202 |
"response": False, |
| 203 |
"requests_response_code": 999, |
| 204 |
}, |
| 205 |
), |
| 206 |
( |
| 207 |
"hassio://user:pass@localhost/long.lived.token", |
| 208 |
{ |
| 209 |
"instance": NotifyHomeAssistant, |
| 210 |
# Throws a series of connection and transfer exceptions |
| 211 |
# when this flag is set and tests that we gracfully handle them |
| 212 |
"test_requests_exceptions": True, |
| 213 |
}, |
| 214 |
), |
| 215 |
) |
| 216 |
|
| 217 |
|
| 218 |
def test_plugin_homeassistant_urls(): |
| 219 |
"""NotifyHomeAssistant() Apprise URLs.""" |
| 220 |
|
| 221 |
# Run our general tests |
| 222 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 223 |
|
| 224 |
|
| 225 |
@mock.patch("requests.post") |
| 226 |
def test_plugin_homeassistant_general(mock_post): |
| 227 |
"""NotifyHomeAssistant() General Checks.""" |
| 228 |
|
| 229 |
response = mock.Mock() |
| 230 |
response.content = "" |
| 231 |
response.status_code = requests.codes.ok |
| 232 |
|
| 233 |
# Prepare Mock |
| 234 |
mock_post.return_value = response |
| 235 |
|
| 236 |
# Variation Initializations |
| 237 |
obj = Apprise.instantiate("hassio://localhost/accesstoken") |
| 238 |
assert isinstance(obj, NotifyHomeAssistant) is True |
| 239 |
assert isinstance(obj.url(), str) is True |
| 240 |
|
| 241 |
# Send Notification |
| 242 |
assert obj.send(body="test") is True |
| 243 |
|
| 244 |
assert mock_post.call_count == 1 |
| 245 |
assert ( |
| 246 |
mock_post.call_args_list[0][0][0] |
| 247 |
== "http://localhost:8123/api/services/persistent_notification/create" |
| 248 |
) |
| 249 |
|
| 250 |
# Reset our mock object |
| 251 |
mock_post.reset_mock() |
| 252 |
|
| 253 |
# Now let's notify an object |
| 254 |
obj = Apprise.instantiate("hassio://localhost/long.lived.token/service") |
| 255 |
assert isinstance(obj, NotifyHomeAssistant) is True |
| 256 |
assert isinstance(obj.url(), str) is True |
| 257 |
|
| 258 |
# Send Notification |
| 259 |
assert obj.send(body="test") is True |
| 260 |
|
| 261 |
assert ( |
| 262 |
mock_post.call_args_list[0][0][0] |
| 263 |
== "http://localhost:8123/api/services/notify/service" |
| 264 |
) |
| 265 |
posted_json = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 266 |
assert "notification_id" not in posted_json |
| 267 |
assert "targets" not in posted_json |
| 268 |
assert "message" in posted_json |
| 269 |
assert posted_json["message"] == "test" |
| 270 |
assert "title" in posted_json |
| 271 |
assert posted_json["title"] == "" |
| 272 |
|
| 273 |
# Reset our mock object |
| 274 |
mock_post.reset_mock() |
| 275 |
|
| 276 |
# |
| 277 |
# No Batch Processing |
| 278 |
# |
| 279 |
|
| 280 |
# Now let's notify an object |
| 281 |
obj = Apprise.instantiate( |
| 282 |
"hassio://localhost/long.lived.token/serviceA:target1,target2/" |
| 283 |
"service2/domain1.service3?batch=no" |
| 284 |
) |
| 285 |
assert isinstance(obj, NotifyHomeAssistant) is True |
| 286 |
assert isinstance(obj.url(), str) is True |
| 287 |
|
| 288 |
# Send Notification |
| 289 |
assert obj.send(body="test-body", title="title") is True |
| 290 |
|
| 291 |
# Entries are split apart |
| 292 |
assert len(obj) == 4 |
| 293 |
assert mock_post.call_count == 4 |
| 294 |
|
| 295 |
assert ( |
| 296 |
mock_post.call_args_list[0][0][0] |
| 297 |
== "http://localhost:8123/api/services/domain1/service3" |
| 298 |
) |
| 299 |
posted_json = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 300 |
assert "notification_id" not in posted_json |
| 301 |
assert "targets" not in posted_json |
| 302 |
assert "message" in posted_json |
| 303 |
assert posted_json["message"] == "test-body" |
| 304 |
assert "title" in posted_json |
| 305 |
assert posted_json["title"] == "title" |
| 306 |
|
| 307 |
assert ( |
| 308 |
mock_post.call_args_list[1][0][0] |
| 309 |
== "http://localhost:8123/api/services/notify/service2" |
| 310 |
) |
| 311 |
posted_json = json.loads(mock_post.call_args_list[1][1]["data"]) |
| 312 |
assert "notification_id" not in posted_json |
| 313 |
assert "targets" not in posted_json |
| 314 |
assert "message" in posted_json |
| 315 |
assert posted_json["message"] == "test-body" |
| 316 |
assert "title" in posted_json |
| 317 |
assert posted_json["title"] == "title" |
| 318 |
|
| 319 |
assert ( |
| 320 |
mock_post.call_args_list[2][0][0] |
| 321 |
== "http://localhost:8123/api/services/notify/serviceA" |
| 322 |
) |
| 323 |
posted_json = json.loads(mock_post.call_args_list[2][1]["data"]) |
| 324 |
assert "notification_id" not in posted_json |
| 325 |
assert "targets" in posted_json |
| 326 |
assert isinstance(posted_json["targets"], list) |
| 327 |
assert len(posted_json["targets"]) == 1 |
| 328 |
assert "target1" in posted_json["targets"] |
| 329 |
assert "message" in posted_json |
| 330 |
assert posted_json["message"] == "test-body" |
| 331 |
assert "title" in posted_json |
| 332 |
assert posted_json["title"] == "title" |
| 333 |
|
| 334 |
assert ( |
| 335 |
mock_post.call_args_list[3][0][0] |
| 336 |
== "http://localhost:8123/api/services/notify/serviceA" |
| 337 |
) |
| 338 |
posted_json = json.loads(mock_post.call_args_list[3][1]["data"]) |
| 339 |
assert "notification_id" not in posted_json |
| 340 |
assert "targets" in posted_json |
| 341 |
assert isinstance(posted_json["targets"], list) |
| 342 |
assert len(posted_json["targets"]) == 1 |
| 343 |
assert "target2" in posted_json["targets"] |
| 344 |
assert "message" in posted_json |
| 345 |
assert posted_json["message"] == "test-body" |
| 346 |
assert "title" in posted_json |
| 347 |
assert posted_json["title"] == "title" |
| 348 |
|
| 349 |
# Reset our mock object |
| 350 |
mock_post.reset_mock() |
| 351 |
|
| 352 |
# |
| 353 |
# Batch Processing |
| 354 |
# |
| 355 |
|
| 356 |
# Now let's notify an object |
| 357 |
obj = Apprise.instantiate( |
| 358 |
"hassio://localhost/long.lived.token/serviceA:target1,target2/" |
| 359 |
"service2/domain1.service3?batch=yes" |
| 360 |
) |
| 361 |
assert isinstance(obj, NotifyHomeAssistant) is True |
| 362 |
assert isinstance(obj.url(), str) is True |
| 363 |
|
| 364 |
# Send Notification |
| 365 |
assert obj.send(body="test-body", title="title") is True |
| 366 |
|
| 367 |
# Entries targets can be grouped |
| 368 |
assert len(obj) == 3 |
| 369 |
assert mock_post.call_count == 3 |
| 370 |
|
| 371 |
assert ( |
| 372 |
mock_post.call_args_list[0][0][0] |
| 373 |
== "http://localhost:8123/api/services/domain1/service3" |
| 374 |
) |
| 375 |
posted_json = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 376 |
assert "notification_id" not in posted_json |
| 377 |
assert "targets" not in posted_json |
| 378 |
assert "message" in posted_json |
| 379 |
assert posted_json["message"] == "test-body" |
| 380 |
assert "title" in posted_json |
| 381 |
assert posted_json["title"] == "title" |
| 382 |
|
| 383 |
assert ( |
| 384 |
mock_post.call_args_list[1][0][0] |
| 385 |
== "http://localhost:8123/api/services/notify/service2" |
| 386 |
) |
| 387 |
posted_json = json.loads(mock_post.call_args_list[1][1]["data"]) |
| 388 |
assert "notification_id" not in posted_json |
| 389 |
assert "targets" not in posted_json |
| 390 |
assert "message" in posted_json |
| 391 |
assert posted_json["message"] == "test-body" |
| 392 |
assert "title" in posted_json |
| 393 |
assert posted_json["title"] == "title" |
| 394 |
|
| 395 |
assert ( |
| 396 |
mock_post.call_args_list[2][0][0] |
| 397 |
== "http://localhost:8123/api/services/notify/serviceA" |
| 398 |
) |
| 399 |
posted_json = json.loads(mock_post.call_args_list[2][1]["data"]) |
| 400 |
assert "notification_id" not in posted_json |
| 401 |
assert "targets" in posted_json |
| 402 |
assert isinstance(posted_json["targets"], list) |
| 403 |
# Our batch groups our targets |
| 404 |
assert len(posted_json["targets"]) == 2 |
| 405 |
assert "target1" in posted_json["targets"] |
| 406 |
assert "target2" in posted_json["targets"] |
| 407 |
assert "message" in posted_json |
| 408 |
assert posted_json["message"] == "test-body" |
| 409 |
assert "title" in posted_json |
| 410 |
assert posted_json["title"] == "title" |
| 411 |
|
| 412 |
# Reset our mock object |
| 413 |
mock_post.reset_mock() |
| 414 |
|
| 415 |
# |
| 416 |
# Test error handling on multi-query request |
| 417 |
# |
| 418 |
|
| 419 |
# Now let's notify an object |
| 420 |
obj = Apprise.instantiate( |
| 421 |
"hassio://localhost/long.lived.token/serviceA:target1,target2/" |
| 422 |
"service2:target3,target4,target5,target6?batch=no" |
| 423 |
) |
| 424 |
|
| 425 |
assert isinstance(obj, NotifyHomeAssistant) is True |
| 426 |
assert isinstance(obj.url(), str) is True |
| 427 |
|
| 428 |
bad_response = mock.Mock() |
| 429 |
bad_response.content = "" |
| 430 |
bad_response.status_code = requests.codes.not_found |
| 431 |
|
| 432 |
mock_post.side_effect = (response, bad_response) |
| 433 |
|
| 434 |
# We will fail on our second message sent |
| 435 |
assert obj.send(body="test-body", title="title") is False |