hass-core/homeassistant/helpers/dispatcher.py
Pascal Vizeli f2a2d6bfa1 Refactory of envisalink (#6160)
* Refactory of envisalink

* remove event buss

* init dispatcher from hass.

* Move platform to new dispatcher

* fix lint

* add unittest & threadded functions

* fix copy & past error
2017-02-23 13:02:56 -08:00

42 lines
1.1 KiB
Python

"""Helpers for hass dispatcher & internal component / platform."""
from homeassistant.core import callback
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)
@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)
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)