2019-04-03 15:40:03 +00:00
|
|
|
"""clicksend_tts platform for notify component."""
|
2021-10-22 17:43:40 +00:00
|
|
|
from http import HTTPStatus
|
2017-10-04 14:34:37 +00:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from aiohttp.hdrs import CONTENT_TYPE
|
2017-11-04 19:04:05 +00:00
|
|
|
import requests
|
2017-10-04 14:34:37 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-12-09 17:56:21 +00:00
|
|
|
from homeassistant.components.notify import PLATFORM_SCHEMA, BaseNotificationService
|
2017-11-04 19:04:05 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_RECIPIENT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2017-10-04 14:34:37 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
BASE_API_URL = "https://rest.clicksend.com/v3"
|
2017-10-04 14:34:37 +00:00
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
HEADERS = {CONTENT_TYPE: CONTENT_TYPE_JSON}
|
2017-10-04 14:34:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_LANGUAGE = "language"
|
|
|
|
CONF_VOICE = "voice"
|
|
|
|
CONF_CALLER = "caller"
|
2017-10-04 14:34:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_LANGUAGE = "en-us"
|
|
|
|
DEFAULT_VOICE = "female"
|
2018-10-24 12:13:07 +00:00
|
|
|
TIMEOUT = 5
|
2017-10-04 14:34:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Required(CONF_RECIPIENT): cv.string,
|
|
|
|
vol.Optional(CONF_LANGUAGE, default=DEFAULT_LANGUAGE): cv.string,
|
|
|
|
vol.Optional(CONF_VOICE, default=DEFAULT_VOICE): cv.string,
|
|
|
|
vol.Optional(CONF_CALLER): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2017-10-04 14:34:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_service(hass, config, discovery_info=None):
|
|
|
|
"""Get the ClickSend notification service."""
|
2018-10-24 12:13:07 +00:00
|
|
|
if not _authenticate(config):
|
2017-10-04 14:34:37 +00:00
|
|
|
_LOGGER.error("You are not authorized to access ClickSend")
|
|
|
|
return None
|
|
|
|
|
|
|
|
return ClicksendNotificationService(config)
|
|
|
|
|
|
|
|
|
|
|
|
class ClicksendNotificationService(BaseNotificationService):
|
|
|
|
"""Implementation of a notification service for the ClickSend service."""
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize the service."""
|
2020-04-14 18:38:55 +00:00
|
|
|
self.username = config[CONF_USERNAME]
|
|
|
|
self.api_key = config[CONF_API_KEY]
|
|
|
|
self.recipient = config[CONF_RECIPIENT]
|
|
|
|
self.language = config[CONF_LANGUAGE]
|
|
|
|
self.voice = config[CONF_VOICE]
|
2019-03-25 01:13:56 +00:00
|
|
|
self.caller = config.get(CONF_CALLER)
|
|
|
|
if self.caller is None:
|
|
|
|
self.caller = self.recipient
|
2017-10-04 14:34:37 +00:00
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
|
|
|
"""Send a voice call to a user."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {
|
|
|
|
"messages": [
|
|
|
|
{
|
|
|
|
"source": "hass.notify",
|
|
|
|
"from": self.caller,
|
|
|
|
"to": self.recipient,
|
|
|
|
"body": message,
|
|
|
|
"lang": self.language,
|
|
|
|
"voice": self.voice,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2019-09-03 15:09:59 +00:00
|
|
|
api_url = f"{BASE_API_URL}/voice/send"
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = requests.post(
|
|
|
|
api_url,
|
|
|
|
data=json.dumps(data),
|
|
|
|
headers=HEADERS,
|
|
|
|
auth=(self.username, self.api_key),
|
|
|
|
timeout=TIMEOUT,
|
|
|
|
)
|
2018-10-24 12:13:07 +00:00
|
|
|
|
2021-10-22 17:43:40 +00:00
|
|
|
if resp.status_code == HTTPStatus.OK:
|
2018-10-24 12:13:07 +00:00
|
|
|
return
|
2017-10-04 14:34:37 +00:00
|
|
|
obj = json.loads(resp.text)
|
2019-07-31 19:25:30 +00:00
|
|
|
response_msg = obj["response_msg"]
|
|
|
|
response_code = obj["response_code"]
|
|
|
|
_LOGGER.error(
|
|
|
|
"Error %s : %s (Code %s)", resp.status_code, response_msg, response_code
|
|
|
|
)
|
2017-10-04 14:34:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _authenticate(config):
|
|
|
|
"""Authenticate with ClickSend."""
|
2019-09-03 15:09:59 +00:00
|
|
|
api_url = f"{BASE_API_URL}/account"
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = requests.get(
|
|
|
|
api_url,
|
|
|
|
headers=HEADERS,
|
|
|
|
auth=(config.get(CONF_USERNAME), config.get(CONF_API_KEY)),
|
|
|
|
timeout=TIMEOUT,
|
|
|
|
)
|
2017-10-04 14:34:37 +00:00
|
|
|
|
2021-10-22 17:43:40 +00:00
|
|
|
return resp.status_code == HTTPStatus.OK
|