Fix slack message icon override (#41212)
* Fix slack message icon override Allows overriding the icon for individual slack messages using either an emoji or a URL. * Run python3 -m script.gen_requirements_all * Add period to first line * Add support for python 3.7 testing AsyncMock is only available from python 3.8+. Prior to this, CoroutineMock is used which doesn't mock the method so it needs to be done manually. * Fix tests for python3.7 compatibility The Python3.7 mock call object doesn't have the kwargs helper property. * Update default emoji test docstring
This commit is contained in:
parent
e64b8774ec
commit
a1f70e11ae
4 changed files with 73 additions and 2 deletions
|
@ -209,8 +209,9 @@ class SlackNotificationService(BaseNotificationService):
|
||||||
"username": username,
|
"username": username,
|
||||||
}
|
}
|
||||||
|
|
||||||
if self._icon:
|
icon = icon or self._icon
|
||||||
if self._icon.lower().startswith(("http://", "https://")):
|
if icon:
|
||||||
|
if icon.lower().startswith(("http://", "https://")):
|
||||||
icon_type = "url"
|
icon_type = "url"
|
||||||
else:
|
else:
|
||||||
icon_type = "emoji"
|
icon_type = "emoji"
|
||||||
|
|
|
@ -938,6 +938,9 @@ simplehound==0.3
|
||||||
# homeassistant.components.simplisafe
|
# homeassistant.components.simplisafe
|
||||||
simplisafe-python==9.4.1
|
simplisafe-python==9.4.1
|
||||||
|
|
||||||
|
# homeassistant.components.slack
|
||||||
|
slackclient==2.5.0
|
||||||
|
|
||||||
# homeassistant.components.sleepiq
|
# homeassistant.components.sleepiq
|
||||||
sleepyq==0.7
|
sleepyq==0.7
|
||||||
|
|
||||||
|
|
1
tests/components/slack/__init__.py
Normal file
1
tests/components/slack/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
"""Slack notification tests."""
|
66
tests/components/slack/test_notify.py
Normal file
66
tests/components/slack/test_notify.py
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
"""Test slack notifications."""
|
||||||
|
from unittest.mock import Mock
|
||||||
|
|
||||||
|
from homeassistant.components.slack.notify import SlackNotificationService
|
||||||
|
|
||||||
|
from tests.async_mock import AsyncMock
|
||||||
|
|
||||||
|
|
||||||
|
async def test_message_includes_default_emoji():
|
||||||
|
"""Tests that default icon is used when no message icon is given."""
|
||||||
|
mock_client = Mock()
|
||||||
|
mock_client.chat_postMessage = AsyncMock()
|
||||||
|
expected_icon = ":robot_face:"
|
||||||
|
service = SlackNotificationService(None, mock_client, "_", "_", expected_icon)
|
||||||
|
|
||||||
|
await service.async_send_message("test")
|
||||||
|
|
||||||
|
mock_fn = mock_client.chat_postMessage
|
||||||
|
mock_fn.assert_called_once()
|
||||||
|
_, kwargs = mock_fn.call_args
|
||||||
|
assert kwargs["icon_emoji"] == expected_icon
|
||||||
|
|
||||||
|
|
||||||
|
async def test_message_emoji_overrides_default():
|
||||||
|
"""Tests that overriding the default icon emoji when sending a message works."""
|
||||||
|
mock_client = Mock()
|
||||||
|
mock_client.chat_postMessage = AsyncMock()
|
||||||
|
service = SlackNotificationService(None, mock_client, "_", "_", "default_icon")
|
||||||
|
|
||||||
|
expected_icon = ":new:"
|
||||||
|
await service.async_send_message("test", data={"icon": expected_icon})
|
||||||
|
|
||||||
|
mock_fn = mock_client.chat_postMessage
|
||||||
|
mock_fn.assert_called_once()
|
||||||
|
_, kwargs = mock_fn.call_args
|
||||||
|
assert kwargs["icon_emoji"] == expected_icon
|
||||||
|
|
||||||
|
|
||||||
|
async def test_message_includes_default_icon_url():
|
||||||
|
"""Tests that overriding the default icon url when sending a message works."""
|
||||||
|
mock_client = Mock()
|
||||||
|
mock_client.chat_postMessage = AsyncMock()
|
||||||
|
expected_icon = "https://example.com/hass.png"
|
||||||
|
service = SlackNotificationService(None, mock_client, "_", "_", expected_icon)
|
||||||
|
|
||||||
|
await service.async_send_message("test")
|
||||||
|
|
||||||
|
mock_fn = mock_client.chat_postMessage
|
||||||
|
mock_fn.assert_called_once()
|
||||||
|
_, kwargs = mock_fn.call_args
|
||||||
|
assert kwargs["icon_url"] == expected_icon
|
||||||
|
|
||||||
|
|
||||||
|
async def test_message_icon_url_overrides_default():
|
||||||
|
"""Tests that overriding the default icon url when sending a message works."""
|
||||||
|
mock_client = Mock()
|
||||||
|
mock_client.chat_postMessage = AsyncMock()
|
||||||
|
service = SlackNotificationService(None, mock_client, "_", "_", "default_icon")
|
||||||
|
|
||||||
|
expected_icon = "https://example.com/hass.png"
|
||||||
|
await service.async_send_message("test", data={"icon": expected_icon})
|
||||||
|
|
||||||
|
mock_fn = mock_client.chat_postMessage
|
||||||
|
mock_fn.assert_called_once()
|
||||||
|
_, kwargs = mock_fn.call_args
|
||||||
|
assert kwargs["icon_url"] == expected_icon
|
Loading…
Add table
Add a link
Reference in a new issue