1
mirror of https://github.com/home-assistant/core synced 2024-08-31 05:57:13 +02:00
ha-core/homeassistant/components/notify/matrix.py
Lukas Barth af8cd63838 Matrix Chatbot (#13355)
* Add first version of the Matrix bot

* It's a stupid but necessary change…

* Dont list it twice

* All hail the linter!

* More linter-pleasing

* Use the correct user ID

* Add expression commands

* Add tests for new validators

* Fix room alias handling

* Wording

* Defer setup

* Simplify commands

* Handle exceptions

* Update requirements

* Review

* Move login back to constructor

* Fix review comments
2018-05-05 10:00:36 -04:00

51 lines
1.4 KiB
Python

"""
Matrix notification service.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.matrix/
"""
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.notify import (ATTR_TARGET, PLATFORM_SCHEMA,
BaseNotificationService,
ATTR_MESSAGE)
_LOGGER = logging.getLogger(__name__)
CONF_DEFAULT_ROOM = 'default_room'
DOMAIN = 'matrix'
DEPENDENCIES = [DOMAIN]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_DEFAULT_ROOM): cv.string,
})
def get_service(hass, config, discovery_info=None):
"""Get the Matrix notification service."""
return MatrixNotificationService(config.get(CONF_DEFAULT_ROOM))
class MatrixNotificationService(BaseNotificationService):
"""Send Notifications to a Matrix Room."""
def __init__(self, default_room):
"""Set up the notification service."""
self._default_room = default_room
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
}
return self.hass.services.call(
DOMAIN, 'send_message', service_data=service_data)