Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/use_notify/channels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@

# 兼容wecom
WeCom = WeChat

CHANNEL_REGISTRY = {
"bark": Bark,
"chanify": Chanify,
"console": Console,
"ding": Ding,
"email": Email,
"feishu": Feishu,
"ntfy": Ntfy,
"pushdeer": PushDeer,
"pushover": PushOver,
"wechat": WeChat,
"wecom": WeChat,
}


def get_channel_class(name):
return CHANNEL_REGISTRY.get(name.lower())
7 changes: 1 addition & 6 deletions src/use_notify/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,7 @@ def from_settings(cls, settings: dict):
"""
channels = []
for channel, cfg in settings.items():
# Try to get class by case-insensitive match
channel_cls = None
for cls_name in dir(channels_models):
if cls_name.lower() == channel.lower():
channel_cls = getattr(channels_models, cls_name)
break
channel_cls = channels_models.get_channel_class(channel)
if not channel_cls:
raise ValueError(f"Unknown channel {channel}")
channel = channel_cls(cfg)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,15 @@ def test_notify_from_settings_builds_case_insensitive_channels():
assert isinstance(notify_instance.channels[1], useNotifyChannel.WeCom)


def test_notify_from_settings_ignores_unregistered_module_attributes(monkeypatch):
monkeypatch.setattr(
notification_module.channels_models, "Ghost", useNotifyChannel.Bark, raising=False
)

with pytest.raises(ValueError, match="Unknown channel ghost"):
useNotify.from_settings({"ghost": {"token": "x"}})


def test_notify_from_settings_rejects_unknown_channel():
with pytest.raises(ValueError, match="Unknown channel"):
useNotify.from_settings({"unknown": {"token": "x"}})
Expand Down