Ver código fonte

Fix async_test_connection for subdevices

get_device_id should be used here.
Lukas Drbal 2 anos atrás
pai
commit
16c852f918

+ 1 - 1
custom_components/tuya_local/config_flow.py

@@ -192,7 +192,7 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
 
 
 async def async_test_connection(config: dict, hass: HomeAssistant):
 async def async_test_connection(config: dict, hass: HomeAssistant):
     domain_data = hass.data.get(DOMAIN)
     domain_data = hass.data.get(DOMAIN)
-    existing = domain_data.get(config[CONF_DEVICE_ID]) if domain_data else None
+    existing = domain_data.get(get_device_id(config)) if domain_data else None
     if existing:
     if existing:
         existing["device"].pause()
         existing["device"].pause()
         await asyncio.sleep(5)
         await asyncio.sleep(5)

+ 24 - 0
tests/test_config_flow.py

@@ -269,6 +269,30 @@ async def test_async_test_connection_valid(mock_device, hass):
     mock_instance.resume.assert_called_once()
     mock_instance.resume.assert_called_once()
 
 
 
 
+@pytest.mark.asyncio
+@patch("custom_components.tuya_local.config_flow.TuyaLocalDevice")
+async def test_async_test_connection_for_subdevice_valid(mock_device, hass):
+    """Test that subdevice is returned when connection is valid."""
+    mock_instance = AsyncMock()
+    mock_instance.has_returned_state = True
+    mock_device.return_value = mock_instance
+    hass.data[DOMAIN] = {"subdeviceid": {"device": mock_instance}}
+
+    device = await config_flow.async_test_connection(
+        {
+            CONF_DEVICE_ID: "deviceid",
+            CONF_LOCAL_KEY: "localkey",
+            CONF_HOST: "hostname",
+            CONF_PROTOCOL_VERSION: "auto",
+            CONF_DEVICE_CID: "subdeviceid",
+        },
+        hass,
+    )
+    assert device == mock_instance
+    mock_instance.pause.assert_called_once()
+    mock_instance.resume.assert_called_once()
+
+
 @pytest.mark.asyncio
 @pytest.mark.asyncio
 @patch("custom_components.tuya_local.config_flow.TuyaLocalDevice")
 @patch("custom_components.tuya_local.config_flow.TuyaLocalDevice")
 async def test_async_test_connection_invalid(mock_device, hass):
 async def test_async_test_connection_invalid(mock_device, hass):