@@ -119,10 +119,14 @@ async def get_gateway(self):
119
119
return
120
120
gateway : TuyaDevice
121
121
node_host = self ._device_config .host
122
- devices : dict = self ._hass_entry .devices
122
+ devices = self ._hass_entry .devices
123
123
124
124
# Sub to gateway.
125
125
if gateway := devices .get (node_host ):
126
+ # Ensure that sub-device still on the same gateway device.
127
+ if gateway ._local_key != self ._local_key :
128
+ raise Exception ("Sub-device key doesn't match the gateway localkey" )
129
+
126
130
self ._gwateway = gateway
127
131
gateway ._sub_devices [self ._node_id ] = self
128
132
return gateway
@@ -197,28 +201,23 @@ async def _make_connection(self):
197
201
await self ._interface .reset (reset_dpids , cid = self ._node_id )
198
202
199
203
self .debug ("Retrieving initial state" )
200
-
201
204
# Usually we use status instead of detect_available_dps, but some device doesn't reports all dps when ask for status.
202
205
status = await self ._interface .detect_available_dps (cid = self ._node_id )
203
206
if status is None : # and not self.is_subdevice
204
207
raise Exception ("Failed to retrieve status" )
208
+
205
209
self ._interface .start_heartbeat ()
206
210
self .status_updated (status )
207
211
208
- except UnicodeDecodeError as e :
209
- self .exception (f"Connect to { host } failed: due to: { type (e )} " )
210
- await self .abort_connect ()
211
- update_localkey = True
212
- except pytuya .DecodeError as derror :
213
- self .info (f"Initial update state failed { derror } , trying key update" )
212
+ except (UnicodeDecodeError , pytuya .DecodeError ) as e :
213
+ self .exception (f"Connect to { host } failed: due to { type (e )} : { e } " )
214
214
await self .abort_connect ()
215
215
update_localkey = True
216
216
except Exception as e :
217
217
if not (self ._fake_gateway and "Not found" in str (e )):
218
218
e = "Sub device is not connected" if self .is_subdevice else e
219
219
self .warning (f"Connect to { host } failed: { e } " )
220
220
await self .abort_connect ()
221
- update_localkey = True
222
221
except :
223
222
if self ._fake_gateway :
224
223
self .warning (f"Failed to use { name } as gateway." )
0 commit comments