Correct cached stale device tracker handling (#18572)

* Fix async addition of stale devices

* Add comment to mark_stale

* Remove extraneous whitespace
pull/18576/head
arigilder 2018-11-19 06:06:57 -05:00 committed by Paulus Schoutsen
parent 9fa34f0d77
commit 97c493448b
1 changed files with 10 additions and 4 deletions

View File

@ -373,6 +373,7 @@ class DeviceTracker:
for device in self.devices.values(): for device in self.devices.values():
if (device.track and device.last_update_home) and \ if (device.track and device.last_update_home) and \
device.stale(now): device.stale(now):
device.mark_stale()
self.hass.async_create_task(device.async_update_ha_state(True)) self.hass.async_create_task(device.async_update_ha_state(True))
async def async_setup_tracked_device(self): async def async_setup_tracked_device(self):
@ -528,9 +529,15 @@ class Device(Entity):
Async friendly. 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 (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): async def async_update(self):
"""Update state of entity. """Update state of entity.
@ -550,9 +557,7 @@ class Device(Entity):
else: else:
self._state = zone_state.name self._state = zone_state.name
elif self.stale(): elif self.stale():
self._state = STATE_NOT_HOME self.mark_stale()
self.gps = None
self.last_update_home = False
else: else:
self._state = STATE_HOME self._state = STATE_HOME
self.last_update_home = True self.last_update_home = True
@ -563,6 +568,7 @@ class Device(Entity):
if not state: if not state:
return return
self._state = state.state self._state = state.state
self.last_update_home = (state.state == STATE_HOME)
for attr, var in ( for attr, var in (
(ATTR_SOURCE_TYPE, 'source_type'), (ATTR_SOURCE_TYPE, 'source_type'),