Files
core/homeassistant/components/notify/slack.py
T
Martin Hjelmare 9db1aa7629 Add discovery notify support and mysensors notify (#5219)
* Add mysensors notify platform

* Make add_devices optional in platform callback function.
* Use new argument structure for all existing mysensors platforms.
* Add notify platform.
* Update mysensors gateway.

* Refactor notify setup

* Enable discovery of notify platforms.
* Update and add tests for notify component and some platforms.
* Continue setup of notify platforms if a platform fails setup.
* Remove notify tests that check platform config. These tests are not
  needed when config validation is used.
* Add config validation to APNS notify platform.
* Use discovery to set up mysensors notify platform.

* Add discovery_info to get_service and update tests

* Add discovery_info as keyword argument to the get_service function
  signature and update all notify platforms.
* Update existing notify tests to check config validation using test
  helper.
* Add removed tests back in that checked config in apns, command_line
  and file platforms, but use config validation test helper to verify
  config.
* Add a test for notify file to increase coverage.
* Fix some PEP issues.

* Fix comments and use more constants

* Move apns notify service under notify domain
2017-01-15 03:53:14 +01:00

89 lines
2.7 KiB
Python

"""
Slack platform for notify component.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.slack/
"""
import logging
import voluptuous as vol
from homeassistant.components.notify import (
ATTR_TARGET, PLATFORM_SCHEMA, BaseNotificationService)
from homeassistant.const import (
CONF_API_KEY, CONF_USERNAME, CONF_ICON)
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['slacker==0.9.30']
_LOGGER = logging.getLogger(__name__)
CONF_CHANNEL = 'default_channel'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_CHANNEL): cv.string,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_ICON): cv.string,
})
# pylint: disable=unused-variable
def get_service(hass, config, discovery_info=None):
"""Get the Slack notification service."""
import slacker
try:
return SlackNotificationService(
config[CONF_CHANNEL],
config[CONF_API_KEY],
config.get(CONF_USERNAME, None),
config.get(CONF_ICON, None))
except slacker.Error:
_LOGGER.exception("Slack authentication failed")
return None
class SlackNotificationService(BaseNotificationService):
"""Implement the notification service for Slack."""
def __init__(self, default_channel, api_token, username, icon):
"""Initialize the service."""
from slacker import Slacker
self._default_channel = default_channel
self._api_token = api_token
self._username = username
self._icon = icon
if self._username or self._icon:
self._as_user = False
else:
self._as_user = True
self.slack = Slacker(self._api_token)
self.slack.auth.test()
def send_message(self, message="", **kwargs):
"""Send a message to a user."""
import slacker
if kwargs.get(ATTR_TARGET) is None:
targets = [self._default_channel]
else:
targets = kwargs.get(ATTR_TARGET)
data = kwargs.get('data')
attachments = data.get('attachments') if data else None
for target in targets:
try:
self.slack.chat.post_message(target, message,
as_user=self._as_user,
username=self._username,
icon_emoji=self._icon,
attachments=attachments,
link_names=True)
except slacker.Error as err:
_LOGGER.error("Could not send slack notification. Error: %s",
err)