Use websocket fixture in deCONZ lock tests (#47827)

Localize test data
Improve asserts
This commit is contained in:
Robert Svensson 2021-03-18 11:06:44 +01:00 committed by GitHub
parent 3f2d3bd1b2
commit a21d0cadf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,8 +1,7 @@
"""deCONZ lock platform tests.""" """deCONZ lock platform tests."""
from copy import deepcopy from unittest.mock import patch
from homeassistant.components.deconz.gateway import get_gateway_from_config_entry
from homeassistant.components.lock import ( from homeassistant.components.lock import (
DOMAIN as LOCK_DOMAIN, DOMAIN as LOCK_DOMAIN,
SERVICE_LOCK, SERVICE_LOCK,
@ -21,7 +20,17 @@ from .test_gateway import (
setup_deconz_integration, setup_deconz_integration,
) )
LOCKS = {
async def test_no_locks(hass, aioclient_mock):
"""Test that no lock entities are created."""
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
async def test_locks(hass, aioclient_mock, mock_deconz_websocket):
"""Test that all supported lock entities are created."""
data = {
"lights": {
"1": { "1": {
"etag": "5c2ec06cde4bd654aef3a555fcd8ad12", "etag": "5c2ec06cde4bd654aef3a555fcd8ad12",
"hascolor": False, "hascolor": False,
@ -35,38 +44,22 @@ LOCKS = {
"type": "Door Lock", "type": "Door Lock",
"uniqueid": "00:00:00:00:00:00:00:00-00", "uniqueid": "00:00:00:00:00:00:00:00-00",
} }
} }
}
with patch.dict(DECONZ_WEB_REQUEST, data):
async def test_no_locks(hass, aioclient_mock): config_entry = await setup_deconz_integration(hass, aioclient_mock)
"""Test that no lock entities are created."""
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
async def test_locks(hass, aioclient_mock):
"""Test that all supported lock entities are created."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["lights"] = deepcopy(LOCKS)
config_entry = await setup_deconz_integration(
hass, aioclient_mock, get_state_response=data
)
gateway = get_gateway_from_config_entry(hass, config_entry)
assert len(hass.states.async_all()) == 1 assert len(hass.states.async_all()) == 1
assert hass.states.get("lock.door_lock").state == STATE_UNLOCKED assert hass.states.get("lock.door_lock").state == STATE_UNLOCKED
door_lock = hass.states.get("lock.door_lock") event_changed_light = {
assert door_lock.state == STATE_UNLOCKED
state_changed_event = {
"t": "event", "t": "event",
"e": "changed", "e": "changed",
"r": "lights", "r": "lights",
"id": "1", "id": "1",
"state": {"on": True}, "state": {"on": True},
} }
gateway.api.event_handler(state_changed_event) await mock_deconz_websocket(data=event_changed_light)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("lock.door_lock").state == STATE_LOCKED assert hass.states.get("lock.door_lock").state == STATE_LOCKED
@ -98,7 +91,7 @@ async def test_locks(hass, aioclient_mock):
await hass.config_entries.async_unload(config_entry.entry_id) await hass.config_entries.async_unload(config_entry.entry_id)
states = hass.states.async_all() states = hass.states.async_all()
assert len(hass.states.async_all()) == 1 assert len(states) == 1
for state in states: for state in states:
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE