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
|
2017-04-24 19:43:02 +00:00
|
|
|
from homeassistant.components.notify import (ATTR_TARGET, PLATFORM_SCHEMA,
|
2018-05-05 14:00:36 +00:00
|
|
|
BaseNotificationService,
|
|
|
|
ATTR_MESSAGE)
|
2017-03-27 08:35:40 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-10-18 19:13:00 +00:00
|
|
|
|
|
|
|
CONF_DEFAULT_ROOM = 'default_room'
|
|
|
|
|
2018-05-05 14:00:36 +00:00
|
|
|
DOMAIN = 'matrix'
|
2016-10-18 19:13:00 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_DEFAULT_ROOM): cv.string,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-05-05 14:00:36 +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(
|
|
|
|
DOMAIN, 'send_message', service_data=service_data)
|