-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Add lock support to deCONZ #40807
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
Add lock support to deCONZ #40807
Changes from all commits
Commits
Show all changes
3 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
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,59 @@ | ||
| """Support for deCONZ locks.""" | ||
| from homeassistant.components.lock import DOMAIN, LockEntity | ||
| from homeassistant.core import callback | ||
| from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
|
|
||
| from .const import LOCKS, NEW_LIGHT | ||
| from .deconz_device import DeconzDevice | ||
| from .gateway import get_gateway_from_config_entry | ||
|
|
||
|
|
||
| async def async_setup_entry(hass, config_entry, async_add_entities): | ||
| """Set up locks for deCONZ component. | ||
|
|
||
| Locks are based on the same device class as lights in deCONZ. | ||
| """ | ||
| gateway = get_gateway_from_config_entry(hass, config_entry) | ||
| gateway.entities[DOMAIN] = set() | ||
|
|
||
| @callback | ||
| def async_add_lock(lights): | ||
| """Add lock from deCONZ.""" | ||
| entities = [] | ||
|
|
||
| for light in lights: | ||
|
|
||
| if light.type in LOCKS and light.uniqueid not in gateway.entities[DOMAIN]: | ||
| entities.append(DeconzLock(light, gateway)) | ||
|
|
||
| if entities: | ||
| async_add_entities(entities, True) | ||
|
|
||
| gateway.listeners.append( | ||
| async_dispatcher_connect( | ||
| hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_lock | ||
| ) | ||
| ) | ||
|
|
||
| async_add_lock(gateway.api.lights.values()) | ||
|
|
||
|
|
||
| class DeconzLock(DeconzDevice, LockEntity): | ||
| """Representation of a deCONZ lock.""" | ||
|
|
||
| TYPE = DOMAIN | ||
|
|
||
| @property | ||
| def is_locked(self): | ||
| """Return true if lock is on.""" | ||
| return self._device.state | ||
|
|
||
| async def async_lock(self, **kwargs): | ||
| """Lock the lock.""" | ||
| data = {"on": True} | ||
| await self._device.async_set_state(data) | ||
|
|
||
| async def async_unlock(self, **kwargs): | ||
| """Unlock the lock.""" | ||
| data = {"on": False} | ||
| await self._device.async_set_state(data) |
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,96 @@ | ||
| """deCONZ lock platform tests.""" | ||
| from copy import deepcopy | ||
|
|
||
| from homeassistant.components import deconz | ||
| import homeassistant.components.lock as lock | ||
| from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED | ||
| from homeassistant.setup import async_setup_component | ||
|
|
||
| from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration | ||
|
|
||
| from tests.async_mock import patch | ||
|
|
||
| LOCKS = { | ||
| "1": { | ||
| "etag": "5c2ec06cde4bd654aef3a555fcd8ad12", | ||
| "hascolor": False, | ||
| "lastannounced": None, | ||
| "lastseen": "2020-08-22T15:29:03Z", | ||
| "manufacturername": "Danalock", | ||
| "modelid": "V3-BTZB", | ||
| "name": "Door lock", | ||
| "state": {"alert": "none", "on": False, "reachable": True}, | ||
| "swversion": "19042019", | ||
| "type": "Door Lock", | ||
| "uniqueid": "00:00:00:00:00:00:00:00-00", | ||
| } | ||
| } | ||
|
|
||
|
|
||
| async def test_platform_manually_configured(hass): | ||
| """Test that we do not discover anything or try to set up a gateway.""" | ||
| assert ( | ||
| await async_setup_component( | ||
| hass, lock.DOMAIN, {"lock": {"platform": deconz.DOMAIN}} | ||
| ) | ||
| is True | ||
| ) | ||
| assert deconz.DOMAIN not in hass.data | ||
|
|
||
|
|
||
| async def test_no_locks(hass): | ||
| """Test that no lock entities are created.""" | ||
| gateway = await setup_deconz_integration(hass) | ||
| assert len(gateway.deconz_ids) == 0 | ||
| assert len(hass.states.async_all()) == 0 | ||
|
|
||
|
|
||
| async def test_locks(hass): | ||
| """Test that all supported lock entities are created.""" | ||
| data = deepcopy(DECONZ_WEB_REQUEST) | ||
| data["lights"] = deepcopy(LOCKS) | ||
| gateway = await setup_deconz_integration(hass, get_state_response=data) | ||
| assert "lock.door_lock" in gateway.deconz_ids | ||
| assert len(hass.states.async_all()) == 1 | ||
|
|
||
| door_lock = hass.states.get("lock.door_lock") | ||
| assert door_lock.state == STATE_UNLOCKED | ||
|
|
||
| state_changed_event = { | ||
| "t": "event", | ||
| "e": "changed", | ||
| "r": "lights", | ||
| "id": "1", | ||
| "state": {"on": True}, | ||
| } | ||
| gateway.api.event_handler(state_changed_event) | ||
| await hass.async_block_till_done() | ||
|
|
||
| door_lock = hass.states.get("lock.door_lock") | ||
| assert door_lock.state == STATE_LOCKED | ||
|
|
||
| door_lock_device = gateway.api.lights["1"] | ||
|
|
||
| with patch.object(door_lock_device, "_request", return_value=True) as set_callback: | ||
| await hass.services.async_call( | ||
| lock.DOMAIN, | ||
| lock.SERVICE_LOCK, | ||
| {"entity_id": "lock.door_lock"}, | ||
| blocking=True, | ||
| ) | ||
| await hass.async_block_till_done() | ||
| set_callback.assert_called_with("put", "/lights/1/state", json={"on": True}) | ||
|
|
||
| with patch.object(door_lock_device, "_request", return_value=True) as set_callback: | ||
| await hass.services.async_call( | ||
| lock.DOMAIN, | ||
| lock.SERVICE_UNLOCK, | ||
| {"entity_id": "lock.door_lock"}, | ||
| blocking=True, | ||
| ) | ||
| await hass.async_block_till_done() | ||
| set_callback.assert_called_with("put", "/lights/1/state", json={"on": False}) | ||
|
|
||
| await gateway.async_reset() | ||
|
|
||
| assert len(hass.states.async_all()) == 0 | ||
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.
Uh oh!
There was an error while loading. Please reload this page.