Add Matrix.io HTML message format support (#69951)

This commit is contained in:
Erik J. Olson 2022-06-30 14:00:29 -05:00 committed by GitHub
parent 1bfd8b1a76
commit 0caeeb56c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 6 deletions

View File

@ -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)

View File

@ -2,3 +2,6 @@
DOMAIN = "matrix"
SERVICE_SEND_MESSAGE = "send_message"
FORMAT_HTML = "html"
FORMAT_TEXT = "text"

View File

@ -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: