Skip to content

Commit

Permalink
Fix stop event cleanup when reloading MQTT (#116525)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored and frenck committed May 1, 2024
1 parent b42f367 commit e1c0895
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions homeassistant/components/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,12 @@
CONF_PORT,
CONF_PROTOCOL,
CONF_USERNAME,
EVENT_HOMEASSISTANT_STARTED,
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.core import (
CALLBACK_TYPE,
CoreState,
Event,
HassJob,
HomeAssistant,
callback,
)
from homeassistant.core import CALLBACK_TYPE, Event, HassJob, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.start import async_at_started
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import bind_hass
from homeassistant.util import dt as dt_util
Expand Down Expand Up @@ -429,24 +422,21 @@ def __init__(
UNSUBSCRIBE_COOLDOWN, self._async_perform_unsubscribes
)
self._pending_unsubscribes: set[str] = set() # topic
self._cleanup_on_unload.extend(
(
async_at_started(hass, self._async_ha_started),
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, self._async_ha_stop),
)
)

if self.hass.state is CoreState.running:
self._ha_started.set()
else:

@callback
def ha_started(_: Event) -> None:
self._ha_started.set()

self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, ha_started)

async def async_stop_mqtt(_event: Event) -> None:
"""Stop MQTT component."""
await self.async_disconnect()
@callback
def _async_ha_started(self, _hass: HomeAssistant) -> None:
"""Handle HA started."""
self._ha_started.set()

self._cleanup_on_unload.append(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop_mqtt)
)
async def _async_ha_stop(self, _event: Event) -> None:
"""Handle HA stop."""
await self.async_disconnect()

def start(
self,
Expand Down

0 comments on commit e1c0895

Please sign in to comment.