Add new_device_discovered event (#4132)

This commit is contained in:
Lewis Juggins 2016-11-02 04:52:27 +00:00 committed by Paulus Schoutsen
parent e487a09190
commit a5fb284717
2 changed files with 23 additions and 0 deletions

View File

@ -55,6 +55,8 @@ DEFAULT_SCAN_INTERVAL = 12
CONF_AWAY_HIDE = 'hide_if_away'
DEFAULT_AWAY_HIDE = False
EVENT_NEW_DEVICE = 'device_tracker_new_device'
SERVICE_SEE = 'see'
ATTR_MAC = 'mac'
@ -236,9 +238,12 @@ class DeviceTracker(object):
device.seen(host_name, location_name, gps, gps_accuracy, battery,
attributes)
if device.track:
device.update_ha_state()
self.hass.bus.async_fire(EVENT_NEW_DEVICE, device)
# During init, we ignore the group
if self.group is not None:
self.group.update_tracked_entity_ids(

View File

@ -306,6 +306,24 @@ class TestComponentsDeviceTracker(unittest.TestCase):
self.assertEqual(mock_see.call_count, 1)
self.assertEqual(mock_see.call_args, call(**params))
def test_new_device_event_fired(self):
"""Test that the device tracker will fire an event."""
self.assertTrue(setup_component(self.hass, device_tracker.DOMAIN,
TEST_PLATFORM))
test_events = []
def listener(event):
"""Helper method that will verify our event got called."""
test_events.append(event)
self.hass.bus.listen("device_tracker_new_device", listener)
device_tracker.see(self.hass, 'mac_1', host_name='hello')
device_tracker.see(self.hass, 'mac_1', host_name='hello')
self.hass.block_till_done()
self.assertEqual(1, len(test_events))
# pylint: disable=invalid-name
def test_not_write_duplicate_yaml_keys(self):
"""Test that the device tracker will not generate invalid YAML."""