From 63552abce501ab0fe5a6f01f5a8a765b1fd97fb2 Mon Sep 17 00:00:00 2001 From: Mike Megally Date: Sun, 25 Feb 2018 05:47:46 -0800 Subject: [PATCH] Synology Chat as a notification platform (#12596) * first attempt at synology chat as a notification platform * quick fix * houndci and coverage * Cleanup Some cleanup of the file * Ugh underscore * Use string formatting * Remove `CONF_NAME` --- .coveragerc | 1 + .../components/notify/synology_chat.py | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 homeassistant/components/notify/synology_chat.py diff --git a/.coveragerc b/.coveragerc index a1022dcb42e..4cee2925383 100644 --- a/.coveragerc +++ b/.coveragerc @@ -509,6 +509,7 @@ omit = homeassistant/components/notify/simplepush.py homeassistant/components/notify/slack.py homeassistant/components/notify/smtp.py + homeassistant/components/notify/synology_chat.py homeassistant/components/notify/syslog.py homeassistant/components/notify/telegram.py homeassistant/components/notify/telstra.py diff --git a/homeassistant/components/notify/synology_chat.py b/homeassistant/components/notify/synology_chat.py new file mode 100644 index 00000000000..8b968729074 --- /dev/null +++ b/homeassistant/components/notify/synology_chat.py @@ -0,0 +1,53 @@ +""" +SynologyChat platform for notify component. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/notify.synology_chat/ +""" +import logging +import json + +import requests +import voluptuous as vol + +from homeassistant.components.notify import ( + BaseNotificationService, PLATFORM_SCHEMA) +from homeassistant.const import CONF_RESOURCE +import homeassistant.helpers.config_validation as cv + + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_RESOURCE): cv.url, +}) + +_LOGGER = logging.getLogger(__name__) + + +def get_service(hass, config, discovery_info=None): + """Get the Synology Chat notification service.""" + resource = config.get(CONF_RESOURCE) + + return SynologyChatNotificationService(resource) + + +class SynologyChatNotificationService(BaseNotificationService): + """Implementation of a notification service for Synology Chat.""" + + def __init__(self, resource): + """Initialize the service.""" + self._resource = resource + + def send_message(self, message="", **kwargs): + """Send a message to a user.""" + data = { + 'text': message + } + + to_send = 'payload={}'.format(json.dumps(data)) + + response = requests.post(self._resource, data=to_send, timeout=10) + + if response.status_code not in (200, 201): + _LOGGER.exception( + "Error sending message. Response %d: %s:", + response.status_code, response.reason)