|
| 1 | +"""Fixtures for lock_code_manager tests.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import copy |
| 5 | +import json |
| 6 | +from unittest.mock import DEFAULT, AsyncMock, patch |
| 7 | + |
| 8 | +import pytest |
| 9 | +from zwave_js_server.model.driver import Driver |
| 10 | +from zwave_js_server.model.node import Node |
| 11 | +from zwave_js_server.version import VersionInfo |
| 12 | + |
| 13 | +from homeassistant.core import HomeAssistant |
| 14 | + |
| 15 | +from pytest_homeassistant_custom_component.common import MockConfigEntry |
| 16 | + |
| 17 | +from tests.helpers import load_fixture |
| 18 | + |
| 19 | + |
| 20 | +# Z-Wave JS fixtures |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(name="controller_state", scope="session") |
| 24 | +def controller_state_fixture(): |
| 25 | + """Load the controller state fixture data.""" |
| 26 | + return json.loads(load_fixture("controller_state.json")) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(name="controller_node_state", scope="session") |
| 30 | +def controller_node_state_fixture(): |
| 31 | + """Load the controller node state fixture data.""" |
| 32 | + return json.loads(load_fixture("controller_node_state.json")) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture(name="version_state", scope="session") |
| 36 | +def version_state_fixture(): |
| 37 | + """Load the version state fixture data.""" |
| 38 | + return { |
| 39 | + "type": "version", |
| 40 | + "driverVersion": "6.0.0-beta.0", |
| 41 | + "serverVersion": "1.0.0", |
| 42 | + "homeId": 1234567890, |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(name="log_config_state") |
| 47 | +def log_config_state_fixture(): |
| 48 | + """Return log config state fixture data.""" |
| 49 | + return { |
| 50 | + "enabled": True, |
| 51 | + "level": "info", |
| 52 | + "logToFile": False, |
| 53 | + "filename": "", |
| 54 | + "forceConsole": False, |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(name="lock_schlage_be469_state", scope="session") |
| 59 | +def lock_schlage_be469_state_fixture(): |
| 60 | + """Load the schlage lock node state fixture data.""" |
| 61 | + return json.loads( |
| 62 | + load_fixture("lock_schlage_be469_state.json") |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture(name="lock_august_asl03_state", scope="session") |
| 67 | +def lock_august_asl03_state_fixture(): |
| 68 | + """Load the August Pro lock node state fixture data.""" |
| 69 | + return json.loads( |
| 70 | + load_fixture("lock_august_asl03_state.json") |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture(name="lock_id_lock_as_id150_state", scope="session") |
| 75 | +def lock_id_lock_as_id150_state_fixture(): |
| 76 | + """Load the id lock id-150 lock node state fixture data.""" |
| 77 | + return json.loads( |
| 78 | + load_fixture("/lock_id_lock_as_id150_state.json") |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +# model fixtures |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture(name="listen_block") |
| 86 | +def mock_listen_block_fixture(): |
| 87 | + """Mock a listen block.""" |
| 88 | + return asyncio.Event() |
| 89 | + |
| 90 | + |
| 91 | +@pytest.fixture(name="client") |
| 92 | +def mock_client_fixture( |
| 93 | + controller_state, |
| 94 | + controller_node_state, |
| 95 | + version_state, |
| 96 | + log_config_state, |
| 97 | + listen_block, |
| 98 | +): |
| 99 | + """Mock a client.""" |
| 100 | + with patch( |
| 101 | + "homeassistant.components.zwave_js.ZwaveClient", autospec=True |
| 102 | + ) as client_class: |
| 103 | + client = client_class.return_value |
| 104 | + |
| 105 | + async def connect(): |
| 106 | + await asyncio.sleep(0) |
| 107 | + client.connected = True |
| 108 | + |
| 109 | + async def listen(driver_ready: asyncio.Event) -> None: |
| 110 | + driver_ready.set() |
| 111 | + await listen_block.wait() |
| 112 | + |
| 113 | + async def disconnect(): |
| 114 | + client.connected = False |
| 115 | + |
| 116 | + client.connect = AsyncMock(side_effect=connect) |
| 117 | + client.listen = AsyncMock(side_effect=listen) |
| 118 | + client.disconnect = AsyncMock(side_effect=disconnect) |
| 119 | + client.driver = Driver( |
| 120 | + client, copy.deepcopy(controller_state), copy.deepcopy(log_config_state) |
| 121 | + ) |
| 122 | + node = Node(client, copy.deepcopy(controller_node_state)) |
| 123 | + client.driver.controller.nodes[node.node_id] = node |
| 124 | + |
| 125 | + client.version = VersionInfo.from_message(version_state) |
| 126 | + client.ws_server_url = "ws://test:3000/zjs" |
| 127 | + |
| 128 | + async def async_send_command_side_effect(message, require_schema=None): |
| 129 | + """Return the command response.""" |
| 130 | + if message["command"] == "node.has_device_config_changed": |
| 131 | + return {"changed": False} |
| 132 | + return DEFAULT |
| 133 | + |
| 134 | + client.async_send_command.return_value = { |
| 135 | + "result": {"success": True, "status": 255} |
| 136 | + } |
| 137 | + client.async_send_command.side_effect = async_send_command_side_effect |
| 138 | + |
| 139 | + yield client |
| 140 | + |
| 141 | + |
| 142 | +@pytest.fixture(name="lock_schlage_be469") |
| 143 | +def lock_schlage_be469_fixture(client, lock_schlage_be469_state): |
| 144 | + """Mock a schlage lock node.""" |
| 145 | + node = Node(client, copy.deepcopy(lock_schlage_be469_state)) |
| 146 | + client.driver.controller.nodes[node.node_id] = node |
| 147 | + return node |
| 148 | + |
| 149 | + |
| 150 | +@pytest.fixture(name="lock_august_pro") |
| 151 | +def lock_august_asl03_fixture(client, lock_august_asl03_state): |
| 152 | + """Mock a August Pro lock node.""" |
| 153 | + node = Node(client, copy.deepcopy(lock_august_asl03_state)) |
| 154 | + client.driver.controller.nodes[node.node_id] = node |
| 155 | + return node |
| 156 | + |
| 157 | + |
| 158 | +@pytest.fixture(name="lock_id_lock_as_id150") |
| 159 | +def lock_id_lock_as_id150(client, lock_id_lock_as_id150_state): |
| 160 | + """Mock an id lock id-150 lock node.""" |
| 161 | + node = Node(client, copy.deepcopy(lock_id_lock_as_id150_state)) |
| 162 | + client.driver.controller.nodes[node.node_id] = node |
| 163 | + return node |
| 164 | + |
| 165 | + |
| 166 | +@pytest.fixture(name="integration") |
| 167 | +async def integration_fixture(hass: HomeAssistant, client): |
| 168 | + """Set up the zwave_js integration.""" |
| 169 | + entry = MockConfigEntry(domain="zwave_js", data={"url": "ws://test.org"}) |
| 170 | + entry.add_to_hass(hass) |
| 171 | + await hass.config_entries.async_setup(entry.entry_id) |
| 172 | + await hass.async_block_till_done() |
| 173 | + |
| 174 | + client.async_send_command.reset_mock() |
| 175 | + |
| 176 | + return entry |
| 177 | + |
| 178 | + |
| 179 | +@pytest.fixture(name="lock_id_lock_as_id150_not_ready") |
| 180 | +def node_not_ready(client, lock_id_lock_as_id150_state): |
| 181 | + """Mock an id lock id-150 lock node that's not ready.""" |
| 182 | + state = copy.deepcopy(lock_id_lock_as_id150_state) |
| 183 | + state["ready"] = False |
| 184 | + node = Node(client, state) |
| 185 | + client.driver.controller.nodes[node.node_id] = node |
| 186 | + return node |
0 commit comments