2015-07-31 20:45:41 +00:00
|
|
|
"""
|
|
|
|
Slack platform for notify component.
|
|
|
|
|
2015-10-13 20:45:36 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 17:33:11 +00:00
|
|
|
https://home-assistant.io/components/notify.slack/
|
2015-07-31 20:45:41 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-09-03 15:01:05 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.notify import (
|
|
|
|
PLATFORM_SCHEMA, BaseNotificationService)
|
2016-09-12 07:49:26 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_API_KEY, CONF_USERNAME, CONF_ICON)
|
2016-09-03 15:01:05 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-07-31 20:45:41 +00:00
|
|
|
|
2016-09-06 14:51:23 +00:00
|
|
|
REQUIREMENTS = ['slacker==0.9.25']
|
2016-09-03 15:01:05 +00:00
|
|
|
|
2015-07-31 20:45:41 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-09-03 15:01:05 +00:00
|
|
|
CONF_CHANNEL = 'default_channel'
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Required(CONF_CHANNEL): cv.string,
|
2016-09-12 07:49:26 +00:00
|
|
|
vol.Optional(CONF_USERNAME): cv.string,
|
|
|
|
vol.Optional(CONF_ICON): cv.string,
|
2016-09-03 15:01:05 +00:00
|
|
|
})
|
|
|
|
|
2015-07-31 20:45:41 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-variable
|
|
|
|
def get_service(hass, config):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Get the Slack notification service."""
|
2015-11-09 06:15:34 +00:00
|
|
|
import slacker
|
2015-07-31 20:45:41 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
return SlackNotificationService(
|
2016-09-03 15:01:05 +00:00
|
|
|
config[CONF_CHANNEL],
|
2016-09-12 07:49:26 +00:00
|
|
|
config[CONF_API_KEY],
|
|
|
|
config.get(CONF_USERNAME, None),
|
|
|
|
config.get(CONF_ICON, None))
|
2015-07-31 20:45:41 +00:00
|
|
|
|
2015-11-09 06:15:34 +00:00
|
|
|
except slacker.Error:
|
2016-07-02 18:15:39 +00:00
|
|
|
_LOGGER.exception("Slack authentication failed")
|
2015-11-09 06:15:34 +00:00
|
|
|
return None
|
2015-07-31 20:45:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class SlackNotificationService(BaseNotificationService):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Implement the notification service for Slack."""
|
2015-07-31 20:45:41 +00:00
|
|
|
|
2016-09-12 07:49:26 +00:00
|
|
|
def __init__(self, default_channel, api_token, username, icon):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Initialize the service."""
|
2015-07-31 20:45:41 +00:00
|
|
|
from slacker import Slacker
|
|
|
|
self._default_channel = default_channel
|
|
|
|
self._api_token = api_token
|
2016-09-12 07:49:26 +00:00
|
|
|
self._username = username
|
|
|
|
self._icon = icon
|
|
|
|
if self._username or self._icon:
|
|
|
|
self._as_user = False
|
|
|
|
else:
|
|
|
|
self._as_user = True
|
|
|
|
|
2015-07-31 20:45:41 +00:00
|
|
|
self.slack = Slacker(self._api_token)
|
|
|
|
self.slack.auth.test()
|
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Send a message to a user."""
|
2015-11-09 06:15:34 +00:00
|
|
|
import slacker
|
2015-07-31 20:45:41 +00:00
|
|
|
|
2016-05-09 22:19:19 +00:00
|
|
|
channel = kwargs.get('target') or self._default_channel
|
2016-08-21 18:54:28 +00:00
|
|
|
data = kwargs.get('data')
|
2016-09-03 23:44:30 +00:00
|
|
|
attachments = data.get('attachments') if data else None
|
2016-08-21 18:54:28 +00:00
|
|
|
|
2015-07-31 20:45:41 +00:00
|
|
|
try:
|
2016-08-21 18:54:28 +00:00
|
|
|
self.slack.chat.post_message(channel, message,
|
2016-09-12 07:49:26 +00:00
|
|
|
as_user=self._as_user,
|
|
|
|
username=self._username,
|
|
|
|
icon_emoji=self._icon,
|
2016-09-03 23:44:30 +00:00
|
|
|
attachments=attachments,
|
|
|
|
link_names=True)
|
2016-09-03 15:01:05 +00:00
|
|
|
except slacker.Error as err:
|
|
|
|
_LOGGER.error("Could not send slack notification. Error: %s", err)
|