mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Add ability to specify a sender in the clicksend notification (#11046)
* Add ability to specify a sender in the clicksend notification * Style fixes * Fix remaining issue * Add sender validation
This commit is contained in:
parent
1ed0c7d85d
commit
079d4039a1
@ -14,7 +14,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 (
|
from homeassistant.const import (
|
||||||
CONF_API_KEY, CONF_USERNAME, CONF_RECIPIENT, CONTENT_TYPE_JSON)
|
CONF_API_KEY, CONF_RECIPIENT, CONF_SENDER, CONF_USERNAME,
|
||||||
|
CONTENT_TYPE_JSON)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -23,15 +24,27 @@ BASE_API_URL = 'https://rest.clicksend.com/v3'
|
|||||||
|
|
||||||
HEADERS = {CONTENT_TYPE: CONTENT_TYPE_JSON}
|
HEADERS = {CONTENT_TYPE: CONTENT_TYPE_JSON}
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
||||||
vol.Required(CONF_USERNAME): cv.string,
|
def validate_sender(config):
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
"""Set the optional sender name if sender name is not provided."""
|
||||||
vol.Required(CONF_RECIPIENT): cv.string,
|
if CONF_SENDER in config:
|
||||||
})
|
return config
|
||||||
|
config[CONF_SENDER] = config[CONF_RECIPIENT]
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = vol.Schema(
|
||||||
|
vol.All(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_SENDER): cv.string,
|
||||||
|
}), validate_sender))
|
||||||
|
|
||||||
|
|
||||||
def get_service(hass, config, discovery_info=None):
|
def get_service(hass, config, discovery_info=None):
|
||||||
"""Get the ClickSend notification service."""
|
"""Get the ClickSend notification service."""
|
||||||
|
print("#### ", config)
|
||||||
if _authenticate(config) is False:
|
if _authenticate(config) is False:
|
||||||
_LOGGER.exception("You are not authorized to access ClickSend")
|
_LOGGER.exception("You are not authorized to access ClickSend")
|
||||||
return None
|
return None
|
||||||
@ -47,16 +60,26 @@ class ClicksendNotificationService(BaseNotificationService):
|
|||||||
self.username = config.get(CONF_USERNAME)
|
self.username = config.get(CONF_USERNAME)
|
||||||
self.api_key = config.get(CONF_API_KEY)
|
self.api_key = config.get(CONF_API_KEY)
|
||||||
self.recipient = config.get(CONF_RECIPIENT)
|
self.recipient = config.get(CONF_RECIPIENT)
|
||||||
|
self.sender = config.get(CONF_SENDER, CONF_RECIPIENT)
|
||||||
|
|
||||||
def send_message(self, message="", **kwargs):
|
def send_message(self, message="", **kwargs):
|
||||||
"""Send a message to a user."""
|
"""Send a message to a user."""
|
||||||
data = ({'messages': [{'source': 'hass.notify', 'from': self.recipient,
|
data = ({
|
||||||
'to': self.recipient, 'body': message}]})
|
'messages': [
|
||||||
|
{
|
||||||
|
'source': 'hass.notify',
|
||||||
|
'from': self.sender,
|
||||||
|
'to': self.recipient,
|
||||||
|
'body': message,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
api_url = "{}/sms/send".format(BASE_API_URL)
|
api_url = "{}/sms/send".format(BASE_API_URL)
|
||||||
|
|
||||||
resp = requests.post(api_url, data=json.dumps(data), headers=HEADERS,
|
resp = requests.post(
|
||||||
auth=(self.username, self.api_key), timeout=5)
|
api_url, data=json.dumps(data), headers=HEADERS,
|
||||||
|
auth=(self.username, self.api_key), timeout=5)
|
||||||
|
|
||||||
obj = json.loads(resp.text)
|
obj = json.loads(resp.text)
|
||||||
response_msg = obj['response_msg']
|
response_msg = obj['response_msg']
|
||||||
@ -70,9 +93,9 @@ class ClicksendNotificationService(BaseNotificationService):
|
|||||||
def _authenticate(config):
|
def _authenticate(config):
|
||||||
"""Authenticate with ClickSend."""
|
"""Authenticate with ClickSend."""
|
||||||
api_url = '{}/account'.format(BASE_API_URL)
|
api_url = '{}/account'.format(BASE_API_URL)
|
||||||
resp = requests.get(api_url, headers=HEADERS,
|
resp = requests.get(
|
||||||
auth=(config.get(CONF_USERNAME),
|
api_url, headers=HEADERS, auth=(config.get(CONF_USERNAME),
|
||||||
config.get(CONF_API_KEY)), timeout=5)
|
config.get(CONF_API_KEY)), timeout=5)
|
||||||
|
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
return False
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user