From 7e2c8a27374dbc6feebc286e0f9dd0fbe6da2622 Mon Sep 17 00:00:00 2001 From: Philip Allgaier Date: Fri, 9 Apr 2021 19:36:13 +0200 Subject: [PATCH] Fix "notify.events" trim() issue + add initial tests (#48928) Co-authored-by: Paulus Schoutsen --- .../components/notify_events/notify.py | 5 +-- requirements_test_all.txt | 3 ++ tests/components/notify_events/__init__.py | 1 + tests/components/notify_events/test_init.py | 12 ++++++ tests/components/notify_events/test_notify.py | 38 +++++++++++++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 tests/components/notify_events/__init__.py create mode 100644 tests/components/notify_events/test_init.py create mode 100644 tests/components/notify_events/test_notify.py diff --git a/homeassistant/components/notify_events/notify.py b/homeassistant/components/notify_events/notify.py index ce7c353badb..51705453edf 100644 --- a/homeassistant/components/notify_events/notify.py +++ b/homeassistant/components/notify_events/notify.py @@ -116,12 +116,9 @@ class NotifyEventsNotificationService(BaseNotificationService): def send_message(self, message, **kwargs): """Send a message.""" - token = self.token data = kwargs.get(ATTR_DATA) or {} + token = data.get(ATTR_TOKEN, self.token) msg = self.prepare_message(message, data) - if data.get(ATTR_TOKEN, "").trim(): - token = data[ATTR_TOKEN] - msg.send(token) diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 2961a4e344a..4223b1ce19c 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -532,6 +532,9 @@ netdisco==2.8.2 # homeassistant.components.nexia nexia==0.9.5 +# homeassistant.components.notify_events +notify-events==1.0.4 + # homeassistant.components.nsw_fuel_station nsw-fuel-api-client==1.0.10 diff --git a/tests/components/notify_events/__init__.py b/tests/components/notify_events/__init__.py new file mode 100644 index 00000000000..5e2f9c2eaf1 --- /dev/null +++ b/tests/components/notify_events/__init__.py @@ -0,0 +1 @@ +"""Tests for the notify_events integration.""" diff --git a/tests/components/notify_events/test_init.py b/tests/components/notify_events/test_init.py new file mode 100644 index 00000000000..861be83a9cc --- /dev/null +++ b/tests/components/notify_events/test_init.py @@ -0,0 +1,12 @@ +"""The tests for notify_events.""" +from homeassistant.components.notify_events.const import DOMAIN +from homeassistant.setup import async_setup_component + + +async def test_setup(hass): + """Test setup of the integration.""" + config = {"notify_events": {"token": "ABC"}} + assert await async_setup_component(hass, DOMAIN, config) + await hass.async_block_till_done() + + assert DOMAIN in hass.data diff --git a/tests/components/notify_events/test_notify.py b/tests/components/notify_events/test_notify.py new file mode 100644 index 00000000000..55cf6275044 --- /dev/null +++ b/tests/components/notify_events/test_notify.py @@ -0,0 +1,38 @@ +"""The tests for notify_events.""" +from homeassistant.components.notify import ATTR_DATA, ATTR_MESSAGE, DOMAIN +from homeassistant.components.notify_events.notify import ( + ATTR_LEVEL, + ATTR_PRIORITY, + ATTR_TOKEN, +) + +from tests.common import async_mock_service + + +async def test_send_msg(hass): + """Test notify.events service.""" + notify_calls = async_mock_service(hass, DOMAIN, "events") + + await hass.services.async_call( + DOMAIN, + "events", + { + ATTR_MESSAGE: "message content", + ATTR_DATA: { + ATTR_TOKEN: "XYZ", + ATTR_LEVEL: "warning", + ATTR_PRIORITY: "high", + }, + }, + blocking=True, + ) + + assert len(notify_calls) == 1 + call = notify_calls[-1] + + assert call.domain == DOMAIN + assert call.service == "events" + assert call.data.get(ATTR_MESSAGE) == "message content" + assert call.data.get(ATTR_DATA).get(ATTR_TOKEN) == "XYZ" + assert call.data.get(ATTR_DATA).get(ATTR_LEVEL) == "warning" + assert call.data.get(ATTR_DATA).get(ATTR_PRIORITY) == "high"