From 04005c21c7bece3bf841311465b231df88abc6fd Mon Sep 17 00:00:00 2001 From: bruxy70 Date: Mon, 21 Nov 2022 08:13:07 +0100 Subject: [PATCH] #439 Check for attributes on restore --- custom_components/garbage_collection/sensor.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/custom_components/garbage_collection/sensor.py b/custom_components/garbage_collection/sensor.py index 20de58c..86d3f27 100644 --- a/custom_components/garbage_collection/sensor.py +++ b/custom_components/garbage_collection/sensor.py @@ -143,11 +143,21 @@ async def async_added_to_hass(self) -> None: if (state := await self.async_get_last_state()) is not None: self._last_updated = None # Unblock update - after options change self._attr_state = state.state - self._days = state.attributes[const.ATTR_DAYS] - next_date = helpers.parse_datetime(state.attributes[const.ATTR_NEXT_DATE]) + self._days = ( + state.attributes[const.ATTR_DAYS] + if const.ATTR_DAYS in state.attributes + else None + ) + next_date = ( + helpers.parse_datetime(state.attributes[const.ATTR_NEXT_DATE]) + if const.ATTR_NEXT_DATE in state.attributes + else None + ) self._next_date = None if next_date is None else next_date.date() - self.last_collection = helpers.parse_datetime( - state.attributes[const.ATTR_LAST_COLLECTION] + self.last_collection = ( + helpers.parse_datetime(state.attributes[const.ATTR_LAST_COLLECTION]) + if const.ATTR_LAST_COLLECTION in state.attributes + else None ) # Create device