From d774ba46c739070b154f40f51ed15a4e9faf7e95 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 14 Nov 2016 20:59:29 -0800 Subject: [PATCH] Fix device tracker sending invalid event data --- homeassistant/components/device_tracker/__init__.py | 7 +++++-- tests/components/device_tracker/test_init.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/device_tracker/__init__.py b/homeassistant/components/device_tracker/__init__.py index 082602b09f8..13194f88894 100644 --- a/homeassistant/components/device_tracker/__init__.py +++ b/homeassistant/components/device_tracker/__init__.py @@ -31,7 +31,7 @@ from homeassistant.util.yaml import dump from homeassistant.helpers.event import track_utc_time_change from homeassistant.const import ( ATTR_GPS_ACCURACY, ATTR_LATITUDE, ATTR_LONGITUDE, - DEVICE_DEFAULT_NAME, STATE_HOME, STATE_NOT_HOME) + DEVICE_DEFAULT_NAME, STATE_HOME, STATE_NOT_HOME, ATTR_ENTITY_ID) DOMAIN = 'device_tracker' DEPENDENCIES = ['zone'] @@ -242,7 +242,10 @@ class DeviceTracker(object): if device.track: device.update_ha_state() - self.hass.bus.fire(EVENT_NEW_DEVICE, device) + self.hass.bus.fire(EVENT_NEW_DEVICE, { + ATTR_ENTITY_ID: device.entity_id, + ATTR_HOST_NAME: device.host_name, + }) # During init, we ignore the group if self.group is not None: diff --git a/tests/components/device_tracker/test_init.py b/tests/components/device_tracker/test_init.py index 1f95c38cd7f..e4576ec9830 100644 --- a/tests/components/device_tracker/test_init.py +++ b/tests/components/device_tracker/test_init.py @@ -1,5 +1,6 @@ """The tests for the device tracker component.""" # pylint: disable=protected-access +import json import logging import unittest from unittest.mock import call, patch @@ -15,6 +16,7 @@ from homeassistant.const import ( STATE_HOME, STATE_NOT_HOME, CONF_PLATFORM) import homeassistant.components.device_tracker as device_tracker from homeassistant.exceptions import HomeAssistantError +from homeassistant.remote import JSONEncoder from tests.common import ( get_test_home_assistant, fire_time_changed, fire_service_discovered, @@ -324,7 +326,16 @@ class TestComponentsDeviceTracker(unittest.TestCase): device_tracker.see(self.hass, 'mac_1', host_name='hello') self.hass.block_till_done() - self.assertEqual(1, len(test_events)) + + assert len(test_events) == 1 + + # Assert we can serialize the event + json.dumps(test_events[0].as_dict(), cls=JSONEncoder) + + assert test_events[0].data == { + 'entity_id': 'device_tracker.hello', + 'host_name': 'hello', + } # pylint: disable=invalid-name def test_not_write_duplicate_yaml_keys(self):