From 97c493448bd81e44dc55b76d59aa2d1d4b0f5736 Mon Sep 17 00:00:00 2001 From: arigilder <43716164+arigilder@users.noreply.github.com> Date: Mon, 19 Nov 2018 06:06:57 -0500 Subject: [PATCH] Correct cached stale device tracker handling (#18572) * Fix async addition of stale devices * Add comment to mark_stale * Remove extraneous whitespace --- .../components/device_tracker/__init__.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/device_tracker/__init__.py b/homeassistant/components/device_tracker/__init__.py index 82a9fefbb71..a43a7c93bdc 100644 --- a/homeassistant/components/device_tracker/__init__.py +++ b/homeassistant/components/device_tracker/__init__.py @@ -373,6 +373,7 @@ class DeviceTracker: for device in self.devices.values(): if (device.track and device.last_update_home) and \ device.stale(now): + device.mark_stale() self.hass.async_create_task(device.async_update_ha_state(True)) async def async_setup_tracked_device(self): @@ -528,9 +529,15 @@ class Device(Entity): Async friendly. """ - return self.last_seen and \ + return self.last_seen is None or \ (now or dt_util.utcnow()) - self.last_seen > self.consider_home + def mark_stale(self): + """Mark the device state as stale.""" + self._state = STATE_NOT_HOME + self.gps = None + self.last_update_home = False + async def async_update(self): """Update state of entity. @@ -550,9 +557,7 @@ class Device(Entity): else: self._state = zone_state.name elif self.stale(): - self._state = STATE_NOT_HOME - self.gps = None - self.last_update_home = False + self.mark_stale() else: self._state = STATE_HOME self.last_update_home = True @@ -563,6 +568,7 @@ class Device(Entity): if not state: return self._state = state.state + self.last_update_home = (state.state == STATE_HOME) for attr, var in ( (ATTR_SOURCE_TYPE, 'source_type'),