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
1920import logging
20-
21- import voluptuous as vol
22- import homeassistant .helpers .config_validation as cv
23-
2421from 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-
5725def 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 )
0 commit comments