Skip to content

Commit 54caacf

Browse files
authored
Restore before checking input entities at startup (pnbruckner#76)
1 parent 6f41f61 commit 54caacf

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

custom_components/composite/device_tracker.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ async def async_added_to_hass(self) -> None:
269269
self._config_entry_updated
270270
)
271271
)
272-
await self.async_request_call(self._process_config_options())
273272
await self.async_request_call(self._restore_state())
273+
await self.async_request_call(self._process_config_options())
274274

275275
async def async_will_remove_from_hass(self) -> None:
276276
"""Run when entity will be removed from hass."""
@@ -356,22 +356,10 @@ async def _config_entry_updated(
356356

357357
async def _restore_state(self) -> None:
358358
"""Restore state."""
359-
360-
# Do we need to restore from saved state and, if so, is there one?
361-
if self._prev_seen and self.entity_picture:
362-
return
363359
if not (last_state := await self.async_get_last_state()):
364360
return
365361

366-
# Even if we don't need to restore most of the state (i.e., if we've been
367-
# updated by at least one new state), we may need to restore entity picture, if
368-
# we had one but the entities we've been updated from so far do not.
369-
if not self.entity_picture and self._use_entity_picture:
370-
self._attr_entity_picture = last_state.attributes.get(ATTR_ENTITY_PICTURE)
371-
372-
if self._prev_seen:
373-
return
374-
362+
self._attr_entity_picture = last_state.attributes.get(ATTR_ENTITY_PICTURE)
375363
self._battery_level = last_state.attributes.get(ATTR_BATTERY_LEVEL)
376364
self._source_type = last_state.attributes[ATTR_SOURCE_TYPE]
377365
self._location_accuracy = last_state.attributes.get(ATTR_GPS_ACCURACY) or 0
@@ -387,9 +375,11 @@ async def _restore_state(self) -> None:
387375
ATTR_ENTITIES
388376
] = self._attr_extra_state_attributes.pop(ATTR_ENTITY_ID)
389377
with suppress(KeyError):
390-
self._attr_extra_state_attributes[ATTR_LAST_SEEN] = dt_util.parse_datetime(
378+
last_seen = dt_util.parse_datetime(
391379
self._attr_extra_state_attributes[ATTR_LAST_SEEN]
392380
)
381+
self._attr_extra_state_attributes[ATTR_LAST_SEEN] = last_seen
382+
self._prev_seen = last_seen
393383
if self.source_type in _SOURCE_TYPE_NON_GPS and (
394384
self.latitude is None or self.longitude is None
395385
):

0 commit comments

Comments
 (0)