mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
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:
parent
29aa1463ef
commit
294c6a713f
@ -621,8 +621,6 @@ omit =
|
|||||||
homeassistant/components/shodan/sensor.py
|
homeassistant/components/shodan/sensor.py
|
||||||
homeassistant/components/sht31/sensor.py
|
homeassistant/components/sht31/sensor.py
|
||||||
homeassistant/components/sigfox/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/simplepush/notify.py
|
||||||
homeassistant/components/simplisafe/__init__.py
|
homeassistant/components/simplisafe/__init__.py
|
||||||
homeassistant/components/simplisafe/alarm_control_panel.py
|
homeassistant/components/simplisafe/alarm_control_panel.py
|
||||||
|
@ -4,5 +4,5 @@
|
|||||||
"documentation": "https://www.home-assistant.io/integrations/signal_messenger",
|
"documentation": "https://www.home-assistant.io/integrations/signal_messenger",
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": ["@bbernhard"],
|
"codeowners": ["@bbernhard"],
|
||||||
"requirements": ["pysignalclirestapi==0.1.4"]
|
"requirements": ["pysignalclirestapi==0.2.4"]
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ CONF_SENDER_NR = "number"
|
|||||||
CONF_RECP_NR = "recipients"
|
CONF_RECP_NR = "recipients"
|
||||||
CONF_SIGNAL_CLI_REST_API = "url"
|
CONF_SIGNAL_CLI_REST_API = "url"
|
||||||
ATTR_FILENAME = "attachment"
|
ATTR_FILENAME = "attachment"
|
||||||
|
ATTR_FILENAMES = "attachments"
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
@ -34,9 +35,7 @@ def get_service(hass, config, discovery_info=None):
|
|||||||
recp_nrs = config[CONF_RECP_NR]
|
recp_nrs = config[CONF_RECP_NR]
|
||||||
signal_cli_rest_api_url = config[CONF_SIGNAL_CLI_REST_API]
|
signal_cli_rest_api_url = config[CONF_SIGNAL_CLI_REST_API]
|
||||||
|
|
||||||
signal_cli_rest_api = SignalCliRestApi(
|
signal_cli_rest_api = SignalCliRestApi(signal_cli_rest_api_url, sender_nr)
|
||||||
signal_cli_rest_api_url, sender_nr, api_version=1
|
|
||||||
)
|
|
||||||
|
|
||||||
return SignalNotificationService(recp_nrs, signal_cli_rest_api)
|
return SignalNotificationService(recp_nrs, signal_cli_rest_api)
|
||||||
|
|
||||||
@ -60,12 +59,21 @@ class SignalNotificationService(BaseNotificationService):
|
|||||||
|
|
||||||
data = kwargs.get(ATTR_DATA)
|
data = kwargs.get(ATTR_DATA)
|
||||||
|
|
||||||
filename = None
|
filenames = None
|
||||||
if data is not None and ATTR_FILENAME in data:
|
if data is not None:
|
||||||
filename = data[ATTR_FILENAME]
|
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:
|
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:
|
except SignalCliRestApiError as ex:
|
||||||
_LOGGER.error("%s", ex)
|
_LOGGER.error("%s", ex)
|
||||||
raise ex
|
raise ex
|
||||||
|
@ -1491,7 +1491,7 @@ pysesame2==1.0.1
|
|||||||
pysher==1.0.1
|
pysher==1.0.1
|
||||||
|
|
||||||
# homeassistant.components.signal_messenger
|
# homeassistant.components.signal_messenger
|
||||||
pysignalclirestapi==0.1.4
|
pysignalclirestapi==0.2.4
|
||||||
|
|
||||||
# homeassistant.components.sma
|
# homeassistant.components.sma
|
||||||
pysma==0.3.5
|
pysma==0.3.5
|
||||||
|
@ -515,6 +515,9 @@ pyps4-2ndscreen==1.0.6
|
|||||||
# homeassistant.components.qwikswitch
|
# homeassistant.components.qwikswitch
|
||||||
pyqwikswitch==0.93
|
pyqwikswitch==0.93
|
||||||
|
|
||||||
|
# homeassistant.components.signal_messenger
|
||||||
|
pysignalclirestapi==0.2.4
|
||||||
|
|
||||||
# homeassistant.components.sma
|
# homeassistant.components.sma
|
||||||
pysma==0.3.5
|
pysma==0.3.5
|
||||||
|
|
||||||
|
1
tests/components/signal_messenger/__init__.py
Normal file
1
tests/components/signal_messenger/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
"""Tests for the signal_messenger component."""
|
122
tests/components/signal_messenger/test_notify.py
Normal file
122
tests/components/signal_messenger/test_notify.py
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user