Skip to content

Commit

Permalink
Fix lingering timer in rflink (home-assistant#92460)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored and dknowles2 committed May 4, 2023
1 parent aeda442 commit 8e334cc
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions homeassistant/components/rflink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
EVENT_HOMEASSISTANT_STOP,
STATE_ON,
)
from homeassistant.core import CoreState, HomeAssistant, ServiceCall, callback
from homeassistant.core import CoreState, HassJob, HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType

Expand Down Expand Up @@ -246,7 +247,7 @@ def event_callback(event):
)

@callback
def reconnect(exc=None):
def reconnect(_: Exception | None = None) -> None:
"""Schedule reconnect after connection has been unexpectedly lost."""
# Reset protocol binding before starting reconnect
RflinkCommand.set_rflink_protocol(None)
Expand All @@ -258,6 +259,8 @@ def reconnect(exc=None):
_LOGGER.warning("Disconnected from Rflink, reconnecting")
hass.async_create_task(connect())

_reconnect_job = HassJob(reconnect, "Rflink reconnect", cancel_on_shutdown=True)

async def connect():
"""Set up connection and hook it into HA for reconnect/shutdown."""
_LOGGER.info("Initiating Rflink connection")
Expand All @@ -284,15 +287,15 @@ async def connect():
SerialException,
OSError,
asyncio.TimeoutError,
) as exc:
):
reconnect_interval = config[DOMAIN][CONF_RECONNECT_INTERVAL]
_LOGGER.exception(
"Error connecting to Rflink, reconnecting in %s", reconnect_interval
)
# Connection to Rflink device is lost, make entities unavailable
async_dispatcher_send(hass, SIGNAL_AVAILABILITY, False)

hass.loop.call_later(reconnect_interval, reconnect, exc)
async_call_later(hass, reconnect_interval, _reconnect_job)
return

# There is a valid connection to a Rflink device now so
Expand Down

0 comments on commit 8e334cc

Please sign in to comment.