-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Integration with lockitron #6805
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 1 commit
Commits
Show all changes
2 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
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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| """ | ||
| Lockitron lock platform. | ||
|
|
||
| For more details about this platform, please refer to the documentation | ||
| https://home-assistant.io/components/lockitron/ | ||
| """ | ||
| import logging | ||
|
|
||
| import requests | ||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.components.lock import LockDevice, PLATFORM_SCHEMA | ||
| from homeassistant.const import CONF_ACCESS_TOKEN, CONF_ID | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DOMAIN = 'lockitron' | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_ACCESS_TOKEN): cv.string, | ||
| vol.Required(CONF_ID): cv.string | ||
| }) | ||
| BASE_URL = 'https://api.lockitron.com' | ||
| API_STATE_URL = BASE_URL + '/v2/locks/{}?access_token={}' | ||
| API_ACTION_URL = BASE_URL + '/v2/locks/{}?access_token={}&state={}' | ||
|
|
||
|
|
||
| # pylint: disable=unused-argument | ||
| def setup_platform(hass, config, add_devices, discovery_info=None): | ||
| """Setup the Lockitron platform.""" | ||
| access_token = config.get(CONF_ACCESS_TOKEN) | ||
| device_id = config.get(CONF_ID) | ||
| response = requests.get(API_STATE_URL.format(device_id, access_token)) | ||
| if response.status_code == 200: | ||
| add_devices([Lockitron(response.json()['state'], access_token, | ||
| device_id)]) | ||
| else: | ||
| _LOGGER.error('Error retrieving lock status during init: %s', | ||
| response.text) | ||
|
|
||
|
|
||
| class Lockitron(LockDevice): | ||
| """Representation of a Lockitron lock.""" | ||
|
|
||
| LOCK_STATE = 'lock' | ||
| UNLOCK_STATE = 'unlock' | ||
|
|
||
| def __init__(self, state, access_token, device_id): | ||
| """Initialize the lock.""" | ||
| self._state = state | ||
| self.access_token = access_token | ||
| self.device_id = device_id | ||
|
|
||
| @property | ||
| def should_poll(self): | ||
| """Return True since we need to poll for the lock's new status.""" | ||
| return True | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the device.""" | ||
| return DOMAIN | ||
|
|
||
| @property | ||
| def is_locked(self): | ||
| """Return True if the lock is currently locked, else False.""" | ||
| return self._state == Lockitron.LOCK_STATE | ||
|
|
||
| def lock(self, **kwargs): | ||
| """Lock the device.""" | ||
| self._state = self.do_change_request(Lockitron.LOCK_STATE) | ||
| self.update_ha_state() | ||
|
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. Not needed while it is a polling device |
||
|
|
||
| def unlock(self, **kwargs): | ||
| """Unlock the device.""" | ||
| self._state = self.do_change_request(Lockitron.UNLOCK_STATE) | ||
| self.update_ha_state() | ||
|
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. Not needed while it is a polling device |
||
|
|
||
| def update(self): | ||
| """Update the internal state of the device.""" | ||
| response = requests \ | ||
| .get(API_STATE_URL.format(self.device_id, self.access_token)) | ||
| if response.status_code == 200: | ||
| self._state = response.json()['state'] | ||
| else: | ||
| _LOGGER.error('Error retrieving lock status: %s', response.text) | ||
|
|
||
| def do_change_request(self, requested_state): | ||
| """Execute the change request and pull out the new state.""" | ||
| response = requests.put( | ||
| API_ACTION_URL.format(self.device_id, self.access_token, | ||
| requested_state)) | ||
| if response.status_code == 200: | ||
| return response.json()['state'] | ||
| else: | ||
| _LOGGER.error('Error setting lock state: %s\n%s', | ||
| requested_state, response.text) | ||
| return self._state | ||
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.
Is Default, please remove it