2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Matrix notifications."""
|
2016-10-18 19:13:00 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.notify import (
|
|
|
|
ATTR_TARGET,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService,
|
|
|
|
ATTR_MESSAGE,
|
|
|
|
)
|
2017-03-27 08:35:40 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-10-18 19:13:00 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DEFAULT_ROOM = "default_room"
|
2016-10-18 19:13:00 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "matrix"
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_DEFAULT_ROOM): cv.string})
|
2016-10-18 19:13:00 +00:00
|
|
|
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def get_service(hass, config, discovery_info=None):
|
2016-10-18 19:13:00 +00:00
|
|
|
"""Get the Matrix notification service."""
|
2018-05-05 14:00:36 +00:00
|
|
|
return MatrixNotificationService(config.get(CONF_DEFAULT_ROOM))
|
2016-10-18 19:13:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MatrixNotificationService(BaseNotificationService):
|
2017-04-24 19:43:02 +00:00
|
|
|
"""Send Notifications to a Matrix Room."""
|
2016-10-18 19:13:00 +00:00
|
|
|
|
2018-05-05 14:00:36 +00:00
|
|
|
def __init__(self, default_room):
|
|
|
|
"""Set up the notification service."""
|
|
|
|
self._default_room = default_room
|
2017-04-24 19:43:02 +00:00
|
|
|
|
2018-05-05 14:00:36 +00:00
|
|
|
def send_message(self, message="", **kwargs):
|
2017-04-24 19:43:02 +00:00
|
|
|
"""Send the message to the matrix server."""
|
2018-05-05 14:00:36 +00:00
|
|
|
target_rooms = kwargs.get(ATTR_TARGET) or [self._default_room]
|
2017-04-24 19:43:02 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
service_data = {ATTR_TARGET: target_rooms, ATTR_MESSAGE: message}
|
2017-04-24 19:43:02 +00:00
|
|
|
|
2018-05-05 14:00:36 +00:00
|
|
|
return self.hass.services.call(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN, "send_message", service_data=service_data
|
|
|
|
)
|