2020-10-01 07:50:06 +00:00
|
|
|
"""deCONZ lock platform tests."""
|
2020-10-17 16:20:06 +00:00
|
|
|
|
2020-10-01 07:50:06 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
2020-10-06 21:25:57 +00:00
|
|
|
from homeassistant.components.deconz.gateway import get_gateway_from_config_entry
|
2020-10-17 16:20:06 +00:00
|
|
|
from homeassistant.components.lock import (
|
|
|
|
DOMAIN as LOCK_DOMAIN,
|
|
|
|
SERVICE_LOCK,
|
|
|
|
SERVICE_UNLOCK,
|
|
|
|
)
|
2021-02-08 09:45:46 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
STATE_LOCKED,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
STATE_UNLOCKED,
|
|
|
|
)
|
2020-10-01 07:50:06 +00:00
|
|
|
|
2021-02-09 07:31:29 +00:00
|
|
|
from .test_gateway import (
|
|
|
|
DECONZ_WEB_REQUEST,
|
|
|
|
mock_deconz_put_request,
|
|
|
|
setup_deconz_integration,
|
|
|
|
)
|
2020-10-01 07:50:06 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-09 07:31:29 +00:00
|
|
|
async def test_no_locks(hass, aioclient_mock):
|
2020-10-01 07:50:06 +00:00
|
|
|
"""Test that no lock entities are created."""
|
2021-02-09 07:31:29 +00:00
|
|
|
await setup_deconz_integration(hass, aioclient_mock)
|
2020-10-01 07:50:06 +00:00
|
|
|
assert len(hass.states.async_all()) == 0
|
|
|
|
|
|
|
|
|
2021-02-09 07:31:29 +00:00
|
|
|
async def test_locks(hass, aioclient_mock):
|
2020-10-01 07:50:06 +00:00
|
|
|
"""Test that all supported lock entities are created."""
|
|
|
|
data = deepcopy(DECONZ_WEB_REQUEST)
|
|
|
|
data["lights"] = deepcopy(LOCKS)
|
2021-02-09 07:31:29 +00:00
|
|
|
config_entry = await setup_deconz_integration(
|
|
|
|
hass, aioclient_mock, get_state_response=data
|
|
|
|
)
|
2020-10-06 21:25:57 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2020-10-02 09:20:33 +00:00
|
|
|
|
2020-10-01 07:50:06 +00:00
|
|
|
assert len(hass.states.async_all()) == 1
|
2020-10-02 09:20:33 +00:00
|
|
|
assert hass.states.get("lock.door_lock").state == STATE_UNLOCKED
|
2020-10-01 07:50:06 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2020-10-02 09:20:33 +00:00
|
|
|
assert hass.states.get("lock.door_lock").state == STATE_LOCKED
|
|
|
|
|
|
|
|
# Verify service calls
|
2020-10-01 07:50:06 +00:00
|
|
|
|
2021-02-09 07:31:29 +00:00
|
|
|
mock_deconz_put_request(aioclient_mock, config_entry.data, "/lights/1/state")
|
2020-10-01 07:50:06 +00:00
|
|
|
|
2020-10-02 09:20:33 +00:00
|
|
|
# Service lock door
|
|
|
|
|
2021-02-09 07:31:29 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
LOCK_DOMAIN,
|
|
|
|
SERVICE_LOCK,
|
|
|
|
{ATTR_ENTITY_ID: "lock.door_lock"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
assert aioclient_mock.mock_calls[1][2] == {"on": True}
|
2020-10-01 07:50:06 +00:00
|
|
|
|
2020-10-02 09:20:33 +00:00
|
|
|
# Service unlock door
|
|
|
|
|
2021-02-09 07:31:29 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
LOCK_DOMAIN,
|
|
|
|
SERVICE_UNLOCK,
|
|
|
|
{ATTR_ENTITY_ID: "lock.door_lock"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
assert aioclient_mock.mock_calls[2][2] == {"on": False}
|
2020-10-01 07:50:06 +00:00
|
|
|
|
2020-10-06 21:25:57 +00:00
|
|
|
await hass.config_entries.async_unload(config_entry.entry_id)
|
2020-10-01 07:50:06 +00:00
|
|
|
|
2021-02-08 09:45:46 +00:00
|
|
|
states = hass.states.async_all()
|
|
|
|
assert len(hass.states.async_all()) == 1
|
|
|
|
for state in states:
|
|
|
|
assert state.state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
await hass.config_entries.async_remove(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
2020-10-01 07:50:06 +00:00
|
|
|
assert len(hass.states.async_all()) == 0
|