diff --git a/homeassistant/components/device_tracker/__init__.py b/homeassistant/components/device_tracker/__init__.py index c87af93eeb5..6c6ae3adea6 100644 --- a/homeassistant/components/device_tracker/__init__.py +++ b/homeassistant/components/device_tracker/__init__.py @@ -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( diff --git a/tests/components/device_tracker/test_init.py b/tests/components/device_tracker/test_init.py index 42778244d7a..e9045ecd02e 100644 --- a/tests/components/device_tracker/test_init.py +++ b/tests/components/device_tracker/test_init.py @@ -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."""