Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion custom_components/google_keep/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"issue_tracker": "https://github.com/PiotrMachowski/Home-Assistant-custom-components-Google-Keep/issues",
"dependencies": [],
"codeowners": ["@PiotrMachowski"],
"requirements": ["gkeepapi==0.11.16"],
"requirements": ["gkeepapi==0.11.16","keyring==24.3.0"],
"version": "v1.1.4",
"iot_class": "cloud_polling"
}
25 changes: 20 additions & 5 deletions custom_components/google_keep/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from homeassistant.components.sensor import ENTITY_ID_FORMAT, PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers.entity import async_generate_entity_id, Entity
import keyring

DEFAULT_NAME = 'Google Keep'
CONF_TITLES = 'titles'
Expand Down Expand Up @@ -36,12 +37,26 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
labels = config.get(CONF_LABELS)
pinned = config.get(CONF_PINNED)
keep = gkeepapi.Keep()
login_success = keep.login(username, password)
if not login_success:
raise Exception('Invalid username or password')

token = keyring.get_password("google-keep-token", username)
logged_in = False

# Use an existing master token if one exists
if token:
try:
keep.resume(username, token)
logged_in = True
except gkeepapi.exception.LoginException:
None

# Otherwise, use credentials and login
if not logged_in:
login_success = keep.login(username, password)
if not login_success:
raise Exception('Invalid username or password')
dev = []
hash_value = hashlib.md5(str((username, str(titles), str(labels), pinned)).encode()).hexdigest()[-10:]
uid = '{}_{}'.format(sensor_name, hash_value)
uid = sensor_name #'{}_{}'.format(sensor_name, hash_value)
entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, uid, hass=hass)
dev.append(GoogleKeepSensor(entity_id, sensor_name, username, keep, titles, labels, pinned))
add_entities(dev, True)
Expand All @@ -61,7 +76,7 @@ def __init__(self, entity_id, name, username, keep, titles, labels, pinned):

@property
def name(self):
return '{} - {}'.format(self._name, self._username)
return self._name #'{} - {}'.format(self._name, self._username)

@property
def state(self):
Expand Down