From c0bf3d7f32d64007f99d55f2463ed547ea6703dd Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Sun, 5 Mar 2017 08:06:53 +0100 Subject: [PATCH] Restore flow on device_tracker platform (#6374) * Restore flow on device_tracker platform * fix flow * fix lint --- .../components/device_tracker/__init__.py | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/device_tracker/__init__.py b/homeassistant/components/device_tracker/__init__.py index c11e25ae130..66977222c5d 100644 --- a/homeassistant/components/device_tracker/__init__.py +++ b/homeassistant/components/device_tracker/__init__.py @@ -132,18 +132,6 @@ def async_setup(hass: HomeAssistantType, config: ConfigType): devices = yield from async_load_config(yaml_path, hass, consider_home) tracker = DeviceTracker(hass, consider_home, track_new, devices) - # added_to_hass - add_tasks = [device.async_added_to_hass() for device in devices - if device.track] - if add_tasks: - yield from asyncio.wait(add_tasks, loop=hass.loop) - - # update tracked devices - update_tasks = [device.async_update_ha_state() for device in devices - if device.track] - if update_tasks: - yield from asyncio.wait(update_tasks, loop=hass.loop) - @asyncio.coroutine def async_setup_platform(p_type, p_config, disc_info=None): """Setup a device tracker platform.""" @@ -226,6 +214,8 @@ def async_setup(hass: HomeAssistantType, config: ConfigType): hass.services.async_register( DOMAIN, SERVICE_SEE, async_see_service, descriptions.get(SERVICE_SEE)) + # restore + yield from tracker.async_setup_tracked_device() return True @@ -356,6 +346,27 @@ class DeviceTracker(object): device.stale(now): self.hass.async_add_job(device.async_update_ha_state(True)) + @asyncio.coroutine + def async_setup_tracked_device(self): + """Setup all not exists tracked devices. + + This method is a coroutine. + """ + @asyncio.coroutine + def async_init_single_device(dev): + """Init a single device_tracker entity.""" + yield from dev.async_added_to_hass() + yield from dev.async_update_ha_state() + + tasks = [] + for device in self.devices.values(): + if device.track and not device.last_seen: + tasks.append(self.hass.async_add_job( + async_init_single_device(device))) + + if tasks: + yield from asyncio.wait(tasks, loop=self.hass.loop) + class Device(Entity): """Represent a tracked device."""