Skip to content

Commit

Permalink
Make connection retries every minute (#288)
Browse files Browse the repository at this point in the history
* Make connection retries every minute

This mimics the re-connect loop previosuly present, but in a simpler
form. Every 60 seconds, a new connection attempt is made as well as
initially when a device is set up.

* Fix device look up in reconnect loop
  • Loading branch information
postlund committed Mar 5, 2021
1 parent 9a240bc commit 66fdc16
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion custom_components/localtuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"""
import asyncio
import logging
from datetime import timedelta

import homeassistant.helpers.config_validation as cv
import homeassistant.helpers.entity_registry as er
Expand All @@ -72,6 +73,7 @@
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.reload import async_integration_yaml_config

from .common import TuyaDevice, async_config_entry_by_device_id
Expand All @@ -83,6 +85,8 @@

UNSUB_LISTENER = "unsub_listener"

RECONNECT_INTERVAL = timedelta(seconds=60)

CONFIG_SCHEMA = config_schema()

CONF_DP = "dp"
Expand Down Expand Up @@ -191,7 +195,7 @@ def _device_discovered(device):
_LOGGER.debug("Device %s found with IP %s", device_id, device_ip)

device = hass.data[DOMAIN][entry.entry_id][TUYA_DEVICE]
device.connect()
device.async_connect()

discovery = TuyaDiscovery(_device_discovered)

Expand All @@ -206,6 +210,18 @@ def _shutdown(event):
except Exception: # pylint: disable=broad-except
_LOGGER.exception("failed to set up discovery")

async def _async_reconnect(now):
"""Try connecting to devices not already connected to."""
for entry_id, value in hass.data[DOMAIN].items():
if entry_id == DATA_DISCOVERY:
continue

device = value[TUYA_DEVICE]
if not device.connected:
device.async_connect()

async_track_time_interval(hass, _async_reconnect, RECONNECT_INTERVAL)

hass.helpers.service.async_register_admin_service(
DOMAIN,
SERVICE_RELOAD,
Expand Down Expand Up @@ -245,6 +261,7 @@ async def setup_entities():
for platform in platforms
]
)
device.async_connect()

await async_remove_orphan_entities(hass, entry)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/localtuya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def connected(self):
"""Return if connected to device."""
return self._interface is not None

def connect(self):
def async_connect(self):
"""Connect to device if not already connected."""
if not self._is_closing and self._connect_task is None and not self._interface:
self._connect_task = asyncio.create_task(self._make_connection())
Expand Down

0 comments on commit 66fdc16

Please sign in to comment.