Skip to content

Commit

Permalink
Fix: Fake Gateway fails due to no DPS found on parent DPS.
Browse files Browse the repository at this point in the history
  • Loading branch information
xZetsubou committed Oct 24, 2023
1 parent fbdc809 commit 0a78f89
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions custom_components/localtuya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def __init__(
self._dev_config_entry: dict = config_entry.data[CONF_DEVICES][dev_id].copy()
self._interface = None
# For SubDevices
self._node_id: str = not gateway and self._dev_config_entry.get(CONF_NODE_ID)
self._node_id: str = self._dev_config_entry.get(CONF_NODE_ID)
self._fake_gateway = gateway
self._gwateway: TuyaDevice = None
self._sub_devices = {}
Expand Down Expand Up @@ -212,6 +212,11 @@ def connected(self):
"""Return if connected to device."""
return self._interface is not None

@property
def is_subdevice(self):
"""Return whether this is a subdevice or not."""
return self._node_id and not self._fake_gateway

async def get_gateway(self):
"""Search for gateway device"""
if not self._node_id:
Expand Down Expand Up @@ -245,7 +250,7 @@ async def _make_connection(self):
name = self._dev_config_entry.get(CONF_FRIENDLY_NAME)

try:
if self._node_id:
if self.is_subdevice:
self._gwateway = await self.get_gateway()
if not self._gwateway.connected or self._gwateway.is_connecting:
self._connect_task = None
Expand All @@ -271,27 +276,21 @@ async def _make_connection(self):
if self._interface is not None:
try:
# If reset dpids set - then assume reset is needed before status.
if (self._default_reset_dpids is not None) and (
len(self._default_reset_dpids) > 0
):
self.debug(
"Resetting command for DP IDs: %s",
self._default_reset_dpids,
)
reset_dpids = self._default_reset_dpids
if (reset_dpids is not None) and (len(reset_dpids) > 0):
self.debug(f"Resetting cmd for DP IDs: {reset_dpids}")
# Assume we want to request status updated for the same set of DP_IDs as the reset ones.
self._interface.set_updatedps_list(self._default_reset_dpids)
self._interface.set_updatedps_list(reset_dpids)

# Reset the interface
await self._interface.reset(
self._default_reset_dpids, cid=self._node_id
)
await self._interface.reset(reset_dpids, cid=self._node_id)

self.debug("Retrieving initial state")

status = await self._interface.status(cid=self._node_id)
if status is None:
raise Exception("Failed to retrieve status")
if not self._node_id:
if not self.is_subdevice:
self._interface.start_heartbeat()
self.status_updated(status)

Expand Down Expand Up @@ -335,7 +334,7 @@ def _new_entity_handler(entity_id):
)

self._is_closing = False
self.info(f"Successfully connected to {name if self._node_id else host}")
self.info(f"Success: connected to {name if self.is_subdevice else host}")
if self._sub_devices:
connect_sub_devices = [
device.async_connect() for device in self._sub_devices.values()
Expand All @@ -347,7 +346,7 @@ def _new_entity_handler(entity_id):

async def abort_connect(self):
"""Abort the connect process to the interface[device]"""
if self._node_id:
if self.is_subdevice:
self._interface = None

if self._interface is not None:
Expand Down

0 comments on commit 0a78f89

Please sign in to comment.