EventBus: return function to unlisten

This commit is contained in:
Paulus Schoutsen committed 2016-08-25 23:25:35 -07:00
1 parent 62ba0fa7a2
commit d9ecc4af64
11 files changed
+183 -83

No files matched your search

+6 -10
View File
@@ -7,8 +7,7 @@ at https://home-assistant.io/components/automation/#state-trigger
import voluptuous as vol
import homeassistant.util.dt as dt_util
from homeassistant.const import (
EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, MATCH_ALL, CONF_PLATFORM)
from homeassistant.const import MATCH_ALL, CONF_PLATFORM
from homeassistant.helpers.event import track_state_change, track_point_in_time
import homeassistant.helpers.config_validation as cv
@@ -60,23 +59,20 @@ def trigger(hass, config, action):
def state_for_listener(now):
"""Fire on state changes after a delay and calls action."""
hass.bus.remove_listener(
EVENT_STATE_CHANGED, attached_state_for_cancel)
remove_state_for_cancel()
call_action()
def state_for_cancel_listener(entity, inner_from_s, inner_to_s):
"""Fire on changes and cancel for listener if changed."""
if inner_to_s.state == to_s.state:
return
hass.bus.remove_listener(EVENT_TIME_CHANGED,
attached_state_for_listener)
hass.bus.remove_listener(EVENT_STATE_CHANGED,
attached_state_for_cancel)
remove_state_for_listener()
remove_state_for_cancel()
attached_state_for_listener = track_point_in_time(
remove_state_for_listener = track_point_in_time(
hass, state_for_listener, dt_util.utcnow() + time_delta)
attached_state_for_cancel = track_state_change(
remove_state_for_cancel = track_state_change(
hass, entity, state_for_cancel_listener)
track_state_change(
+12 -15
View File
@@ -5,7 +5,6 @@ For more details about this platform, please refer to the documentation
https://home-assistant.io/components/demo/
"""
from homeassistant.components.cover import CoverDevice
from homeassistant.const import EVENT_TIME_CHANGED
from homeassistant.helpers.event import track_utc_time_change
@@ -32,8 +31,8 @@ class DemoCover(CoverDevice):
self._tilt_position = tilt_position
self._closing = True
self._closing_tilt = True
self._listener_cover = None
self._listener_cover_tilt = None
self._unsub_listener_cover = None
self._unsub_listener_cover_tilt = None
@property
def name(self):
@@ -120,10 +119,9 @@ class DemoCover(CoverDevice):
"""Stop the cover."""
if self._position is None:
return
if self._listener_cover is not None:
self.hass.bus.remove_listener(EVENT_TIME_CHANGED,
self._listener_cover)
self._listener_cover = None
if self._unsub_listener_cover is not None:
self._unsub_listener_cover()
self._unsub_listener_cover = None
self._set_position = None
def stop_cover_tilt(self, **kwargs):
@@ -131,16 +129,15 @@ class DemoCover(CoverDevice):
if self._tilt_position is None:
return
if self._listener_cover_tilt is not None:
self.hass.bus.remove_listener(EVENT_TIME_CHANGED,
self._listener_cover_tilt)
self._listener_cover_tilt = None
if self._unsub_listener_cover_tilt is not None:
self._unsub_listener_cover_tilt()
self._unsub_listener_cover_tilt = None
self._set_tilt_position = None
def _listen_cover(self):
"""Listen for changes in cover."""
if self._listener_cover is None:
self._listener_cover = track_utc_time_change(
if self._unsub_listener_cover is None:
self._unsub_listener_cover = track_utc_time_change(
self.hass, self._time_changed_cover)
def _time_changed_cover(self, now):
@@ -156,8 +153,8 @@ class DemoCover(CoverDevice):
def _listen_cover_tilt(self):
"""Listen for changes in cover tilt."""
if self._listener_cover_tilt is None:
self._listener_cover_tilt = track_utc_time_change(
if self._unsub_listener_cover_tilt is None:
self._unsub_listener_cover_tilt = track_utc_time_change(
self.hass, self._time_changed_cover_tilt)
def _time_changed_cover_tilt(self, now):
+5 -3
View File
@@ -175,6 +175,7 @@ class Group(Entity):
self.group_off = None
self._assumed_state = False
self._lock = threading.Lock()
self._unsub_state_changed = None
if entity_ids is not None:
self.update_tracked_entity_ids(entity_ids)
@@ -236,15 +237,16 @@ class Group(Entity):
def start(self):
"""Start tracking members."""
track_state_change(
self._unsub_state_changed = track_state_change(
self.hass, self.tracking, self._state_changed_listener)
def stop(self):
"""Unregister the group from Home Assistant."""
self.hass.states.remove(self.entity_id)
self.hass.bus.remove_listener(
ha.EVENT_STATE_CHANGED, self._state_changed_listener)
if self._unsub_state_changed:
self._unsub_state_changed()
self._unsub_state_changed = None
def update(self):
"""Query all members and determine current group state."""
+6 -1
View File
@@ -170,9 +170,14 @@ def subscribe(hass, topic, callback, qos=DEFAULT_QOS):
callback(event.data[ATTR_TOPIC], event.data[ATTR_PAYLOAD],
event.data[ATTR_QOS])
hass.bus.listen(EVENT_MQTT_MESSAGE_RECEIVED, mqtt_topic_subscriber)
remove = hass.bus.listen(EVENT_MQTT_MESSAGE_RECEIVED,
mqtt_topic_subscriber)
# Future: track subscriber count and unsubscribe in remove
MQTT_CLIENT.subscribe(topic, qos)
return remove
def _setup_server(hass, config):
"""Try to start embedded MQTT broker."""
@@ -5,7 +5,6 @@ For more details about this platform, please refer to the documentation
https://home-assistant.io/components/demo/
"""
from homeassistant.components.rollershutter import RollershutterDevice
from homeassistant.const import EVENT_TIME_CHANGED
from homeassistant.helpers.event import track_utc_time_change
@@ -27,7 +26,7 @@ class DemoRollershutter(RollershutterDevice):
self._name = name
self._position = position
self._moving_up = True
self._listener = None
self._unsub_listener = None
@property
def name(self):
@@ -70,15 +69,15 @@ class DemoRollershutter(RollershutterDevice):
def stop(self, **kwargs):
"""Stop the roller shutter."""
if self._listener is not None:
self.hass.bus.remove_listener(EVENT_TIME_CHANGED, self._listener)
self._listener = None
if self._unsub_listener is not None:
self._unsub_listener()
self._unsub_listener = None
def _listen(self):
"""Listen for changes."""
if self._listener is None:
self._listener = track_utc_time_change(self.hass,
self._time_changed)
if self._unsub_listener is None:
self._unsub_listener = track_utc_time_change(self.hass,
self._time_changed)
def _time_changed(self, now):
"""Track time changes."""