2019-04-03 17:40:03 +02:00
|
|
|
"""SynologyChat platform for notify component."""
|
2021-10-23 21:49:04 +03:00
|
|
|
from http import HTTPStatus
|
2018-02-25 05:47:46 -08:00
|
|
|
import json
|
2019-03-20 22:56:46 -07:00
|
|
|
import logging
|
2018-02-25 05:47:46 -08:00
|
|
|
|
|
|
|
import requests
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
from homeassistant.components.notify import (
|
|
|
|
ATTR_DATA,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService,
|
|
|
|
)
|
2021-10-23 21:49:04 +03:00
|
|
|
from homeassistant.const import CONF_RESOURCE, CONF_VERIFY_SSL
|
2019-12-09 14:38:01 +01:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_FILE_URL = "file_url"
|
2018-02-25 05:47:46 -08:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_RESOURCE): cv.url,
|
|
|
|
vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
2018-02-25 05:47:46 -08:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def get_service(hass, config, discovery_info=None):
|
|
|
|
"""Get the Synology Chat notification service."""
|
|
|
|
resource = config.get(CONF_RESOURCE)
|
2018-12-14 18:42:01 +11:00
|
|
|
verify_ssl = config.get(CONF_VERIFY_SSL)
|
2018-02-25 05:47:46 -08:00
|
|
|
|
2018-12-14 18:42:01 +11:00
|
|
|
return SynologyChatNotificationService(resource, verify_ssl)
|
2018-02-25 05:47:46 -08:00
|
|
|
|
|
|
|
|
|
|
|
class SynologyChatNotificationService(BaseNotificationService):
|
|
|
|
"""Implementation of a notification service for Synology Chat."""
|
|
|
|
|
2018-12-14 18:42:01 +11:00
|
|
|
def __init__(self, resource, verify_ssl):
|
2018-02-25 05:47:46 -08:00
|
|
|
"""Initialize the service."""
|
|
|
|
self._resource = resource
|
2018-12-14 18:42:01 +11:00
|
|
|
self._verify_ssl = verify_ssl
|
2018-02-25 05:47:46 -08:00
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
|
|
|
"""Send a message to a user."""
|
2019-07-31 12:25:30 -07:00
|
|
|
data = {"text": message}
|
2018-02-25 05:47:46 -08:00
|
|
|
|
2018-11-01 05:33:51 -07:00
|
|
|
extended_data = kwargs.get(ATTR_DATA)
|
|
|
|
file_url = extended_data.get(ATTR_FILE_URL) if extended_data else None
|
|
|
|
|
|
|
|
if file_url:
|
2019-07-31 12:25:30 -07:00
|
|
|
data["file_url"] = file_url
|
2018-11-01 05:33:51 -07:00
|
|
|
|
2021-04-09 18:58:27 +02:00
|
|
|
to_send = f"payload={json.dumps(data)}"
|
2018-02-25 05:47:46 -08:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
response = requests.post(
|
|
|
|
self._resource, data=to_send, timeout=10, verify=self._verify_ssl
|
|
|
|
)
|
2018-02-25 05:47:46 -08:00
|
|
|
|
2021-10-23 21:49:04 +03:00
|
|
|
if response.status_code not in (HTTPStatus.OK, HTTPStatus.CREATED):
|
2018-02-25 05:47:46 -08:00
|
|
|
_LOGGER.exception(
|
|
|
|
"Error sending message. Response %d: %s:",
|
2019-07-31 12:25:30 -07:00
|
|
|
response.status_code,
|
|
|
|
response.reason,
|
|
|
|
)
|