From 98d7e6b898fdafc1cb535f6dad14de617bea4679 Mon Sep 17 00:00:00 2001 From: bestlibre Date: Thu, 18 Mar 2021 19:30:38 +0100 Subject: [PATCH] Add images support to matrix notify (#37625) Co-authored-by: Paulus Schoutsen --- homeassistant/components/matrix/__init__.py | 47 +++++++++++++++++-- homeassistant/components/matrix/notify.py | 6 ++- homeassistant/components/matrix/services.yaml | 3 ++ 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/matrix/__init__.py b/homeassistant/components/matrix/__init__.py index c89de5552d5..62af53079e8 100644 --- a/homeassistant/components/matrix/__init__.py +++ b/homeassistant/components/matrix/__init__.py @@ -1,12 +1,13 @@ """The Matrix bot component.""" from functools import partial import logging +import mimetypes import os from matrix_client.client import MatrixClient, MatrixRequestError import voluptuous as vol -from homeassistant.components.notify import ATTR_MESSAGE, ATTR_TARGET +from homeassistant.components.notify import ATTR_DATA, ATTR_MESSAGE, ATTR_TARGET from homeassistant.const import ( CONF_NAME, CONF_PASSWORD, @@ -31,8 +32,12 @@ CONF_COMMANDS = "commands" CONF_WORD = "word" CONF_EXPRESSION = "expression" +DEFAULT_CONTENT_TYPE = "application/octet-stream" + EVENT_MATRIX_COMMAND = "matrix_command" +ATTR_IMAGES = "images" # optional images + COMMAND_SCHEMA = vol.All( vol.Schema( { @@ -67,6 +72,9 @@ CONFIG_SCHEMA = vol.Schema( SERVICE_SCHEMA_SEND_MESSAGE = vol.Schema( { vol.Required(ATTR_MESSAGE): cv.string, + vol.Optional(ATTR_DATA): { + vol.Optional(ATTR_IMAGES): vol.All(cv.ensure_list, [cv.string]), + }, vol.Required(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]), } ) @@ -336,13 +344,20 @@ class MatrixBot: return _client - def _send_message(self, message, target_rooms): - """Send the message to the Matrix server.""" + def _send_image(self, img, target_rooms): + _LOGGER.debug("Uploading file from path, %s", img) + if not self.hass.config.is_allowed_path(img): + _LOGGER.error("Path not allowed: %s", img) + return + with open(img, "rb") as upfile: + imgfile = upfile.read() + content_type = mimetypes.guess_type(img)[0] + mxc = self._client.upload(imgfile, content_type) for target_room in target_rooms: try: room = self._join_or_get_room(target_room) - _LOGGER.debug(room.send_text(message)) + room.send_image(mxc, img) except MatrixRequestError as ex: _LOGGER.error( "Unable to deliver message to room '%s': %d, %s", @@ -351,6 +366,28 @@ class MatrixBot: ex.content, ) + def _send_message(self, message, data, target_rooms): + """Send the message to the Matrix server.""" + for target_room in target_rooms: + try: + room = self._join_or_get_room(target_room) + if message is not None: + _LOGGER.debug(room.send_text(message)) + except MatrixRequestError as ex: + _LOGGER.error( + "Unable to deliver message to room '%s': %d, %s", + target_room, + ex.code, + ex.content, + ) + if data is not None: + for img in data.get(ATTR_IMAGES, []): + self._send_image(img, target_rooms) + def handle_send_message(self, service): """Handle the send_message service.""" - self._send_message(service.data[ATTR_MESSAGE], service.data[ATTR_TARGET]) + self._send_message( + service.data.get(ATTR_MESSAGE), + service.data.get(ATTR_DATA), + service.data[ATTR_TARGET], + ) diff --git a/homeassistant/components/matrix/notify.py b/homeassistant/components/matrix/notify.py index 0965783bf4d..8643d7511bc 100644 --- a/homeassistant/components/matrix/notify.py +++ b/homeassistant/components/matrix/notify.py @@ -2,6 +2,7 @@ import voluptuous as vol from homeassistant.components.notify import ( + ATTR_DATA, ATTR_MESSAGE, ATTR_TARGET, PLATFORM_SCHEMA, @@ -31,9 +32,10 @@ class MatrixNotificationService(BaseNotificationService): def send_message(self, message="", **kwargs): """Send the message to the Matrix server.""" target_rooms = kwargs.get(ATTR_TARGET) or [self._default_room] - service_data = {ATTR_TARGET: target_rooms, ATTR_MESSAGE: message} - + data = kwargs.get(ATTR_DATA) + if data is not None: + service_data[ATTR_DATA] = data return self.hass.services.call( DOMAIN, SERVICE_SEND_MESSAGE, service_data=service_data ) diff --git a/homeassistant/components/matrix/services.yaml b/homeassistant/components/matrix/services.yaml index f8b0c53bda6..fe99bf6365a 100644 --- a/homeassistant/components/matrix/services.yaml +++ b/homeassistant/components/matrix/services.yaml @@ -7,3 +7,6 @@ send_message: target: description: A list of room(s) to send the message to. example: "#hasstest:matrix.org" + data: + description: Extended information of notification. Supports list of images. Optional. + example: "{'images': ['/tmp/test.jpg']}"