Skip to content

Commit 5e31420

Browse files
committed
migrating and adding calls
1 parent 27b43cb commit 5e31420

File tree

5 files changed

+99
-57
lines changed

5 files changed

+99
-57
lines changed

custom_components/gkeep/__init__.py

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,27 @@
1-
"""
2-
Custom component for Home Assistant to enable adding to and updating lists on
3-
Google Keep. This component relies on gkeepapi, an unofficial client for the
4-
Google Keep API (https://github.com/kiwiz/gkeepapi).
5-
6-
Example configuration.yaml entry:
7-
8-
gkeep:
9-
username: 'this_is_my_username@gmail.com'
10-
password: 'this_is_my_Google_App_password'
11-
12-
With this custom component loaded, a new service named google_keep.add_to_list
13-
is available. This service data call has two inputs: 'title' and 'things', where
14-
'title' is the title of the Google Keep list, and 'things' is a either a list of
15-
things, or a string. A string input for 'things' is parsed for multiple things
16-
separated by 'and'.
17-
"""
1+
"""Custom component for the Google Keep API"""
2+
3+
from .const import (
4+
DOMAIN,
5+
CONF_USERNAME,
6+
CONF_PASSWORD,
7+
CONF_LIST_NAME,
8+
SERVICE_LIST_NAME,
9+
SERVICE_LIST_ITEM,
10+
SHOPPING_LIST_DOMAIN,
11+
SERVICE_LIST_EMAIL
12+
)
13+
14+
from .schema import (
15+
SERVICE_LIST_SCHEMA,
16+
SERVICE_LIST_NAME_SCHEMA,
17+
SERVICE_LIST_EMAIL_SCHEMA
18+
)
1819

1920
import logging
20-
21-
import voluptuous as vol
22-
import homeassistant.helpers.config_validation as cv
23-
2421
from homeassistant.core import callback
2522

2623
_LOGGER = logging.getLogger(__name__)
2724

28-
# Domain and component constants and validation
29-
DOMAIN = "gkeep"
30-
SHOPPING_LIST_DOMAIN = "shopping_list"
31-
CONF_USERNAME = 'username' # Google account username
32-
CONF_PASSWORD = 'password' # Google App password, https://myaccount.google.com/apppasswords
33-
CONF_LIST_NAME = 'list_name' # Default Google Keep list title
34-
DEFAULT_LIST_NAME = 'Grocery'
35-
36-
CONFIG_SCHEMA = vol.Schema({
37-
DOMAIN: vol.Schema({
38-
vol.Required(CONF_USERNAME): cv.string,
39-
vol.Required(CONF_PASSWORD): cv.string,
40-
vol.Optional(CONF_LIST_NAME, default=DEFAULT_LIST_NAME): cv.string,
41-
}),
42-
}, extra=vol.ALLOW_EXTRA)
43-
44-
# Service constants and validation
45-
SERVICE_LIST_NAME = 'title' # Title of the Google Keep list to create or update, string
46-
SERVICE_LIST_ITEM = 'things' # Things(s) to add to the list
47-
48-
SERVICE_LIST_SCHEMA = vol.Schema({
49-
vol.Optional(SERVICE_LIST_NAME): cv.string,
50-
vol.Required(SERVICE_LIST_ITEM): cv.ensure_list_csv,
51-
})
52-
53-
SERVICE_LIST_NAME_SCHEMA = vol.Schema({
54-
vol.Optional(SERVICE_LIST_NAME): cv.string,
55-
})
56-
5725
def setup(hass, config):
5826
"""Setup the google_keep component."""
5927

@@ -151,11 +119,20 @@ def clear_shopping_list(call):
151119
"""Clear a google keep shopping list."""
152120
list_name = call.data.get(SERVICE_LIST_NAME, default_list_name)
153121

154-
# Sync with Google servers
155-
keep.sync()
156122
google_keep_list = _get_or_create_list_name_(list_name)
157123
google_keep_list.delete()
158-
124+
keep.sync()
125+
126+
def share_shopping_list(call):
127+
"""Share a google keep list"""
128+
list_name = call.data.get(SERVICE_LIST_NAME, default_list_name)
129+
email = call.data.get(SERVICE_LIST_EMAIL)
130+
131+
google_keep_list = _get_or_create_list_name_(list_name)
132+
133+
if not email in google_keep_list.collaborators.all():
134+
google_keep_list.collaborators.add(email)
135+
keep.sync()
159136

160137
def _get_or_create_list_name_(list_name):
161138
""" Find the target list amongst all the Keep notes/lists """
@@ -174,6 +151,7 @@ def _get_or_create_list_name_(list_name):
174151
# Register the service google_keep.add_to_list with Home Assistant.
175152
hass.services.register(DOMAIN, 'add_to_list', add_to_list, schema=SERVICE_LIST_SCHEMA)
176153
hass.services.register(DOMAIN, 'clear_shopping_list', clear_shopping_list, schema=SERVICE_LIST_SCHEMA)
154+
hass.services.register(DOMAIN, 'share_shopping_list', share_shopping_list, schema=SERVICE_LIST_EMAIL_SCHEMA)
177155

178156
# Register the service google_keep.sync_shopping_list with Home Assistant.
179157
SHOPPING_LIST = hass.data.get(SHOPPING_LIST_DOMAIN)

custom_components/gkeep/const.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Constants for Google Keep"""
2+
3+
DOMAIN = "gkeep"
4+
SHOPPING_LIST_DOMAIN = "shopping_list"
5+
CONF_USERNAME = 'username' # Google account username
6+
CONF_PASSWORD = 'password' # Google App password, https://myaccount.google.com/apppasswords
7+
CONF_LIST_NAME = 'list_name' # Default Google Keep list title
8+
DEFAULT_LIST_NAME = 'Grocery'
9+
SERVICE_LIST_NAME = 'title' # Title of the Google Keep list to create or update, string
10+
SERVICE_LIST_ITEM = 'things' # Things(s) to add to the list
11+
SERVICE_LIST_EMAIL = 'email'
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"domain": "gkeep",
33
"name": "Google Keep",
4-
"documentation": "https://github.com/aFrankLion/hass-google_keep",
4+
"documentation": "https://github.com/callumeveratt/hass-google_keep",
55
"dependencies": [],
6-
"version": "0.2.1",
7-
"codeowners": ["@aFrankLion"],
8-
"requirements": ["gkeepapi==0.13.4"]
6+
"version": "0.2.2",
7+
"codeowners": ["@callumeveratt"],
8+
"requirements": ["gkeepapi==0.14.2"]
99
}

custom_components/gkeep/schema.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Schemas for Google Keep"""
2+
3+
from .const import (
4+
CONF_USERNAME,
5+
CONF_PASSWORD,
6+
CONF_LIST_NAME,
7+
SERVICE_LIST_NAME,
8+
SERVICE_LIST_ITEM,
9+
DEFAULT_LIST_NAME,
10+
SERVICE_LIST_EMAIL
11+
)
12+
13+
import voluptuous as vol
14+
import homeassistant.helpers.config_validation as cv
15+
16+
CONFIG_SCHEMA = vol.Schema({
17+
DOMAIN: vol.Schema({
18+
vol.Required(CONF_USERNAME): cv.string,
19+
vol.Required(CONF_PASSWORD): cv.string,
20+
vol.Optional(CONF_LIST_NAME, default=DEFAULT_LIST_NAME): cv.string,
21+
}),
22+
}, extra=vol.ALLOW_EXTRA)
23+
24+
SERVICE_LIST_SCHEMA = vol.Schema({
25+
vol.Optional(SERVICE_LIST_NAME): cv.string,
26+
vol.Required(SERVICE_LIST_ITEM): cv.ensure_list_csv,
27+
})
28+
29+
SERVICE_LIST_NAME_SCHEMA = vol.Schema({
30+
vol.Optional(SERVICE_LIST_NAME): cv.string,
31+
})
32+
33+
SERVICE_LIST_EMAIL_SCHEMA = vol.Schema({
34+
vol.Optional(SERVICE_LIST_NAME): cv.string,
35+
vol.Required(SERVICE_LIST_EMAIL): cv.string
36+
})

custom_components/gkeep/services.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,20 @@ sync_shopping_list:
1616
title:
1717
description: Title of the Google Keep list for syncing the shopping list
1818
example: 'Grocery'
19+
20+
clear_shopping_list:
21+
description: Clear a Google Keep shopping list.
22+
fields:
23+
title:
24+
description: Title of the Google Keep list for syncing the shopping list
25+
example: 'Grocery'
26+
27+
share_shopping_list:
28+
description: Share a Google Keep Shopping list
29+
fields:
30+
title:
31+
description: Title of the Google Keep list for sharing
32+
example: 'Grocery'
33+
email:
34+
description: Email of the sharer
35+
example: 'example@me.com'

0 commit comments

Comments
 (0)