| 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 logging |
| 29 |
import sys |
| 30 |
from unittest import mock |
| 31 |
|
| 32 |
from helpers import AppriseURLTester |
| 33 |
import pytest |
| 34 |
|
| 35 |
from apprise import Apprise, NotifyType |
| 36 |
from apprise.exception import AppriseImproperlyConfigured |
| 37 |
from apprise.plugins.blink1 import ( |
| 38 |
BLINK1_DEFAULT_DURATION_MS, |
| 39 |
BLINK1_DEFAULT_FADE_MS, |
| 40 |
BLINK1_MAX_DURATION_MS, |
| 41 |
BLINK1_MAX_FADE_MS, |
| 42 |
BLINK1_REPORT_SIZE, |
| 43 |
Blink1LED, |
| 44 |
NotifyBlink1, |
| 45 |
_blink1_fade_buf, |
| 46 |
) |
| 47 |
|
| 48 |
logging.disable(logging.CRITICAL) |
| 49 |
|
| 50 |
# --------------------------------------------------------------------------- |
| 51 |
# Helpers |
| 52 |
# --------------------------------------------------------------------------- |
| 53 |
|
| 54 |
|
| 55 |
def _mk_device(report_size=BLINK1_REPORT_SIZE, open_exc=None): |
| 56 |
"""Return a mock hidapi device that succeeds by default.""" |
| 57 |
dev = mock.Mock() |
| 58 |
dev.send_feature_report.return_value = report_size |
| 59 |
if open_exc is not None: |
| 60 |
dev.open.side_effect = open_exc |
| 61 |
return dev |
| 62 |
|
| 63 |
|
| 64 |
# --------------------------------------------------------------------------- |
| 65 |
# URL matrix used by AppriseURLTester |
| 66 |
# --------------------------------------------------------------------------- |
| 67 |
|
| 68 |
apprise_url_tests = ( |
| 69 |
# First available device, all defaults |
| 70 |
( |
| 71 |
"blink1://", |
| 72 |
{ |
| 73 |
"instance": NotifyBlink1, |
| 74 |
}, |
| 75 |
), |
| 76 |
# Explicit underscore placeholder for first device |
| 77 |
( |
| 78 |
"blink1://_/", |
| 79 |
{ |
| 80 |
"instance": NotifyBlink1, |
| 81 |
}, |
| 82 |
), |
| 83 |
# Specific device by serial number |
| 84 |
( |
| 85 |
"blink1://ABCD1234/", |
| 86 |
{ |
| 87 |
"instance": NotifyBlink1, |
| 88 |
}, |
| 89 |
), |
| 90 |
# Minimum allowed parameter values |
| 91 |
( |
| 92 |
"blink1://?duration=0&fade=0&ledn=0", |
| 93 |
{ |
| 94 |
"instance": NotifyBlink1, |
| 95 |
}, |
| 96 |
), |
| 97 |
# LED 1 (first LED only) |
| 98 |
( |
| 99 |
"blink1://?ledn=1", |
| 100 |
{ |
| 101 |
"instance": NotifyBlink1, |
| 102 |
}, |
| 103 |
), |
| 104 |
# LED 2 (second LED only) |
| 105 |
( |
| 106 |
"blink1://?ledn=2", |
| 107 |
{ |
| 108 |
"instance": NotifyBlink1, |
| 109 |
}, |
| 110 |
), |
| 111 |
# Maximum allowed duration |
| 112 |
( |
| 113 |
f"blink1://?duration={BLINK1_MAX_DURATION_MS}", |
| 114 |
{ |
| 115 |
"instance": NotifyBlink1, |
| 116 |
}, |
| 117 |
), |
| 118 |
# Maximum allowed fade |
| 119 |
( |
| 120 |
f"blink1://?fade={BLINK1_MAX_FADE_MS}", |
| 121 |
{ |
| 122 |
"instance": NotifyBlink1, |
| 123 |
}, |
| 124 |
), |
| 125 |
# Non-numeric duration |
| 126 |
( |
| 127 |
"blink1://?duration=abc", |
| 128 |
{ |
| 129 |
"instance": AppriseImproperlyConfigured, |
| 130 |
}, |
| 131 |
), |
| 132 |
# Negative duration |
| 133 |
( |
| 134 |
"blink1://?duration=-1", |
| 135 |
{ |
| 136 |
"instance": AppriseImproperlyConfigured, |
| 137 |
}, |
| 138 |
), |
| 139 |
# Duration above the maximum |
| 140 |
( |
| 141 |
f"blink1://?duration={BLINK1_MAX_DURATION_MS + 1}", |
| 142 |
{ |
| 143 |
"instance": AppriseImproperlyConfigured, |
| 144 |
}, |
| 145 |
), |
| 146 |
# Non-numeric fade |
| 147 |
( |
| 148 |
"blink1://?fade=abc", |
| 149 |
{ |
| 150 |
"instance": AppriseImproperlyConfigured, |
| 151 |
}, |
| 152 |
), |
| 153 |
# Negative fade |
| 154 |
( |
| 155 |
"blink1://?fade=-1", |
| 156 |
{ |
| 157 |
"instance": AppriseImproperlyConfigured, |
| 158 |
}, |
| 159 |
), |
| 160 |
# Fade above the maximum |
| 161 |
( |
| 162 |
f"blink1://?fade={BLINK1_MAX_FADE_MS + 1}", |
| 163 |
{ |
| 164 |
"instance": AppriseImproperlyConfigured, |
| 165 |
}, |
| 166 |
), |
| 167 |
) |
| 168 |
|
| 169 |
|
| 170 |
# --------------------------------------------------------------------------- |
| 171 |
# Import-error path (run only when hidapi is NOT installed) |
| 172 |
# --------------------------------------------------------------------------- |
| 173 |
|
| 174 |
|
| 175 |
@pytest.mark.skipif( |
| 176 |
"hid" in sys.modules, |
| 177 |
reason="Requires that hidapi NOT be installed", |
| 178 |
) |
| 179 |
def test_plugin_blink1_import_error(): |
| 180 |
"""NotifyBlink1() returns None when hidapi is absent.""" |
| 181 |
|
| 182 |
obj = Apprise.instantiate("blink1://") |
| 183 |
assert obj is None |
| 184 |
|
| 185 |
|
| 186 |
# --------------------------------------------------------------------------- |
| 187 |
# All remaining tests require hidapi to be installed |
| 188 |
# --------------------------------------------------------------------------- |
| 189 |
|
| 190 |
|
| 191 |
def test_plugin_blink1_urls(): |
| 192 |
"""NotifyBlink1() Apprise URL matrix.""" |
| 193 |
|
| 194 |
pytest.importorskip("hid") |
| 195 |
|
| 196 |
with ( |
| 197 |
mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid, |
| 198 |
mock.patch("apprise.plugins.blink1.time") as mock_time, |
| 199 |
): |
| 200 |
mock_dev = _mk_device() |
| 201 |
mock_hid.device.return_value = mock_dev |
| 202 |
mock_time.sleep.return_value = None |
| 203 |
|
| 204 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 205 |
|
| 206 |
|
| 207 |
def test_plugin_blink1_device_open_failure(): |
| 208 |
"""NotifyBlink1() returns False when the USB device cannot be opened.""" |
| 209 |
|
| 210 |
pytest.importorskip("hid") |
| 211 |
|
| 212 |
with mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid: |
| 213 |
mock_dev = _mk_device(open_exc=OSError("no device")) |
| 214 |
mock_hid.device.return_value = mock_dev |
| 215 |
|
| 216 |
obj = Apprise.instantiate("blink1://ABCD1234/") |
| 217 |
assert obj is not None |
| 218 |
assert bool(obj.notify(body="test")) is False |
| 219 |
|
| 220 |
|
| 221 |
def test_plugin_blink1_device_open_failure_no_serial(): |
| 222 |
"""NotifyBlink1() failure log omits serial when none is configured.""" |
| 223 |
|
| 224 |
pytest.importorskip("hid") |
| 225 |
|
| 226 |
with mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid: |
| 227 |
mock_dev = _mk_device(open_exc=OSError("no device")) |
| 228 |
mock_hid.device.return_value = mock_dev |
| 229 |
|
| 230 |
obj = Apprise.instantiate("blink1://") |
| 231 |
assert obj is not None |
| 232 |
assert bool(obj.notify(body="test")) is False |
| 233 |
|
| 234 |
|
| 235 |
def test_plugin_blink1_send_feature_report_exception(): |
| 236 |
"""NotifyBlink1() returns False and closes device on HID write error.""" |
| 237 |
|
| 238 |
pytest.importorskip("hid") |
| 239 |
|
| 240 |
with ( |
| 241 |
mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid, |
| 242 |
mock.patch("apprise.plugins.blink1.time") as mock_time, |
| 243 |
): |
| 244 |
mock_dev = mock.Mock() |
| 245 |
mock_dev.send_feature_report.side_effect = OSError("USB error") |
| 246 |
mock_hid.device.return_value = mock_dev |
| 247 |
mock_time.sleep.return_value = None |
| 248 |
|
| 249 |
obj = Apprise.instantiate("blink1://") |
| 250 |
assert obj is not None |
| 251 |
assert bool(obj.notify(body="test")) is False |
| 252 |
|
| 253 |
# close() must always be called |
| 254 |
assert mock_dev.close.called |
| 255 |
|
| 256 |
|
| 257 |
def test_plugin_blink1_short_report_fails(): |
| 258 |
"""NotifyBlink1() returns False when the HID report is truncated.""" |
| 259 |
|
| 260 |
pytest.importorskip("hid") |
| 261 |
|
| 262 |
with ( |
| 263 |
mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid, |
| 264 |
mock.patch("apprise.plugins.blink1.time") as mock_time, |
| 265 |
): |
| 266 |
mock_dev = _mk_device(report_size=BLINK1_REPORT_SIZE - 1) |
| 267 |
mock_hid.device.return_value = mock_dev |
| 268 |
mock_time.sleep.return_value = None |
| 269 |
|
| 270 |
obj = Apprise.instantiate("blink1://") |
| 271 |
assert obj is not None |
| 272 |
assert bool(obj.notify(body="test")) is False |
| 273 |
|
| 274 |
|
| 275 |
def test_plugin_blink1_notify_types(): |
| 276 |
"""NotifyBlink1() sends one fade per notification type.""" |
| 277 |
|
| 278 |
pytest.importorskip("hid") |
| 279 |
|
| 280 |
with ( |
| 281 |
mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid, |
| 282 |
mock.patch("apprise.plugins.blink1.time") as mock_time, |
| 283 |
): |
| 284 |
mock_dev = _mk_device() |
| 285 |
mock_hid.device.return_value = mock_dev |
| 286 |
mock_time.sleep.return_value = None |
| 287 |
|
| 288 |
obj = Apprise.instantiate("blink1://ABCD1234/?fade=100&duration=50") |
| 289 |
assert obj is not None |
| 290 |
|
| 291 |
for notify_type in ( |
| 292 |
NotifyType.INFO, |
| 293 |
NotifyType.SUCCESS, |
| 294 |
NotifyType.WARNING, |
| 295 |
NotifyType.FAILURE, |
| 296 |
): |
| 297 |
assert ( |
| 298 |
bool(obj.notify(body="test", notify_type=notify_type)) is True |
| 299 |
) |
| 300 |
|
| 301 |
# Each notification sends two reports (color + off) |
| 302 |
assert mock_dev.send_feature_report.call_count == 8 |
| 303 |
assert mock_dev.close.call_count == 4 |
| 304 |
|
| 305 |
|
| 306 |
def test_plugin_blink1_fade_buf_layout(): |
| 307 |
"""_blink1_fade_buf() constructs the correct 9-byte HID payload.""" |
| 308 |
|
| 309 |
buf = _blink1_fade_buf(255, 128, 0, 1000, ledn=1) |
| 310 |
|
| 311 |
assert len(buf) == 9 |
| 312 |
assert buf[0] == 0x01 # REPORT_ID |
| 313 |
assert buf[1] == ord("c") # command byte |
| 314 |
assert buf[2] == 255 # red |
| 315 |
assert buf[3] == 128 # green |
| 316 |
assert buf[4] == 0 # blue |
| 317 |
# fade_time = 1000 // 10 = 100 = 0x0064 |
| 318 |
assert buf[5] == 0x00 # th |
| 319 |
assert buf[6] == 0x64 # tl |
| 320 |
assert buf[7] == 1 # ledn |
| 321 |
assert buf[8] == 0x00 # padding |
| 322 |
|
| 323 |
|
| 324 |
def test_plugin_blink1_fade_rgb_args(): |
| 325 |
"""NotifyBlink1() passes correct ledn and RGB values to the device.""" |
| 326 |
|
| 327 |
pytest.importorskip("hid") |
| 328 |
|
| 329 |
with ( |
| 330 |
mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid, |
| 331 |
mock.patch("apprise.plugins.blink1.time") as mock_time, |
| 332 |
): |
| 333 |
mock_dev = _mk_device() |
| 334 |
mock_hid.device.return_value = mock_dev |
| 335 |
mock_time.sleep.return_value = None |
| 336 |
|
| 337 |
obj = Apprise.instantiate("blink1://?fade=200&duration=100&ledn=2") |
| 338 |
assert obj is not None |
| 339 |
assert ( |
| 340 |
bool(obj.notify(body="test", notify_type=NotifyType.INFO)) is True |
| 341 |
) |
| 342 |
|
| 343 |
first_call_buf = mock_dev.send_feature_report.call_args_list[0][0][0] |
| 344 |
assert first_call_buf[1] == ord("c") # command byte |
| 345 |
assert first_call_buf[7] == 2 # ledn |
| 346 |
# fade_time = 200 // 10 = 20 = 0x0014 |
| 347 |
assert first_call_buf[5] == 0x00 |
| 348 |
assert first_call_buf[6] == 0x14 |
| 349 |
|
| 350 |
|
| 351 |
def test_plugin_blink1_off_sent_after_duration(): |
| 352 |
"""NotifyBlink1() sends an off command after sleeping for fade+duration.""" |
| 353 |
|
| 354 |
pytest.importorskip("hid") |
| 355 |
|
| 356 |
with ( |
| 357 |
mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid, |
| 358 |
mock.patch("apprise.plugins.blink1.time") as mock_time, |
| 359 |
): |
| 360 |
mock_dev = _mk_device() |
| 361 |
mock_hid.device.return_value = mock_dev |
| 362 |
mock_time.sleep.return_value = None |
| 363 |
|
| 364 |
obj = Apprise.instantiate("blink1://?fade=500&duration=2000") |
| 365 |
assert obj is not None |
| 366 |
assert bool(obj.notify(body="test")) is True |
| 367 |
|
| 368 |
mock_time.sleep.assert_called_once_with(2.5) |
| 369 |
|
| 370 |
# Second report should be the "off" command (all zeros for RGB) |
| 371 |
off_buf = mock_dev.send_feature_report.call_args_list[1][0][0] |
| 372 |
assert off_buf[2] == 0 # red |
| 373 |
assert off_buf[3] == 0 # green |
| 374 |
assert off_buf[4] == 0 # blue |
| 375 |
|
| 376 |
|
| 377 |
def test_plugin_blink1_off_command_failure_returns_false(): |
| 378 |
"""NotifyBlink1() returns False when the off HID write fails.""" |
| 379 |
|
| 380 |
pytest.importorskip("hid") |
| 381 |
|
| 382 |
with ( |
| 383 |
mock.patch("apprise.plugins.blink1.blink1_hid") as mock_hid, |
| 384 |
mock.patch("apprise.plugins.blink1.time") as mock_time, |
| 385 |
): |
| 386 |
mock_dev = mock.Mock() |
| 387 |
# First call (color fade) succeeds; second call (off) fails. |
| 388 |
mock_dev.send_feature_report.side_effect = [ |
| 389 |
BLINK1_REPORT_SIZE, |
| 390 |
BLINK1_REPORT_SIZE - 1, |
| 391 |
] |
| 392 |
mock_hid.device.return_value = mock_dev |
| 393 |
mock_time.sleep.return_value = None |
| 394 |
|
| 395 |
obj = Apprise.instantiate("blink1://") |
| 396 |
assert obj is not None |
| 397 |
assert bool(obj.notify(body="test")) is False |
| 398 |
|
| 399 |
# close() must still be called |
| 400 |
assert mock_dev.close.called |
| 401 |
|
| 402 |
|
| 403 |
def test_plugin_blink1_url_round_trip(): |
| 404 |
"""NotifyBlink1() URL reconstructs an equivalent object.""" |
| 405 |
|
| 406 |
obj = NotifyBlink1(serial="ABCD1234", duration=3000, fade=500, ledn=1) |
| 407 |
rebuilt = NotifyBlink1(**NotifyBlink1.parse_url(obj.url())) |
| 408 |
|
| 409 |
assert rebuilt.serial == "ABCD1234" |
| 410 |
assert rebuilt.duration == 3000 |
| 411 |
assert rebuilt.fade == 500 |
| 412 |
assert rebuilt.ledn == 1 |
| 413 |
|
| 414 |
|
| 415 |
def test_plugin_blink1_url_round_trip_no_serial(): |
| 416 |
"""NotifyBlink1() URL round-trips correctly without a serial.""" |
| 417 |
|
| 418 |
obj = NotifyBlink1() |
| 419 |
rebuilt = NotifyBlink1(**NotifyBlink1.parse_url(obj.url())) |
| 420 |
|
| 421 |
assert rebuilt.serial is None |
| 422 |
assert rebuilt.duration == BLINK1_DEFAULT_DURATION_MS |
| 423 |
assert rebuilt.fade == BLINK1_DEFAULT_FADE_MS |
| 424 |
assert rebuilt.ledn == Blink1LED.ALL |
| 425 |
|
| 426 |
|
| 427 |
def test_plugin_blink1_url_identifier(): |
| 428 |
"""NotifyBlink1() url_identifier distinguishes different connections.""" |
| 429 |
|
| 430 |
obj_a = NotifyBlink1() |
| 431 |
obj_b = NotifyBlink1(serial="ABCD1234") |
| 432 |
obj_c = NotifyBlink1(ledn=1) |
| 433 |
|
| 434 |
assert obj_a.url_identifier != obj_b.url_identifier |
| 435 |
assert obj_a.url_identifier != obj_c.url_identifier |
| 436 |
assert obj_b.url_identifier != obj_c.url_identifier |
| 437 |
|
| 438 |
|
| 439 |
def test_plugin_blink1_invalid_ledn_defaults(): |
| 440 |
"""NotifyBlink1() silently falls back to ALL for an unrecognised ledn.""" |
| 441 |
|
| 442 |
obj = NotifyBlink1(ledn="99") |
| 443 |
assert obj.ledn == Blink1LED.ALL |
| 444 |
|
| 445 |
|
| 446 |
def test_plugin_blink1_runtime_deps(): |
| 447 |
"""NotifyBlink1.runtime_deps() returns the importable hid module name.""" |
| 448 |
|
| 449 |
assert NotifyBlink1.runtime_deps() == ("hid",) |
| 450 |
|
| 451 |
|
| 452 |
def test_plugin_blink1_parse_url_underscore(): |
| 453 |
"""NotifyBlink1() treats underscore host as first-device placeholder.""" |
| 454 |
|
| 455 |
result = NotifyBlink1.parse_url("blink1://_/") |
| 456 |
assert result is not None |
| 457 |
assert result.get("serial") is None |
| 458 |
|
| 459 |
|
| 460 |
def test_plugin_blink1_parse_url_serial(): |
| 461 |
"""NotifyBlink1() extracts serial number from URL host field.""" |
| 462 |
|
| 463 |
result = NotifyBlink1.parse_url("blink1://DEADBEEF/") |
| 464 |
assert result is not None |
| 465 |
assert result["serial"] == "DEADBEEF" |