Add Matrix.io HTML message format support (#69951)
parent
1bfd8b1a76
commit
0caeeb56c5
|
@ -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)
|
||||
|
||||
|
|
|
@ -2,3 +2,6 @@
|
|||
DOMAIN = "matrix"
|
||||
|
||||
SERVICE_SEND_MESSAGE = "send_message"
|
||||
|
||||
FORMAT_HTML = "html"
|
||||
FORMAT_TEXT = "text"
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue