Add Matrix.io HTML message format support (#69951)
This commit is contained in:
parent
1bfd8b1a76
commit
0caeeb56c5
3 changed files with 19 additions and 6 deletions
|
@ -22,7 +22,7 @@ import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.util.json import load_json, save_json
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -36,8 +36,12 @@ CONF_EXPRESSION = "expression"
|
||||||
|
|
||||||
DEFAULT_CONTENT_TYPE = "application/octet-stream"
|
DEFAULT_CONTENT_TYPE = "application/octet-stream"
|
||||||
|
|
||||||
|
MESSAGE_FORMATS = [FORMAT_HTML, FORMAT_TEXT]
|
||||||
|
DEFAULT_MESSAGE_FORMAT = FORMAT_TEXT
|
||||||
|
|
||||||
EVENT_MATRIX_COMMAND = "matrix_command"
|
EVENT_MATRIX_COMMAND = "matrix_command"
|
||||||
|
|
||||||
|
ATTR_FORMAT = "format" # optional message format
|
||||||
ATTR_IMAGES = "images" # optional images
|
ATTR_IMAGES = "images" # optional images
|
||||||
|
|
||||||
COMMAND_SCHEMA = vol.All(
|
COMMAND_SCHEMA = vol.All(
|
||||||
|
@ -74,7 +78,10 @@ CONFIG_SCHEMA = vol.Schema(
|
||||||
SERVICE_SCHEMA_SEND_MESSAGE = vol.Schema(
|
SERVICE_SCHEMA_SEND_MESSAGE = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(ATTR_MESSAGE): cv.string,
|
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.Optional(ATTR_IMAGES): vol.All(cv.ensure_list, [cv.string]),
|
||||||
},
|
},
|
||||||
vol.Required(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]),
|
vol.Required(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]),
|
||||||
|
@ -377,6 +384,9 @@ class MatrixBot:
|
||||||
try:
|
try:
|
||||||
room = self._join_or_get_room(target_room)
|
room = self._join_or_get_room(target_room)
|
||||||
if message is not None:
|
if message is not None:
|
||||||
|
if data.get(ATTR_FORMAT) == FORMAT_HTML:
|
||||||
|
_LOGGER.debug(room.send_html(message))
|
||||||
|
else:
|
||||||
_LOGGER.debug(room.send_text(message))
|
_LOGGER.debug(room.send_text(message))
|
||||||
except MatrixRequestError as ex:
|
except MatrixRequestError as ex:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
|
@ -385,7 +395,7 @@ class MatrixBot:
|
||||||
ex.code,
|
ex.code,
|
||||||
ex.content,
|
ex.content,
|
||||||
)
|
)
|
||||||
if data is not None:
|
if ATTR_IMAGES in data:
|
||||||
for img in data.get(ATTR_IMAGES, []):
|
for img in data.get(ATTR_IMAGES, []):
|
||||||
self._send_image(img, target_rooms)
|
self._send_image(img, target_rooms)
|
||||||
|
|
||||||
|
|
|
@ -2,3 +2,6 @@
|
||||||
DOMAIN = "matrix"
|
DOMAIN = "matrix"
|
||||||
|
|
||||||
SERVICE_SEND_MESSAGE = "send_message"
|
SERVICE_SEND_MESSAGE = "send_message"
|
||||||
|
|
||||||
|
FORMAT_HTML = "html"
|
||||||
|
FORMAT_TEXT = "text"
|
||||||
|
|
|
@ -18,7 +18,7 @@ send_message:
|
||||||
text:
|
text:
|
||||||
data:
|
data:
|
||||||
name: Data
|
name: Data
|
||||||
description: Extended information of notification. Supports list of images. Optional.
|
description: Extended information of notification. Supports list of images. Supports message format. Optional.
|
||||||
example: "{'images': ['/tmp/test.jpg']}"
|
example: "{'images': ['/tmp/test.jpg'], 'format': 'text'}"
|
||||||
selector:
|
selector:
|
||||||
object:
|
object:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue