Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lingering timer in rflink #92460

Merged
merged 1 commit into from
May 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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