mirror of
https://github.com/home-assistant/core.git
synced 2025-11-09 10:59:40 +00:00
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
This commit is contained in:
committed by
Paulus Schoutsen
parent
4f990ce488
commit
f2a2d6bfa1
42
homeassistant/helpers/dispatcher.py
Normal file
42
homeassistant/helpers/dispatcher.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""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)
|
||||
Reference in New Issue
Block a user