From 161b378c28da899f77060b339b79bb44ef6b5855 Mon Sep 17 00:00:00 2001 From: Jason Verhagen Date: Sun, 29 Mar 2020 16:13:09 -0500 Subject: [PATCH] Fix ordering with simultaneous alerts Sometimes more than one alert is issued with the same 'sent' date/time. The alert json feed randomly changes the ordering of these alerts in the feed and can result in re-triggering of notifications. The alerts array is now being reverse sorted by 'sent' time and 'id' to keep them consistent when the json feed shuffles them around. --- custom_components/weatheralerts/sensor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/weatheralerts/sensor.py b/custom_components/weatheralerts/sensor.py index 6db5ce7..d82445a 100644 --- a/custom_components/weatheralerts/sensor.py +++ b/custom_components/weatheralerts/sensor.py @@ -16,7 +16,7 @@ from homeassistant.helpers.entity import Entity import homeassistant.helpers.config_validation as cv -__version__ = '0.1.0' +__version__ = '0.1.1' CONF_STATE = "state" CONF_ZONE = "zone" @@ -180,6 +180,7 @@ async def async_update(self): "zoneid": self.feedid, } ) + alerts.sort(key=lambda x: (x['sent'], x['id']), reverse=True) self._state = len(alerts) self._attr = {