Make connection retries every minute (#288)
* 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
This commit is contained in:
@@ -56,6 +56,7 @@ localtuya:
|
|||||||
"""
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
import homeassistant.helpers.entity_registry as er
|
import homeassistant.helpers.entity_registry as er
|
||||||
@@ -72,6 +73,7 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
from homeassistant.helpers.reload import async_integration_yaml_config
|
from homeassistant.helpers.reload import async_integration_yaml_config
|
||||||
|
|
||||||
from .common import TuyaDevice, async_config_entry_by_device_id
|
from .common import TuyaDevice, async_config_entry_by_device_id
|
||||||
@@ -83,6 +85,8 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
UNSUB_LISTENER = "unsub_listener"
|
UNSUB_LISTENER = "unsub_listener"
|
||||||
|
|
||||||
|
RECONNECT_INTERVAL = timedelta(seconds=60)
|
||||||
|
|
||||||
CONFIG_SCHEMA = config_schema()
|
CONFIG_SCHEMA = config_schema()
|
||||||
|
|
||||||
CONF_DP = "dp"
|
CONF_DP = "dp"
|
||||||
@@ -191,7 +195,7 @@ async def async_setup(hass: HomeAssistant, config: dict):
|
|||||||
_LOGGER.debug("Device %s found with IP %s", device_id, device_ip)
|
_LOGGER.debug("Device %s found with IP %s", device_id, device_ip)
|
||||||
|
|
||||||
device = hass.data[DOMAIN][entry.entry_id][TUYA_DEVICE]
|
device = hass.data[DOMAIN][entry.entry_id][TUYA_DEVICE]
|
||||||
device.connect()
|
device.async_connect()
|
||||||
|
|
||||||
discovery = TuyaDiscovery(_device_discovered)
|
discovery = TuyaDiscovery(_device_discovered)
|
||||||
|
|
||||||
@@ -206,6 +210,18 @@ async def async_setup(hass: HomeAssistant, config: dict):
|
|||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # pylint: disable=broad-except
|
||||||
_LOGGER.exception("failed to set up discovery")
|
_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(
|
hass.helpers.service.async_register_admin_service(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_RELOAD,
|
SERVICE_RELOAD,
|
||||||
@@ -245,6 +261,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
for platform in platforms
|
for platform in platforms
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
device.async_connect()
|
||||||
|
|
||||||
await async_remove_orphan_entities(hass, entry)
|
await async_remove_orphan_entities(hass, entry)
|
||||||
|
|
||||||
|
@@ -127,7 +127,7 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
|
|||||||
"""Return if connected to device."""
|
"""Return if connected to device."""
|
||||||
return self._interface is not None
|
return self._interface is not None
|
||||||
|
|
||||||
def connect(self):
|
def async_connect(self):
|
||||||
"""Connect to device if not already connected."""
|
"""Connect to device if not already connected."""
|
||||||
if not self._is_closing and self._connect_task is None and not self._interface:
|
if not self._is_closing and self._connect_task is None and not self._interface:
|
||||||
self._connect_task = asyncio.create_task(self._make_connection())
|
self._connect_task = asyncio.create_task(self._make_connection())
|
||||||
|
Reference in New Issue
Block a user