EventBus: return function to unlisten
This commit is contained in:
1 parent
62ba0fa7a2
commit
d9ecc4af64
11 files changed
+183
-83
No files matched your search
@@ -149,7 +149,7 @@ class TestMQTT(unittest.TestCase):
|
||||
|
||||
def test_subscribe_topic(self):
|
||||
"""Test the subscription of a topic."""
|
||||
mqtt.subscribe(self.hass, 'test-topic', self.record_calls)
|
||||
unsub = mqtt.subscribe(self.hass, 'test-topic', self.record_calls)
|
||||
|
||||
fire_mqtt_message(self.hass, 'test-topic', 'test-payload')
|
||||
|
||||
@@ -158,6 +158,13 @@ class TestMQTT(unittest.TestCase):
|
||||
self.assertEqual('test-topic', self.calls[0][0])
|
||||
self.assertEqual('test-payload', self.calls[0][1])
|
||||
|
||||
unsub()
|
||||
|
||||
fire_mqtt_message(self.hass, 'test-topic', 'test-payload')
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
|
||||
def test_subscribe_topic_not_match(self):
|
||||
"""Test if subscribed topic is not a match."""
|
||||
mqtt.subscribe(self.hass, 'test-topic', self.record_calls)
|
||||
|
||||
+67
-10
@@ -65,13 +65,21 @@ class TestEventHelpers(unittest.TestCase):
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(2, len(runs))
|
||||
|
||||
unsub = track_point_in_time(
|
||||
self.hass, lambda x: runs.append(1), birthday_paulus)
|
||||
unsub()
|
||||
|
||||
self._send_time_changed(after_birthday)
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(2, len(runs))
|
||||
|
||||
def test_track_time_change(self):
|
||||
"""Test tracking time change."""
|
||||
wildcard_runs = []
|
||||
specific_runs = []
|
||||
|
||||
track_time_change(self.hass, lambda x: wildcard_runs.append(1))
|
||||
track_utc_time_change(
|
||||
unsub = track_time_change(self.hass, lambda x: wildcard_runs.append(1))
|
||||
unsub_utc = track_utc_time_change(
|
||||
self.hass, lambda x: specific_runs.append(1), second=[0, 30])
|
||||
|
||||
self._send_time_changed(datetime(2014, 5, 24, 12, 0, 0))
|
||||
@@ -89,6 +97,14 @@ class TestEventHelpers(unittest.TestCase):
|
||||
self.assertEqual(2, len(specific_runs))
|
||||
self.assertEqual(3, len(wildcard_runs))
|
||||
|
||||
unsub()
|
||||
unsub_utc()
|
||||
|
||||
self._send_time_changed(datetime(2014, 5, 24, 12, 0, 30))
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(2, len(specific_runs))
|
||||
self.assertEqual(3, len(wildcard_runs))
|
||||
|
||||
def test_track_state_change(self):
|
||||
"""Test track_state_change."""
|
||||
# 2 lists to track how often our callbacks get called
|
||||
@@ -186,11 +202,12 @@ class TestEventHelpers(unittest.TestCase):
|
||||
|
||||
# Track sunrise
|
||||
runs = []
|
||||
track_sunrise(self.hass, lambda: runs.append(1))
|
||||
unsub = track_sunrise(self.hass, lambda: runs.append(1))
|
||||
|
||||
offset_runs = []
|
||||
offset = timedelta(minutes=30)
|
||||
track_sunrise(self.hass, lambda: offset_runs.append(1), offset)
|
||||
unsub2 = track_sunrise(self.hass, lambda: offset_runs.append(1),
|
||||
offset)
|
||||
|
||||
# run tests
|
||||
self._send_time_changed(next_rising - offset)
|
||||
@@ -208,6 +225,14 @@ class TestEventHelpers(unittest.TestCase):
|
||||
self.assertEqual(2, len(runs))
|
||||
self.assertEqual(1, len(offset_runs))
|
||||
|
||||
unsub()
|
||||
unsub2()
|
||||
|
||||
self._send_time_changed(next_rising + offset)
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(2, len(runs))
|
||||
self.assertEqual(1, len(offset_runs))
|
||||
|
||||
def test_track_sunset(self):
|
||||
"""Test track the sunset."""
|
||||
latitude = 32.87336
|
||||
@@ -232,11 +257,11 @@ class TestEventHelpers(unittest.TestCase):
|
||||
|
||||
# Track sunset
|
||||
runs = []
|
||||
track_sunset(self.hass, lambda: runs.append(1))
|
||||
unsub = track_sunset(self.hass, lambda: runs.append(1))
|
||||
|
||||
offset_runs = []
|
||||
offset = timedelta(minutes=30)
|
||||
track_sunset(self.hass, lambda: offset_runs.append(1), offset)
|
||||
unsub2 = track_sunset(self.hass, lambda: offset_runs.append(1), offset)
|
||||
|
||||
# Run tests
|
||||
self._send_time_changed(next_setting - offset)
|
||||
@@ -254,6 +279,14 @@ class TestEventHelpers(unittest.TestCase):
|
||||
self.assertEqual(2, len(runs))
|
||||
self.assertEqual(1, len(offset_runs))
|
||||
|
||||
unsub()
|
||||
unsub2()
|
||||
|
||||
self._send_time_changed(next_setting + offset)
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(2, len(runs))
|
||||
self.assertEqual(1, len(offset_runs))
|
||||
|
||||
def _send_time_changed(self, now):
|
||||
"""Send a time changed event."""
|
||||
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now})
|
||||
@@ -262,7 +295,7 @@ class TestEventHelpers(unittest.TestCase):
|
||||
"""Test periodic tasks per minute."""
|
||||
specific_runs = []
|
||||
|
||||
track_utc_time_change(
|
||||
unsub = track_utc_time_change(
|
||||
self.hass, lambda x: specific_runs.append(1), minute='/5')
|
||||
|
||||
self._send_time_changed(datetime(2014, 5, 24, 12, 0, 0))
|
||||
@@ -277,11 +310,17 @@ class TestEventHelpers(unittest.TestCase):
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(2, len(specific_runs))
|
||||
|
||||
unsub()
|
||||
|
||||
self._send_time_changed(datetime(2014, 5, 24, 12, 5, 0))
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(2, len(specific_runs))
|
||||
|
||||
def test_periodic_task_hour(self):
|
||||
"""Test periodic tasks per hour."""
|
||||
specific_runs = []
|
||||
|
||||
track_utc_time_change(
|
||||
unsub = track_utc_time_change(
|
||||
self.hass, lambda x: specific_runs.append(1), hour='/2')
|
||||
|
||||
self._send_time_changed(datetime(2014, 5, 24, 22, 0, 0))
|
||||
@@ -304,11 +343,17 @@ class TestEventHelpers(unittest.TestCase):
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(3, len(specific_runs))
|
||||
|
||||
unsub()
|
||||
|
||||
self._send_time_changed(datetime(2014, 5, 25, 2, 0, 0))
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(3, len(specific_runs))
|
||||
|
||||
def test_periodic_task_day(self):
|
||||
"""Test periodic tasks per day."""
|
||||
specific_runs = []
|
||||
|
||||
track_utc_time_change(
|
||||
unsub = track_utc_time_change(
|
||||
self.hass, lambda x: specific_runs.append(1), day='/2')
|
||||
|
||||
self._send_time_changed(datetime(2014, 5, 2, 0, 0, 0))
|
||||
@@ -323,11 +368,17 @@ class TestEventHelpers(unittest.TestCase):
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(2, len(specific_runs))
|
||||
|
||||
unsub()
|
||||
|
||||
self._send_time_changed(datetime(2014, 5, 4, 0, 0, 0))
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(2, len(specific_runs))
|
||||
|
||||
def test_periodic_task_year(self):
|
||||
"""Test periodic tasks per year."""
|
||||
specific_runs = []
|
||||
|
||||
track_utc_time_change(
|
||||
unsub = track_utc_time_change(
|
||||
self.hass, lambda x: specific_runs.append(1), year='/2')
|
||||
|
||||
self._send_time_changed(datetime(2014, 5, 2, 0, 0, 0))
|
||||
@@ -342,6 +393,12 @@ class TestEventHelpers(unittest.TestCase):
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(2, len(specific_runs))
|
||||
|
||||
unsub()
|
||||
|
||||
self._send_time_changed(datetime(2016, 5, 2, 0, 0, 0))
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(2, len(specific_runs))
|
||||
|
||||
def test_periodic_task_wrong_input(self):
|
||||
"""Test periodic tasks with wrong input."""
|
||||
specific_runs = []
|
||||
|
||||
@@ -175,6 +175,29 @@ class TestEventBus(unittest.TestCase):
|
||||
# Try deleting listener while category doesn't exist either
|
||||
self.bus.remove_listener('test', listener)
|
||||
|
||||
def test_unsubscribe_listener(self):
|
||||
"""Test unsubscribe listener from returned function."""
|
||||
self.bus._pool.add_worker()
|
||||
calls = []
|
||||
|
||||
def listener(event):
|
||||
"""Mock listener."""
|
||||
calls.append(event)
|
||||
|
||||
unsub = self.bus.listen('test', listener)
|
||||
|
||||
self.bus.fire('test')
|
||||
self.bus._pool.block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
unsub()
|
||||
|
||||
self.bus.fire('event')
|
||||
self.bus._pool.block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
def test_listen_once_event(self):
|
||||
"""Test listen_once_event method."""
|
||||
runs = []
|
||||
|
||||
Reference in new issue
Block a user