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
Wunderlist component #18339
Merged
Merged
Wunderlist component #18339
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
""" | ||
Component to interact with Wunderlist. | ||
|
||
For more details about this component, please refer to the documentation at | ||
https://home-assistant.io/components/wunderlist/ | ||
""" | ||
import logging | ||
|
||
import voluptuous as vol | ||
|
||
import homeassistant.helpers.config_validation as cv | ||
from homeassistant.const import ( | ||
CONF_NAME, CONF_ACCESS_TOKEN) | ||
|
||
REQUIREMENTS = ['wunderpy2==0.1.6'] | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
DOMAIN = 'wunderlist' | ||
CONF_CLIENT_ID = 'client_id' | ||
CONF_LIST_NAME = 'list_name' | ||
CONF_STARRED = 'starred' | ||
|
||
|
||
CONFIG_SCHEMA = vol.Schema({ | ||
DOMAIN: vol.Schema({ | ||
vol.Required(CONF_CLIENT_ID): cv.string, | ||
vol.Required(CONF_ACCESS_TOKEN): cv.string | ||
}) | ||
}, extra=vol.ALLOW_EXTRA) | ||
|
||
|
||
SERVICE_CREATE_TASK = 'create_task' | ||
|
||
SERVICE_SCHEMA_CREATE_TASK = vol.Schema({ | ||
vol.Required(CONF_LIST_NAME): cv.string, | ||
vol.Required(CONF_NAME): cv.string, | ||
vol.Optional(CONF_STARRED): cv.boolean | ||
}) | ||
|
||
|
||
def setup(hass, config): | ||
"""Set up the Wunderlist component.""" | ||
conf = config[DOMAIN] | ||
client_id = conf.get(CONF_CLIENT_ID) | ||
access_token = conf.get(CONF_ACCESS_TOKEN) | ||
data = Wunderlist(access_token, client_id) | ||
if not data.check_credentials(): | ||
_LOGGER.error("Invalid credentials") | ||
return False | ||
|
||
hass.services.register(DOMAIN, 'create_task', data.create_task) | ||
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. The service schema should be used here. |
||
return True | ||
|
||
|
||
class Wunderlist: | ||
"""Representation of an interface to Wunderlist.""" | ||
|
||
def __init__(self, access_token, client_id): | ||
"""Create new instance of Wunderlist component.""" | ||
import wunderpy2 | ||
|
||
api = wunderpy2.WunderApi() | ||
self._client = api.get_client(access_token, client_id) | ||
|
||
_LOGGER.debug("Instance created") | ||
|
||
def check_credentials(self): | ||
"""Check if the provided credentials are valid.""" | ||
try: | ||
self._client.get_lists() | ||
return True | ||
except ValueError: | ||
return False | ||
|
||
def create_task(self, call): | ||
"""Create a new task on a list of Wunderlist.""" | ||
list_name = call.data.get(CONF_LIST_NAME) | ||
task_title = call.data.get(CONF_NAME) | ||
starred = call.data.get(CONF_STARRED) | ||
list_id = self._list_by_name(list_name) | ||
self._client.create_task(list_id, task_title, starred=starred) | ||
return True | ||
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. Nothing is checking this return value. Please remove the statement. |
||
|
||
def _list_by_name(self, name): | ||
"""Return a list ID by name.""" | ||
lists = self._client.get_lists() | ||
tmp = [l for l in lists if l["title"] == name] | ||
if tmp: | ||
return tmp[0]["id"] | ||
return None |
This file contains 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Describes the format for available Wunderlist | ||
|
||
create_task: | ||
description: > | ||
Create a new task in Wunderlist. | ||
fields: | ||
list_name: | ||
description: name of the new list where the task will be created | ||
example: 'Shopping list' | ||
name: | ||
description: name of the new task | ||
example: 'Buy 5 bottles of beer' | ||
starred: | ||
description: Create the task as starred [Optional] | ||
example: true |
This file contains 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
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.
We use constants named
ATTR_*
in service schemas.