-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Add ability to specify a sender in the clicksend notification #11046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,8 @@ | |
| from homeassistant.components.notify import ( | ||
| PLATFORM_SCHEMA, BaseNotificationService) | ||
| 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 | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
@@ -23,15 +24,27 @@ | |
|
|
||
| HEADERS = {CONTENT_TYPE: CONTENT_TYPE_JSON} | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_USERNAME): cv.string, | ||
| vol.Required(CONF_API_KEY): cv.string, | ||
| vol.Required(CONF_RECIPIENT): cv.string, | ||
| }) | ||
|
|
||
| def validate_sender(config): | ||
| """Set the optional sender name if sender name is not provided.""" | ||
| 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): | ||
| """Get the ClickSend notification service.""" | ||
| print("#### ", config) | ||
| if _authenticate(config) is False: | ||
| _LOGGER.exception("You are not authorized to access ClickSend") | ||
| return None | ||
|
|
@@ -47,16 +60,26 @@ def __init__(self, config): | |
| self.username = config.get(CONF_USERNAME) | ||
| self.api_key = config.get(CONF_API_KEY) | ||
| self.recipient = config.get(CONF_RECIPIENT) | ||
| self.sender = config.get(CONF_SENDER, CONF_RECIPIENT) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to add a schema validator that sets the default value during config validation. def validate_sender(config):
if CONF_SENDER in config:
return config
config[CONF_SENDER] = config[CONF_RECIPIENT]
return configThen use this validator like this in the schema: PLATFORM_SCHEMA = vol.Schema(
vol.All(PLATFORM_SCHEMA.extend({...}), validate_sender)) |
||
|
|
||
| def send_message(self, message="", **kwargs): | ||
| """Send a message to a user.""" | ||
| data = ({'messages': [{'source': 'hass.notify', 'from': self.recipient, | ||
| 'to': self.recipient, 'body': message}]}) | ||
| data = ({ | ||
| 'messages': [ | ||
| { | ||
| 'source': 'hass.notify', | ||
| 'from': self.sender, | ||
| 'to': self.recipient, | ||
| 'body': message, | ||
| } | ||
| ] | ||
| }) | ||
|
|
||
| api_url = "{}/sms/send".format(BASE_API_URL) | ||
|
|
||
| resp = requests.post(api_url, data=json.dumps(data), headers=HEADERS, | ||
| auth=(self.username, self.api_key), timeout=5) | ||
| resp = requests.post( | ||
| api_url, data=json.dumps(data), headers=HEADERS, | ||
| auth=(self.username, self.api_key), timeout=5) | ||
|
|
||
| obj = json.loads(resp.text) | ||
| response_msg = obj['response_msg'] | ||
|
|
@@ -70,9 +93,9 @@ def send_message(self, message="", **kwargs): | |
| def _authenticate(config): | ||
| """Authenticate with ClickSend.""" | ||
| api_url = '{}/account'.format(BASE_API_URL) | ||
| resp = requests.get(api_url, headers=HEADERS, | ||
| auth=(config.get(CONF_USERNAME), | ||
| config.get(CONF_API_KEY)), timeout=5) | ||
| resp = requests.get( | ||
| api_url, headers=HEADERS, auth=(config.get(CONF_USERNAME), | ||
| config.get(CONF_API_KEY)), timeout=5) | ||
|
|
||
| if resp.status_code != 200: | ||
| return False | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was left.