Move redundant attribute and key error handling to event parser caller (#140630)

This commit is contained in:
Jeff Terrace 2025-03-14 20:16:35 -04:00 committed by GitHub
parent 537302ce56
commit 11e15b1405
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 420 additions and 498 deletions

View File

@ -174,11 +174,20 @@ class EventManager:
UNHANDLED_TOPICS.add(topic) UNHANDLED_TOPICS.add(topic)
continue continue
event = await parser(unique_id, msg) try:
event = await parser(unique_id, msg)
error = None
except (AttributeError, KeyError) as e:
event = None
error = e
if not event: if not event:
LOGGER.warning( LOGGER.warning(
"%s: Unable to parse event from %s: %s", self.name, unique_id, msg "%s: Unable to parse event from %s: %s: %s",
self.name,
unique_id,
error,
msg,
) )
return return

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@ import os
import onvif import onvif
import onvif.settings import onvif.settings
import pytest
from zeep import Client from zeep import Client
from zeep.transports import Transport from zeep.transports import Transport
@ -732,25 +733,24 @@ async def test_tapo_intrusion(hass: HomeAssistant) -> None:
async def test_tapo_missing_attributes(hass: HomeAssistant) -> None: async def test_tapo_missing_attributes(hass: HomeAssistant) -> None:
"""Tests async_parse_tplink_detector with missing fields.""" """Tests async_parse_tplink_detector with missing fields."""
event = await get_event( with pytest.raises(AttributeError, match="SimpleItem"):
{ await get_event(
"Message": { {
"_value_1": { "Message": {
"Data": { "_value_1": {
"ElementItem": [], "Data": {
"Extension": None, "ElementItem": [],
"SimpleItem": [{"Name": "IsPeople", "Value": "true"}], "Extension": None,
"_attr_1": None, "SimpleItem": [{"Name": "IsPeople", "Value": "true"}],
}, "_attr_1": None,
} },
}, }
"Topic": { },
"_value_1": "tns1:RuleEngine/PeopleDetector/People", "Topic": {
}, "_value_1": "tns1:RuleEngine/PeopleDetector/People",
} },
) }
)
assert event is None
async def test_tapo_unknown_type(hass: HomeAssistant) -> None: async def test_tapo_unknown_type(hass: HomeAssistant) -> None: