Skip to content

Commit

Permalink
Merge pull request #50 from leeyuentuen/rebase-upstream-3_7_0
Browse files Browse the repository at this point in the history
Rebase from master branch (changes from 3.6.6)
  • Loading branch information
leeyuentuen authored Apr 3, 2023
2 parents 1f1a855 + 2aa237b commit 5b9e513
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
11 changes: 11 additions & 0 deletions custom_components/localtuya/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@
HVACMode.HEAT: "1",
HVACMode.AUTO: "0",
},
"hot/cold/wet/wind/auto": {
HVACMode.HEAT: "hot",
HVACMode.COOL: "cold",
HVACMode.DRY: "wet",
HVACMode.FAN_ONLY: "wind",
HVACMode.AUTO: "auto",
},
}
HVAC_ACTION_SETS = {
"True/False": {
Expand All @@ -95,6 +102,10 @@
HVACAction.HEATING: "Heat",
HVACAction.IDLE: "Warming",
},
"heating/idle": {
HVACAction.HEATING: "Heat",
HVACAction.IDLE: "Idle",
},
}
PRESET_SETS = {
"Manual/Holiday/Program": {
Expand Down
48 changes: 30 additions & 18 deletions custom_components/localtuya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,14 @@ async def _make_connection(self):
self,
is_gateway=True,
)

self._interface.start_heartbeat()
self.debug("Connected to gateway %s successfully", self._config_entry[CONF_HOST])
self.debug("Attempting to reconnect %s subdevices", str(len(self._sub_devices.items())))
# Re-add and get status of previously added sub-devices
# Note this assumes the gateway device has not been tear down
for subitem in self._sub_devices.items():
cid = None
dps = None
self.debug("Parsing subdevice %s", str(subitem))

for value in subitem:
# if value is string then it is a cid
Expand All @@ -361,22 +362,26 @@ async def _make_connection(self):
continue


if cid and dps:
self._add_sub_device_interface(cid, dps)
self._dispatch_event(GW_EVT_CONNECTED, None, cid)
self.debug('Dispatch Event GW_EVT_CONNECTED %s', cid)
try:
if cid and dps:
self._add_sub_device_interface(cid, dps)
self._dispatch_event(GW_EVT_CONNECTED, None, cid)
self.debug('Dispatch Event GW_EVT_CONNECTED %s', cid)

# Initial status update
await self._get_sub_device_status(cid)
# Initial status update
await self._get_sub_device_status(cid)

except Exception as e: # pylint: disable=broad-except
self.warning("Adding subdevice %s failed with exception\n %s", cid, str(e))

self._retry_sub_conn_interval = async_track_time_interval(
self._hass,
self._retry_sub_device_connection,
timedelta(seconds=SUB_DEVICE_RECONNECT_INTERVAL),
)

except Exception: # pylint: disable=broad-except
self.warning(f"Connect to gateway {self._config_entry[CONF_HOST]} failed")
except Exception as e: # pylint: disable=broad-except
self.warning("Connect to gateway %s failed with exception\n %s", self._config_entry[CONF_HOST], str(e))
if self._interface is not None:
await self._interface.close()
self._interface = None
Expand Down Expand Up @@ -463,15 +468,20 @@ async def _retry_sub_device_connection(self, _now):
"""Retries sub-device status, to be called by a HASS interval"""

for subitem in self._sub_devices.items():
cid = None
retry = None
for value in subitem:
if not isinstance(value, dict):
return
if isinstance(value, str):
cid = value
continue

if STATUS_RETRY not in value.keys():
return
if STATUS_RETRY in value.keys():
retry = value[STATUS_RETRY]
continue

if value[STATUS_RETRY]:
await self._get_sub_device_status(subitem)
if cid and retry:
if retry:
await self._get_sub_device_status(cid)

async def close(self):
"""Close connection and stop re-connect loop."""
Expand All @@ -495,6 +505,7 @@ def status_updated(self, status):
if cid == "": # Not a status update we are interested in
return


self._dispatch_event(GW_EVT_STATUS_UPDATED, status[cid], cid)

@callback
Expand All @@ -504,15 +515,15 @@ def disconnected(self):
self._retry_sub_conn_interval()
self._retry_sub_conn_interval = None

self.debug("Sending event_disconnected to %s subdevices", str(len(self._sub_devices)))
for cid in self._sub_devices:
self._dispatch_event(GW_EVT_DISCONNECTED, None, cid)
self.debug("Disconnected (TuyaGatewayDevice) - event dispatch event_disconnected")


self._interface = None
self.debug("Disconnected (TuyaGatewayDevice) - waiting for discovery broadcast")

self.async_connect()
# self._connect_task = asyncio.create_task(self._make_connection())

class TuyaSubDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
"""Cache wrapper for a sub-device under a gateway."""
Expand Down Expand Up @@ -591,6 +602,7 @@ def _handle_gateway_event(self, data):
self.status_updated(event_data)
elif event == GW_EVT_CONNECTED:
self.is_connected(True)
self._dispatch_status()
elif event == GW_EVT_DISCONNECTED:
self.disconnected()
else:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ async def async_step_pick_entity_type(self, user_input=None):
# Add a checkbox that allows bailing out from config flow iff at least one
# entity has been added
schema = PICK_ENTITY_SCHEMA
if self.platform is not None:
if self.platform is not None or (CONF_IS_GATEWAY in self.basic_info and self.basic_info[CONF_IS_GATEWAY]):
schema = schema.extend(
{vol.Required(NO_ADDITIONAL_PLATFORMS, default=True): bool}
)
Expand Down

0 comments on commit 5b9e513

Please sign in to comment.