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:
Pascal Vizeli
2017-02-23 22:02:56 +01:00
committed by Paulus Schoutsen
parent 4f990ce488
commit f2a2d6bfa1
7 changed files with 371 additions and 206 deletions

View 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)