diff --git a/homeassistant/components/hue/hue_event.py b/homeassistant/components/hue/hue_event.py index 6bd68b106bb..069bb1e58b5 100644 --- a/homeassistant/components/hue/hue_event.py +++ b/homeassistant/components/hue/hue_event.py @@ -60,10 +60,17 @@ class HueEvent(GenericHueDevice): self.sensor.last_event is not None and self.sensor.last_event["type"] != EVENT_BUTTON ) - or - # Filter out old states. Can happen when events fire while refreshing - dt_util.parse_datetime(self.sensor.state["lastupdated"]) - <= dt_util.parse_datetime(self._last_state["lastupdated"]) + ): + return + + # Filter out old states. Can happen when events fire while refreshing + now_updated = dt_util.parse_datetime(self.sensor.state["lastupdated"]) + last_updated = dt_util.parse_datetime(self._last_state["lastupdated"]) + + if ( + now_updated is not None + and last_updated is not None + and now_updated <= last_updated ): return diff --git a/tests/components/hue/test_device_trigger.py b/tests/components/hue/test_device_trigger.py index 28bb989d475..d0c20018c30 100644 --- a/tests/components/hue/test_device_trigger.py +++ b/tests/components/hue/test_device_trigger.py @@ -55,7 +55,7 @@ async def test_get_triggers(hass, mock_bridge, device_reg): "type": t_type, "subtype": t_subtype, } - for t_type, t_subtype in device_trigger.HUE_TAP_REMOTE.keys() + for t_type, t_subtype in device_trigger.HUE_TAP_REMOTE ] assert_lists_same(triggers, expected_triggers) @@ -82,7 +82,7 @@ async def test_get_triggers(hass, mock_bridge, device_reg): "type": t_type, "subtype": t_subtype, } - for t_type, t_subtype in device_trigger.HUE_DIMMER_REMOTE.keys() + for t_type, t_subtype in device_trigger.HUE_DIMMER_REMOTE ), ] assert_lists_same(triggers, expected_triggers) @@ -140,6 +140,7 @@ async def test_if_fires_on_state_change(hass, mock_bridge, device_reg, calls): # Fake that the remote is being pressed. new_sensor_response = dict(REMOTES_RESPONSE) + new_sensor_response["7"] = dict(new_sensor_response["7"]) new_sensor_response["7"]["state"] = { "buttonevent": 18, "lastupdated": "2019-12-28T22:58:02", @@ -156,7 +157,7 @@ async def test_if_fires_on_state_change(hass, mock_bridge, device_reg, calls): assert calls[0].data["some"] == "B4 - 18" # Fake another button press. - new_sensor_response = dict(REMOTES_RESPONSE) + new_sensor_response["7"] = dict(new_sensor_response["7"]) new_sensor_response["7"]["state"] = { "buttonevent": 34, "lastupdated": "2019-12-28T22:58:05", diff --git a/tests/components/hue/test_sensor_base.py b/tests/components/hue/test_sensor_base.py index b8e9c83e47d..5b3b6619efe 100644 --- a/tests/components/hue/test_sensor_base.py +++ b/tests/components/hue/test_sensor_base.py @@ -450,6 +450,7 @@ async def test_hue_events(hass, mock_bridge): mock_bridge.api.sensors["8"].last_event = {"type": "button"} new_sensor_response = dict(SENSOR_RESPONSE) + new_sensor_response["7"] = dict(new_sensor_response["7"]) new_sensor_response["7"]["state"] = { "buttonevent": 18, "lastupdated": "2019-12-28T22:58:03", @@ -473,6 +474,7 @@ async def test_hue_events(hass, mock_bridge): } new_sensor_response = dict(new_sensor_response) + new_sensor_response["8"] = dict(new_sensor_response["8"]) new_sensor_response["8"]["state"] = { "buttonevent": 3002, "lastupdated": "2019-12-28T22:58:03", @@ -497,6 +499,7 @@ async def test_hue_events(hass, mock_bridge): # Fire old event, it should be ignored new_sensor_response = dict(new_sensor_response) + new_sensor_response["8"] = dict(new_sensor_response["8"]) new_sensor_response["8"]["state"] = { "buttonevent": 18, "lastupdated": "2019-12-28T22:58:02",