From 91b177fbc5833841b1fa2bb38183078effbd3350 Mon Sep 17 00:00:00 2001 From: RenierM26 <66512715+RenierM26@users.noreply.github.com> Date: Thu, 11 Aug 2022 19:44:01 +0200 Subject: [PATCH] Fix key error when notification returns empty list. --- pyhyypapi/alarm_info.py | 20 +++++++++++--------- pyhyypapi/client.py | 2 +- setup.py | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pyhyypapi/alarm_info.py b/pyhyypapi/alarm_info.py index d55b98a..4eac21b 100644 --- a/pyhyypapi/alarm_info.py +++ b/pyhyypapi/alarm_info.py @@ -31,15 +31,17 @@ def _last_notice(self, site_id: int) -> dict[Any, Any]: site_id=site_id, json_key=0 ) - _last_event = _last_notification["eventNumber"] - _last_event_datetime = str( - datetime.fromtimestamp(_last_notification["timestamp"] / 1000) - ) # Epoch in ms - - _response = { - "lastNoticeTime": _last_event_datetime, - "lastNoticeName": EventNumber[str(_last_event)], - } + if _last_notification: + + _last_event = _last_notification["eventNumber"] + _last_event_datetime = str( + datetime.fromtimestamp(_last_notification["timestamp"] / 1000) + ) # Epoch in ms + + _response = { + "lastNoticeTime": _last_event_datetime, + "lastNoticeName": EventNumber[str(_last_event)], + } return _response diff --git a/pyhyypapi/client.py b/pyhyypapi/client.py index 066c3fa..d3c407c 100644 --- a/pyhyypapi/client.py +++ b/pyhyypapi/client.py @@ -178,7 +178,7 @@ def site_notifications( f"Error getting site notifications from api: {_json_result['error']}" ) - if json_key is None: + if not json_key or _json_result["listSiteNotifications"][str(site_id)] is None: return _json_result["listSiteNotifications"][str(site_id)] return _json_result["listSiteNotifications"][str(site_id)][json_key] diff --git a/setup.py b/setup.py index 8409ce5..9adf4a4 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name='pyhyypapi', - version="0.0.0.5", + version="0.0.0.6", license='Apache Software License 2.0', author='Renier Moorcroft', author_email='renierm26@users.github.com',