Don't assume that the activity/sleep labels are always present in Tractive event (#99197)

* Don't assume that the activity_label and sleep_labes are always present in an event

* Catch KeyError
This commit is contained in:
Maciej Bieniek 2023-08-29 15:52:29 +00:00 committed by GitHub
parent b9fd2ee3b6
commit fe713cec8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -240,7 +240,9 @@ class TractiveClient:
self._config_entry.data[CONF_EMAIL],
)
return
except KeyError as error:
_LOGGER.error("Error while listening for events: %s", error)
continue
except aiotractive.exceptions.TractiveError:
_LOGGER.debug(
(
@ -283,12 +285,12 @@ class TractiveClient:
def _send_wellness_update(self, event: dict[str, Any]) -> None:
payload = {
ATTR_ACTIVITY_LABEL: event["wellness"]["activity_label"],
ATTR_ACTIVITY_LABEL: event["wellness"].get("activity_label"),
ATTR_CALORIES: event["activity"]["calories"],
ATTR_MINUTES_DAY_SLEEP: event["sleep"]["minutes_day_sleep"],
ATTR_MINUTES_NIGHT_SLEEP: event["sleep"]["minutes_night_sleep"],
ATTR_MINUTES_REST: event["activity"]["minutes_rest"],
ATTR_SLEEP_LABEL: event["wellness"]["sleep_label"],
ATTR_SLEEP_LABEL: event["wellness"].get("sleep_label"),
}
self._dispatch_tracker_event(
TRACKER_WELLNESS_STATUS_UPDATED, event["pet_id"], payload