Support multiple attachments in signal messenger integration (#31141)

* added support for multiple attachments to signal_messenger integration

* updated pysignalclirestapi version in requirements_all.txt

* reworked multiple attachments feature in signal_messenger integration

* stay backwards compatible by both allowing the "attachment" and
  the "attachments" attribute.

* reworked multiple attachments feature in signal_messenger integration

* stay backwards compatible by both allowing the "attachment" and
  the "attachments" attribute.

* small change in signal_messenger integration

* added deprecation warning for 'attachment' attribute

* small changes in signal_messenger integration

* use 'warning' instead of 'warn' when logging the warning message

* re-generated requirements_test_pre_commit.txt

* added tests for signal_messenger integration

* regenerated requirements_test_all.txt for signal_messenger integration

* added more signal_messenger tests

* remove signal_messenger integration files from .coveragerc
This commit is contained in:
Bernhard B 2020-02-01 23:21:16 +01:00 committed by GitHub
parent 29aa1463ef
commit 294c6a713f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 143 additions and 11 deletions

View File

@ -621,8 +621,6 @@ omit =
homeassistant/components/shodan/sensor.py
homeassistant/components/sht31/sensor.py
homeassistant/components/sigfox/sensor.py
homeassistant/components/signal_messenger/__init__.py
homeassistant/components/signal_messenger/notify.py
homeassistant/components/simplepush/notify.py
homeassistant/components/simplisafe/__init__.py
homeassistant/components/simplisafe/alarm_control_panel.py

View File

@ -4,5 +4,5 @@
"documentation": "https://www.home-assistant.io/integrations/signal_messenger",
"dependencies": [],
"codeowners": ["@bbernhard"],
"requirements": ["pysignalclirestapi==0.1.4"]
"requirements": ["pysignalclirestapi==0.2.4"]
}

View File

@ -17,6 +17,7 @@ CONF_SENDER_NR = "number"
CONF_RECP_NR = "recipients"
CONF_SIGNAL_CLI_REST_API = "url"
ATTR_FILENAME = "attachment"
ATTR_FILENAMES = "attachments"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
@ -34,9 +35,7 @@ def get_service(hass, config, discovery_info=None):
recp_nrs = config[CONF_RECP_NR]
signal_cli_rest_api_url = config[CONF_SIGNAL_CLI_REST_API]
signal_cli_rest_api = SignalCliRestApi(
signal_cli_rest_api_url, sender_nr, api_version=1
)
signal_cli_rest_api = SignalCliRestApi(signal_cli_rest_api_url, sender_nr)
return SignalNotificationService(recp_nrs, signal_cli_rest_api)
@ -60,12 +59,21 @@ class SignalNotificationService(BaseNotificationService):
data = kwargs.get(ATTR_DATA)
filename = None
if data is not None and ATTR_FILENAME in data:
filename = data[ATTR_FILENAME]
filenames = None
if data is not None:
if ATTR_FILENAMES in data:
filenames = data[ATTR_FILENAMES]
if ATTR_FILENAME in data:
_LOGGER.warning(
"The 'attachment' option is deprecated, please replace it with 'attachments'. This option will become invalid in version 0.108."
)
if filenames is None:
filenames = [data[ATTR_FILENAME]]
else:
filenames.append(data[ATTR_FILENAME])
try:
self._signal_cli_rest_api.send_message(message, self._recp_nrs, filename)
self._signal_cli_rest_api.send_message(message, self._recp_nrs, filenames)
except SignalCliRestApiError as ex:
_LOGGER.error("%s", ex)
raise ex

View File

@ -1491,7 +1491,7 @@ pysesame2==1.0.1
pysher==1.0.1
# homeassistant.components.signal_messenger
pysignalclirestapi==0.1.4
pysignalclirestapi==0.2.4
# homeassistant.components.sma
pysma==0.3.5

View File

@ -515,6 +515,9 @@ pyps4-2ndscreen==1.0.6
# homeassistant.components.qwikswitch
pyqwikswitch==0.93
# homeassistant.components.signal_messenger
pysignalclirestapi==0.2.4
# homeassistant.components.sma
pysma==0.3.5

View File

@ -0,0 +1 @@
"""Tests for the signal_messenger component."""

View File

@ -0,0 +1,122 @@
"""The tests for the signal_messenger platform."""
import os
import tempfile
import unittest
from unittest.mock import patch
from pysignalclirestapi import SignalCliRestApi
import requests_mock
import homeassistant.components.signal_messenger.notify as signalmessenger
from homeassistant.setup import async_setup_component
BASE_COMPONENT = "notify"
async def test_signal_messenger_init(hass):
"""Test that service loads successfully."""
config = {
BASE_COMPONENT: {
"name": "test",
"platform": "signal_messenger",
"url": "http://127.0.0.1:8080",
"number": "+43443434343",
"recipients": ["+435565656565"],
}
}
with patch("pysignalclirestapi.SignalCliRestApi.send_message", return_value=None):
assert await async_setup_component(hass, BASE_COMPONENT, config)
await hass.async_block_till_done()
# Test that service loads successfully
assert hass.services.has_service(BASE_COMPONENT, "test")
class TestSignalMesssenger(unittest.TestCase):
"""Test the signal_messenger notify."""
def setUp(self):
"""Set up things to be run when tests are started."""
recipients = ["+435565656565"]
number = "+43443434343"
client = SignalCliRestApi("http://127.0.0.1:8080", number)
self._signalmessenger = signalmessenger.SignalNotificationService(
recipients, client
)
@requests_mock.Mocker()
def test_send_message(self, mock):
"""Test send message."""
message = "Testing Signal Messenger platform :)"
mock.register_uri(
"POST", "http://127.0.0.1:8080/v2/send", status_code=201,
)
mock.register_uri(
"GET",
"http://127.0.0.1:8080/v1/about",
status_code=200,
json={"versions": ["v1", "v2"]},
)
with self.assertLogs(
"homeassistant.components.signal_messenger.notify", level="DEBUG"
) as context:
self._signalmessenger.send_message(message)
self.assertIn("Sending signal message", context.output[0])
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 2)
@requests_mock.Mocker()
def test_send_message_should_show_deprecation_warning(self, mock):
"""Test send message."""
message = "Testing Signal Messenger platform with attachment :)"
mock.register_uri(
"POST", "http://127.0.0.1:8080/v2/send", status_code=201,
)
mock.register_uri(
"GET",
"http://127.0.0.1:8080/v1/about",
status_code=200,
json={"versions": ["v1", "v2"]},
)
with self.assertLogs(
"homeassistant.components.signal_messenger.notify", level="WARNING"
) as context:
with tempfile.NamedTemporaryFile(
suffix=".png", prefix=os.path.basename(__file__)
) as tf:
data = {"data": {"attachment": tf.name}}
self._signalmessenger.send_message(message, **data)
self.assertIn(
"The 'attachment' option is deprecated, please replace it with 'attachments'. This option will become invalid in version 0.108.",
context.output[0],
)
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 2)
@requests_mock.Mocker()
def test_send_message_with_attachment(self, mock):
"""Test send message."""
message = "Testing Signal Messenger platform :)"
mock.register_uri(
"POST", "http://127.0.0.1:8080/v2/send", status_code=201,
)
mock.register_uri(
"GET",
"http://127.0.0.1:8080/v1/about",
status_code=200,
json={"versions": ["v1", "v2"]},
)
with self.assertLogs(
"homeassistant.components.signal_messenger.notify", level="DEBUG"
) as context:
with tempfile.NamedTemporaryFile(
suffix=".png", prefix=os.path.basename(__file__)
) as tf:
data = {"data": {"attachments": [tf.name]}}
self._signalmessenger.send_message(message, **data)
self.assertIn("Sending signal message", context.output[0])
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 2)