|
81 | 81 | VERSION, |
82 | 82 | ) |
83 | 83 | from homeassistant.components.zwave_js.const import ( |
| 84 | + ATTR_COMMAND_CLASS, |
| 85 | + ATTR_ENDPOINT, |
| 86 | + ATTR_METHOD_NAME, |
| 87 | + ATTR_PARAMETERS, |
| 88 | + ATTR_WAIT_FOR_RESULT, |
84 | 89 | CONF_DATA_COLLECTION_OPTED_IN, |
85 | 90 | DOMAIN, |
86 | 91 | ) |
87 | 92 | from homeassistant.components.zwave_js.helpers import get_device_id |
88 | 93 | from homeassistant.core import HomeAssistant |
89 | 94 | from homeassistant.helpers import device_registry as dr |
90 | 95 |
|
91 | | -from tests.common import MockUser |
| 96 | +from tests.common import MockConfigEntry, MockUser |
92 | 97 | from tests.typing import ClientSessionGenerator, WebSocketGenerator |
93 | 98 |
|
94 | 99 | CONTROLLER_PATCH_PREFIX = "zwave_js_server.model.controller.Controller" |
@@ -4828,3 +4833,157 @@ async def test_hard_reset_controller( |
4828 | 4833 |
|
4829 | 4834 | assert not msg["success"] |
4830 | 4835 | assert msg["error"]["code"] == ERR_NOT_FOUND |
| 4836 | + |
| 4837 | + |
| 4838 | +async def test_node_capabilities( |
| 4839 | + hass: HomeAssistant, |
| 4840 | + multisensor_6: Node, |
| 4841 | + integration: MockConfigEntry, |
| 4842 | + hass_ws_client: WebSocketGenerator, |
| 4843 | +) -> None: |
| 4844 | + """Test the node_capabilities websocket command.""" |
| 4845 | + entry = integration |
| 4846 | + ws_client = await hass_ws_client(hass) |
| 4847 | + |
| 4848 | + node = multisensor_6 |
| 4849 | + device = get_device(hass, node) |
| 4850 | + await ws_client.send_json_auto_id( |
| 4851 | + { |
| 4852 | + TYPE: "zwave_js/node_capabilities", |
| 4853 | + DEVICE_ID: device.id, |
| 4854 | + } |
| 4855 | + ) |
| 4856 | + msg = await ws_client.receive_json() |
| 4857 | + assert msg["result"] == { |
| 4858 | + "0": [ |
| 4859 | + { |
| 4860 | + "id": 113, |
| 4861 | + "name": "Notification", |
| 4862 | + "version": 8, |
| 4863 | + "isSecure": False, |
| 4864 | + "is_secure": False, |
| 4865 | + } |
| 4866 | + ] |
| 4867 | + } |
| 4868 | + |
| 4869 | + # Test getting non-existent node fails |
| 4870 | + await ws_client.send_json_auto_id( |
| 4871 | + { |
| 4872 | + TYPE: "zwave_js/node_status", |
| 4873 | + DEVICE_ID: "fake_device", |
| 4874 | + } |
| 4875 | + ) |
| 4876 | + msg = await ws_client.receive_json() |
| 4877 | + assert not msg["success"] |
| 4878 | + assert msg["error"]["code"] == ERR_NOT_FOUND |
| 4879 | + |
| 4880 | + # Test sending command with not loaded entry fails |
| 4881 | + await hass.config_entries.async_unload(entry.entry_id) |
| 4882 | + await hass.async_block_till_done() |
| 4883 | + |
| 4884 | + await ws_client.send_json_auto_id( |
| 4885 | + { |
| 4886 | + TYPE: "zwave_js/node_status", |
| 4887 | + DEVICE_ID: device.id, |
| 4888 | + } |
| 4889 | + ) |
| 4890 | + msg = await ws_client.receive_json() |
| 4891 | + |
| 4892 | + assert not msg["success"] |
| 4893 | + assert msg["error"]["code"] == ERR_NOT_LOADED |
| 4894 | + |
| 4895 | + |
| 4896 | +async def test_invoke_cc_api( |
| 4897 | + hass: HomeAssistant, |
| 4898 | + client, |
| 4899 | + climate_radio_thermostat_ct100_plus_different_endpoints: Node, |
| 4900 | + integration: MockConfigEntry, |
| 4901 | + hass_ws_client: WebSocketGenerator, |
| 4902 | +) -> None: |
| 4903 | + """Test the invoke_cc_api websocket command.""" |
| 4904 | + ws_client = await hass_ws_client(hass) |
| 4905 | + |
| 4906 | + device_radio_thermostat = get_device( |
| 4907 | + hass, climate_radio_thermostat_ct100_plus_different_endpoints |
| 4908 | + ) |
| 4909 | + assert device_radio_thermostat |
| 4910 | + |
| 4911 | + # Test successful invoke_cc_api call with a static endpoint |
| 4912 | + client.async_send_command.return_value = {"response": True} |
| 4913 | + client.async_send_command_no_wait.return_value = {"response": True} |
| 4914 | + |
| 4915 | + # Test with wait_for_result=False (default) |
| 4916 | + await ws_client.send_json_auto_id( |
| 4917 | + { |
| 4918 | + TYPE: "zwave_js/invoke_cc_api", |
| 4919 | + DEVICE_ID: device_radio_thermostat.id, |
| 4920 | + ATTR_COMMAND_CLASS: 67, |
| 4921 | + ATTR_METHOD_NAME: "someMethod", |
| 4922 | + ATTR_PARAMETERS: [1, 2], |
| 4923 | + } |
| 4924 | + ) |
| 4925 | + msg = await ws_client.receive_json() |
| 4926 | + assert msg["success"] |
| 4927 | + assert msg["result"] is None # We did not specify wait_for_result=True |
| 4928 | + |
| 4929 | + await hass.async_block_till_done() |
| 4930 | + |
| 4931 | + assert len(client.async_send_command_no_wait.call_args_list) == 1 |
| 4932 | + args = client.async_send_command_no_wait.call_args[0][0] |
| 4933 | + assert args == { |
| 4934 | + "command": "endpoint.invoke_cc_api", |
| 4935 | + "nodeId": 26, |
| 4936 | + "endpoint": 0, |
| 4937 | + "commandClass": 67, |
| 4938 | + "methodName": "someMethod", |
| 4939 | + "args": [1, 2], |
| 4940 | + } |
| 4941 | + |
| 4942 | + client.async_send_command_no_wait.reset_mock() |
| 4943 | + |
| 4944 | + # Test with wait_for_result=True |
| 4945 | + await ws_client.send_json_auto_id( |
| 4946 | + { |
| 4947 | + TYPE: "zwave_js/invoke_cc_api", |
| 4948 | + DEVICE_ID: device_radio_thermostat.id, |
| 4949 | + ATTR_COMMAND_CLASS: 67, |
| 4950 | + ATTR_ENDPOINT: 0, |
| 4951 | + ATTR_METHOD_NAME: "someMethod", |
| 4952 | + ATTR_PARAMETERS: [1, 2], |
| 4953 | + ATTR_WAIT_FOR_RESULT: True, |
| 4954 | + } |
| 4955 | + ) |
| 4956 | + msg = await ws_client.receive_json() |
| 4957 | + assert msg["success"] |
| 4958 | + assert msg["result"] is True |
| 4959 | + |
| 4960 | + await hass.async_block_till_done() |
| 4961 | + |
| 4962 | + assert len(client.async_send_command.call_args_list) == 1 |
| 4963 | + args = client.async_send_command.call_args[0][0] |
| 4964 | + assert args == { |
| 4965 | + "command": "endpoint.invoke_cc_api", |
| 4966 | + "nodeId": 26, |
| 4967 | + "endpoint": 0, |
| 4968 | + "commandClass": 67, |
| 4969 | + "methodName": "someMethod", |
| 4970 | + "args": [1, 2], |
| 4971 | + } |
| 4972 | + |
| 4973 | + client.async_send_command.side_effect = NotFoundError |
| 4974 | + |
| 4975 | + # Ensure an error is returned |
| 4976 | + await ws_client.send_json_auto_id( |
| 4977 | + { |
| 4978 | + TYPE: "zwave_js/invoke_cc_api", |
| 4979 | + DEVICE_ID: device_radio_thermostat.id, |
| 4980 | + ATTR_COMMAND_CLASS: 67, |
| 4981 | + ATTR_ENDPOINT: 0, |
| 4982 | + ATTR_METHOD_NAME: "someMethod", |
| 4983 | + ATTR_PARAMETERS: [1, 2], |
| 4984 | + ATTR_WAIT_FOR_RESULT: True, |
| 4985 | + } |
| 4986 | + ) |
| 4987 | + msg = await ws_client.receive_json() |
| 4988 | + assert not msg["success"] |
| 4989 | + assert msg["error"] == {"code": "NotFoundError", "message": ""} |
0 commit comments