1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

Move mqtt from eventbus to dispatcher / add unsub for dispatcher (#6206)

* Move mqtt from eventbus to dispatcher / add unsub for dispatcher

* Fix lint

* Fix test

* Fix lint v2

* fix dispatcher_send
This commit is contained in:
Pascal Vizeli
2017-02-25 02:11:50 +01:00
committed by Paulus Schoutsen
parent d6818c7015
commit 81ca978413
7 changed files with 105 additions and 91 deletions

View File

@@ -1,13 +1,24 @@
"""Helpers for hass dispatcher & internal component / platform."""
import logging
from homeassistant.core import callback
from homeassistant.util.async import run_callback_threadsafe
_LOGGER = logging.getLogger(__name__)
DATA_DISPATCHER = 'dispatcher'
def dispatcher_connect(hass, signal, target):
"""Connect a callable function to a singal."""
hass.add_job(async_dispatcher_connect, hass, signal, target)
async_unsub = run_callback_threadsafe(
hass.loop, async_dispatcher_connect, hass, signal, target).result()
def remove_dispatcher():
"""Remove signal listener."""
run_callback_threadsafe(hass.loop, async_unsub).result()
return remove_dispatcher
@callback
@@ -24,6 +35,19 @@ def async_dispatcher_connect(hass, signal, target):
hass.data[DATA_DISPATCHER][signal].append(target)
@callback
def async_remove_dispatcher():
"""Remove signal listener."""
try:
hass.data[DATA_DISPATCHER][signal].remove(target)
except (KeyError, ValueError):
# KeyError is key target listener did not exist
# ValueError if listener did not exist within signal
_LOGGER.warning(
"Unable to remove unknown dispatcher %s", target)
return async_remove_dispatcher
def dispatcher_send(hass, signal, *args):
"""Send signal and data."""