hass-core/homeassistant/helpers/dispatcher.py
Pascal Vizeli 81ca978413 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
2017-02-24 17:11:50 -08:00

66 lines
1.9 KiB
Python

"""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."""
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
def async_dispatcher_connect(hass, signal, target):
"""Connect a callable function to a singal.
This method must be run in the event loop.
"""
if DATA_DISPATCHER not in hass.data:
hass.data[DATA_DISPATCHER] = {}
if signal not in hass.data[DATA_DISPATCHER]:
hass.data[DATA_DISPATCHER][signal] = []
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."""
hass.add_job(async_dispatcher_send, hass, signal, *args)
@callback
def async_dispatcher_send(hass, signal, *args):
"""Send signal and data.
This method must be run in the event loop.
"""
target_list = hass.data.get(DATA_DISPATCHER, {}).get(signal, [])
for target in target_list:
hass.async_add_job(target, *args)