소스 검색

Cloud config_flow: avoid using hub product id when adding sub device

When adding a subdevice with cloud config flow, we do a switch part
way through the process to using the hub's details with cid from the
sub device, since local connection details are mostly from the hub.

But this was also switching the product id, at a point before it is
used for config matching, which is incorrect. A related problem was
that during the network scan, the local productKey for the hub would
be reported, and included along with the cloud product id for matching
later.

Ensure that the product id from the sub device goes all the way
through to the matching code, and do not use the local productKey
for subdevices, since we do not see those directly on the local
network.

Issue #3280
Jason Rumney 9 달 전
부모
커밋
24179ce758
2개의 변경된 파일19개의 추가작업 그리고 3개의 파일을 삭제
  1. 10 1
      custom_components/tuya_local/cloud.py
  2. 9 2
      custom_components/tuya_local/config_flow.py

+ 10 - 1
custom_components/tuya_local/cloud.py

@@ -169,7 +169,16 @@ class Cloud:
             )
             existing = existing_id or existing_uuid
             cloud_device["exists"] = existing and existing.get("device")
-            cloud_devices[cloud_device["id"]] = cloud_device
+            if hasattr(device, "node_id"):
+                index = "/".join(
+                    [
+                        cloud_device["id"],
+                        cloud_device["node_id"],
+                    ]
+                )
+            else:
+                index = cloud_device["id"]
+            cloud_devices[index] = cloud_device
 
         return cloud_devices
 

+ 9 - 2
custom_components/tuya_local/config_flow.py

@@ -211,7 +211,11 @@ class ConfigFlowHandler(ConfigFlow, domain=DOMAIN):
                     hub_choice[CONF_DEVICE_CID] = (
                         device_choice["node_id"] or device_choice["uuid"]
                     )
-                    hub_choice[CONF_LOCAL_KEY] = device_choice[CONF_LOCAL_KEY]
+                    if device_choice.get(CONF_LOCAL_KEY):
+                        hub_choice[CONF_LOCAL_KEY] = device_choice[CONF_LOCAL_KEY]
+                    # Communicate the sub device product id to help match the
+                    # correect device config in the next step.
+                    hub_choice["product_id"] = device_choice["product_id"]
                     self.__cloud_device = hub_choice
                     return await self.async_step_search()
                 else:
@@ -299,7 +303,10 @@ class ConfigFlowHandler(ConfigFlow, domain=DOMAIN):
                 _LOGGER.debug(f"Found: {local_device}")
                 self.__cloud_device["ip"] = local_device.get("ip")
                 self.__cloud_device["version"] = local_device.get("version")
-                self.__cloud_device["local_product_id"] = local_device.get("productKey")
+                if not self.__cloud_device.get(CONF_DEVICE_CID):
+                    self.__cloud_device["local_product_id"] = local_device.get(
+                        "productKey"
+                    )
             else:
                 _LOGGER.warning(
                     f"Could not find device: {self.__cloud_device.get('id', 'DEVICE_KEY_UNAVAILABLE')}"