2019-04-03 15:40:03 +00:00
|
|
|
"""Rocket.Chat notification service."""
|
2017-10-09 07:38:48 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
2019-03-21 05:56:46 +00:00
|
|
|
CONF_PASSWORD, CONF_ROOM, CONF_URL, CONF_USERNAME)
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
from homeassistant.components.notify import (ATTR_DATA, PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService)
|
2017-10-09 07:38:48 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# pylint: disable=no-value-for-parameter
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_URL): vol.Url(),
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Required(CONF_ROOM): cv.string,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def get_service(hass, config, discovery_info=None):
|
|
|
|
"""Return the notify service."""
|
|
|
|
from rocketchat_API.APIExceptions.RocketExceptions import (
|
|
|
|
RocketConnectionException, RocketAuthenticationException)
|
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
|
|
|
|
url = config.get(CONF_URL)
|
|
|
|
room = config.get(CONF_ROOM)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return RocketChatNotificationService(url, username, password, room)
|
|
|
|
except RocketConnectionException:
|
|
|
|
_LOGGER.warning(
|
2017-10-29 16:28:07 +00:00
|
|
|
"Unable to connect to Rocket.Chat server at %s", url)
|
2017-10-09 07:38:48 +00:00
|
|
|
except RocketAuthenticationException:
|
|
|
|
_LOGGER.warning(
|
2017-10-29 16:28:07 +00:00
|
|
|
"Rocket.Chat authentication failed for user %s", username)
|
|
|
|
_LOGGER.info("Please check your username/password")
|
2017-10-09 07:38:48 +00:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class RocketChatNotificationService(BaseNotificationService):
|
|
|
|
"""Implement the notification service for Rocket.Chat."""
|
|
|
|
|
|
|
|
def __init__(self, url, username, password, room):
|
|
|
|
"""Initialize the service."""
|
|
|
|
from rocketchat_API.rocketchat import RocketChat
|
|
|
|
self._room = room
|
|
|
|
self._server = RocketChat(username, password, server_url=url)
|
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
|
|
|
"""Send a message to Rocket.Chat."""
|
|
|
|
data = kwargs.get(ATTR_DATA) or {}
|
2017-10-29 16:28:07 +00:00
|
|
|
resp = self._server.chat_post_message(
|
|
|
|
message, channel=self._room, **data)
|
2017-10-09 07:38:48 +00:00
|
|
|
if resp.status_code == 200:
|
|
|
|
success = resp.json()["success"]
|
|
|
|
if not success:
|
|
|
|
_LOGGER.error("Unable to post Rocket.Chat message")
|
|
|
|
else:
|
|
|
|
_LOGGER.error("Incorrect status code when posting message: %d",
|
|
|
|
resp.status_code)
|