Upgrade slacker to 0.9.60 (#9065)

* Upgrade slacker to 0.9.60

* Group imports
This commit is contained in:
Fabian Affolter 2017-08-21 10:23:29 +02:00 committed by GitHub
parent 3c9e09ce16
commit fe7384a4ef
2 changed files with 26 additions and 30 deletions

View File

@ -5,20 +5,19 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.slack/ https://home-assistant.io/components/notify.slack/
""" """
import logging import logging
import requests
from requests.auth import HTTPDigestAuth
from requests.auth import HTTPBasicAuth
import requests
from requests.auth import HTTPBasicAuth
from requests.auth import HTTPDigestAuth
import voluptuous as vol import voluptuous as vol
from homeassistant.components.notify import (
ATTR_TARGET, ATTR_TITLE, ATTR_DATA,
PLATFORM_SCHEMA, BaseNotificationService)
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
from homeassistant.components.notify import (
ATTR_TARGET, ATTR_TITLE, ATTR_DATA, PLATFORM_SCHEMA,
BaseNotificationService)
from homeassistant.const import (CONF_API_KEY, CONF_USERNAME, CONF_ICON)
REQUIREMENTS = ['slacker==0.9.50'] REQUIREMENTS = ['slacker==0.9.60']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -34,7 +33,7 @@ ATTR_FILE_PATH = 'path'
ATTR_FILE_USERNAME = 'username' ATTR_FILE_USERNAME = 'username'
ATTR_FILE_PASSWORD = 'password' ATTR_FILE_PASSWORD = 'password'
ATTR_FILE_AUTH = 'auth' ATTR_FILE_AUTH = 'auth'
# Any other value or absense of 'auth' lead to basic authentication being used # Any other value or absence of 'auth' lead to basic authentication being used
ATTR_FILE_AUTH_DIGEST = 'digest' ATTR_FILE_AUTH_DIGEST = 'digest'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
@ -49,14 +48,14 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
def get_service(hass, config, discovery_info=None): def get_service(hass, config, discovery_info=None):
"""Get the Slack notification service.""" """Get the Slack notification service."""
import slacker import slacker
channel = config.get(CONF_CHANNEL)
api_key = config.get(CONF_API_KEY)
username = config.get(CONF_USERNAME)
icon = config.get(CONF_ICON)
try: try:
return SlackNotificationService( return SlackNotificationService(
config[CONF_CHANNEL], channel, api_key, username, icon, hass.config.is_allowed_path)
config[CONF_API_KEY],
config.get(CONF_USERNAME, None),
config.get(CONF_ICON, None),
hass.config.is_allowed_path)
except slacker.Error: except slacker.Error:
_LOGGER.exception("Authentication failed") _LOGGER.exception("Authentication failed")
@ -66,9 +65,8 @@ def get_service(hass, config, discovery_info=None):
class SlackNotificationService(BaseNotificationService): class SlackNotificationService(BaseNotificationService):
"""Implement the notification service for Slack.""" """Implement the notification service for Slack."""
def __init__(self, default_channel, def __init__(
api_token, username, self, default_channel, api_token, username, icon, is_allowed_path):
icon, is_allowed_path):
"""Initialize the service.""" """Initialize the service."""
from slacker import Slacker from slacker import Slacker
self._default_channel = default_channel self._default_channel = default_channel
@ -101,7 +99,7 @@ class SlackNotificationService(BaseNotificationService):
for target in targets: for target in targets:
try: try:
if file is not None: if file is not None:
# Load from file or url # Load from file or URL
file_as_bytes = self.load_file( file_as_bytes = self.load_file(
url=file.get(ATTR_FILE_URL), url=file.get(ATTR_FILE_URL),
local_path=file.get(ATTR_FILE_PATH), local_path=file.get(ATTR_FILE_PATH),
@ -113,7 +111,7 @@ class SlackNotificationService(BaseNotificationService):
filename = file.get(ATTR_FILE_URL) filename = file.get(ATTR_FILE_URL)
else: else:
filename = file.get(ATTR_FILE_PATH) filename = file.get(ATTR_FILE_PATH)
# Prepare structure for slack API # Prepare structure for Slack API
data = { data = {
'content': None, 'content': None,
'filetype': None, 'filetype': None,
@ -135,35 +133,33 @@ class SlackNotificationService(BaseNotificationService):
except slacker.Error as err: except slacker.Error as err:
_LOGGER.error("Could not send notification. Error: %s", err) _LOGGER.error("Could not send notification. Error: %s", err)
def load_file(self, url=None, local_path=None, def load_file(self, url=None, local_path=None, username=None,
username=None, password=None, auth=None): password=None, auth=None):
"""Load image/document/etc from a local path or url.""" """Load image/document/etc from a local path or URL."""
try: try:
if url is not None: if url is not None:
# check whether authentication parameters are provided # Check whether authentication parameters are provided
if username is not None and password is not None: if username is not None and password is not None:
# Use digest or basic authentication # Use digest or basic authentication
if ATTR_FILE_AUTH_DIGEST == auth: if ATTR_FILE_AUTH_DIGEST == auth:
auth_ = HTTPDigestAuth(username, password) auth_ = HTTPDigestAuth(username, password)
else: else:
auth_ = HTTPBasicAuth(username, password) auth_ = HTTPBasicAuth(username, password)
# load file from url with authentication # Load file from URL with authentication
req = requests.get(url, auth=auth_, timeout=CONF_TIMEOUT) req = requests.get(url, auth=auth_, timeout=CONF_TIMEOUT)
else: else:
# load file from url without authentication # Load file from URL without authentication
req = requests.get(url, timeout=CONF_TIMEOUT) req = requests.get(url, timeout=CONF_TIMEOUT)
return req.content return req.content
elif local_path is not None: elif local_path is not None:
# Check whether path is whitelisted in configuration.yaml # Check whether path is whitelisted in configuration.yaml
if self.is_allowed_path(local_path): if self.is_allowed_path(local_path):
# load file from local path on server
return open(local_path, "rb") return open(local_path, "rb")
_LOGGER.warning("'%s' is not secure to load data from!", _LOGGER.warning("'%s' is not secure to load data from!",
local_path) local_path)
else: else:
# neither url nor path provided _LOGGER.warning("Neither URL nor local path found in params!")
_LOGGER.warning("Neither url nor local path found in params!")
except OSError as error: except OSError as error:
_LOGGER.error("Can't load from url or local path: %s", error) _LOGGER.error("Can't load from url or local path: %s", error)

View File

@ -887,7 +887,7 @@ shodan==1.7.4
simplisafe-python==1.0.4 simplisafe-python==1.0.4
# homeassistant.components.notify.slack # homeassistant.components.notify.slack
slacker==0.9.50 slacker==0.9.60
# homeassistant.components.notify.xmpp # homeassistant.components.notify.xmpp
sleekxmpp==1.3.2 sleekxmpp==1.3.2