Add images support to matrix notify (#37625)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
parent
5174f63fd8
commit
98d7e6b898
3 changed files with 49 additions and 7 deletions
|
@ -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],
|
||||
)
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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']}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue