mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Slack notification optional username / icon (#3314)
* Slack notification optional username / icon * Small bugfix to handle defaults better * Dedup'ing code
This commit is contained in:
parent
c90cc77c41
commit
44681ebd55
@ -10,7 +10,8 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.components.notify import (
|
from homeassistant.components.notify import (
|
||||||
PLATFORM_SCHEMA, BaseNotificationService)
|
PLATFORM_SCHEMA, BaseNotificationService)
|
||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import (
|
||||||
|
CONF_API_KEY, CONF_USERNAME, CONF_ICON)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
REQUIREMENTS = ['slacker==0.9.25']
|
REQUIREMENTS = ['slacker==0.9.25']
|
||||||
@ -22,6 +23,8 @@ CONF_CHANNEL = 'default_channel'
|
|||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
vol.Required(CONF_API_KEY): cv.string,
|
||||||
vol.Required(CONF_CHANNEL): cv.string,
|
vol.Required(CONF_CHANNEL): cv.string,
|
||||||
|
vol.Optional(CONF_USERNAME): cv.string,
|
||||||
|
vol.Optional(CONF_ICON): cv.string,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -33,7 +36,9 @@ def get_service(hass, config):
|
|||||||
try:
|
try:
|
||||||
return SlackNotificationService(
|
return SlackNotificationService(
|
||||||
config[CONF_CHANNEL],
|
config[CONF_CHANNEL],
|
||||||
config[CONF_API_KEY])
|
config[CONF_API_KEY],
|
||||||
|
config.get(CONF_USERNAME, None),
|
||||||
|
config.get(CONF_ICON, None))
|
||||||
|
|
||||||
except slacker.Error:
|
except slacker.Error:
|
||||||
_LOGGER.exception("Slack authentication failed")
|
_LOGGER.exception("Slack authentication failed")
|
||||||
@ -44,11 +49,18 @@ def get_service(hass, config):
|
|||||||
class SlackNotificationService(BaseNotificationService):
|
class SlackNotificationService(BaseNotificationService):
|
||||||
"""Implement the notification service for Slack."""
|
"""Implement the notification service for Slack."""
|
||||||
|
|
||||||
def __init__(self, default_channel, api_token):
|
def __init__(self, default_channel, api_token, username, icon):
|
||||||
"""Initialize the service."""
|
"""Initialize the service."""
|
||||||
from slacker import Slacker
|
from slacker import Slacker
|
||||||
self._default_channel = default_channel
|
self._default_channel = default_channel
|
||||||
self._api_token = api_token
|
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 = Slacker(self._api_token)
|
||||||
self.slack.auth.test()
|
self.slack.auth.test()
|
||||||
|
|
||||||
@ -62,7 +74,9 @@ class SlackNotificationService(BaseNotificationService):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
self.slack.chat.post_message(channel, message,
|
self.slack.chat.post_message(channel, message,
|
||||||
as_user=True,
|
as_user=self._as_user,
|
||||||
|
username=self._username,
|
||||||
|
icon_emoji=self._icon,
|
||||||
attachments=attachments,
|
attachments=attachments,
|
||||||
link_names=True)
|
link_names=True)
|
||||||
except slacker.Error as err:
|
except slacker.Error as err:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user