From 0caeeb56c5efd5b07d110a44c297cbda85a43c60 Mon Sep 17 00:00:00 2001 From: "Erik J. Olson" Date: Thu, 30 Jun 2022 14:00:29 -0500 Subject: [PATCH] Add Matrix.io HTML message format support (#69951) --- homeassistant/components/matrix/__init__.py | 18 ++++++++++++++---- homeassistant/components/matrix/const.py | 3 +++ homeassistant/components/matrix/services.yaml | 4 ++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/matrix/__init__.py b/homeassistant/components/matrix/__init__.py index 47646a586ca..17df46a0849 100644 --- a/homeassistant/components/matrix/__init__.py +++ b/homeassistant/components/matrix/__init__.py @@ -22,7 +22,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.typing import ConfigType from homeassistant.util.json import load_json, save_json -from .const import DOMAIN, SERVICE_SEND_MESSAGE +from .const import DOMAIN, FORMAT_HTML, FORMAT_TEXT, SERVICE_SEND_MESSAGE _LOGGER = logging.getLogger(__name__) @@ -36,8 +36,12 @@ CONF_EXPRESSION = "expression" DEFAULT_CONTENT_TYPE = "application/octet-stream" +MESSAGE_FORMATS = [FORMAT_HTML, FORMAT_TEXT] +DEFAULT_MESSAGE_FORMAT = FORMAT_TEXT + EVENT_MATRIX_COMMAND = "matrix_command" +ATTR_FORMAT = "format" # optional message format ATTR_IMAGES = "images" # optional images COMMAND_SCHEMA = vol.All( @@ -74,7 +78,10 @@ CONFIG_SCHEMA = vol.Schema( SERVICE_SCHEMA_SEND_MESSAGE = vol.Schema( { vol.Required(ATTR_MESSAGE): cv.string, - vol.Optional(ATTR_DATA): { + vol.Optional(ATTR_DATA, default={}): { + vol.Optional(ATTR_FORMAT, default=DEFAULT_MESSAGE_FORMAT): vol.In( + MESSAGE_FORMATS + ), vol.Optional(ATTR_IMAGES): vol.All(cv.ensure_list, [cv.string]), }, vol.Required(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]), @@ -377,7 +384,10 @@ class MatrixBot: try: room = self._join_or_get_room(target_room) if message is not None: - _LOGGER.debug(room.send_text(message)) + if data.get(ATTR_FORMAT) == FORMAT_HTML: + _LOGGER.debug(room.send_html(message)) + else: + _LOGGER.debug(room.send_text(message)) except MatrixRequestError as ex: _LOGGER.error( "Unable to deliver message to room '%s': %d, %s", @@ -385,7 +395,7 @@ class MatrixBot: ex.code, ex.content, ) - if data is not None: + if ATTR_IMAGES in data: for img in data.get(ATTR_IMAGES, []): self._send_image(img, target_rooms) diff --git a/homeassistant/components/matrix/const.py b/homeassistant/components/matrix/const.py index 6b082bde121..b7e0c22e2ac 100644 --- a/homeassistant/components/matrix/const.py +++ b/homeassistant/components/matrix/const.py @@ -2,3 +2,6 @@ DOMAIN = "matrix" SERVICE_SEND_MESSAGE = "send_message" + +FORMAT_HTML = "html" +FORMAT_TEXT = "text" diff --git a/homeassistant/components/matrix/services.yaml b/homeassistant/components/matrix/services.yaml index c58a27c3370..9b5171d1483 100644 --- a/homeassistant/components/matrix/services.yaml +++ b/homeassistant/components/matrix/services.yaml @@ -18,7 +18,7 @@ send_message: text: data: name: Data - description: Extended information of notification. Supports list of images. Optional. - example: "{'images': ['/tmp/test.jpg']}" + description: Extended information of notification. Supports list of images. Supports message format. Optional. + example: "{'images': ['/tmp/test.jpg'], 'format': 'text'}" selector: object: