From 7a40ae13a4012b6518d80bcc8d7650db988354ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Fri, 11 Feb 2022 14:57:45 +0100 Subject: [PATCH] Add guard for invalid EntityCategory value (#66316) --- homeassistant/helpers/entity.py | 5 ++++- tests/helpers/test_entity_registry.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index b670c734b47..e9038d1f658 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -223,7 +223,10 @@ def convert_to_entity_category( "EntityCategory instead" % (type(value).__name__, value), error_if_core=False, ) - return EntityCategory(value) + try: + return EntityCategory(value) + except ValueError: + return None return value diff --git a/tests/helpers/test_entity_registry.py b/tests/helpers/test_entity_registry.py index 5f49889b6c6..714ac037e2a 100644 --- a/tests/helpers/test_entity_registry.py +++ b/tests/helpers/test_entity_registry.py @@ -1138,3 +1138,15 @@ async def test_deprecated_entity_category_str(hass, registry, caplog): assert entry.entity_category is EntityCategory.DIAGNOSTIC assert " should be updated to use EntityCategory" in caplog.text + + +async def test_invalid_entity_category_str(hass, registry, caplog): + """Test use of invalid entity category.""" + entry = er.RegistryEntry( + "light", + "hue", + "5678", + entity_category="invalid", + ) + + assert entry.entity_category is None