From e71f6c5948d9cca0ae2598adb434d9d48cd5d4aa Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 5 Jul 2024 01:57:08 -0500 Subject: [PATCH] Small speedup to processing entity customize (#121271) --- homeassistant/helpers/entity.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index f730223ab8f..dbc1a036ef6 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -1185,11 +1185,18 @@ class Entity( report_issue, ) - # Overwrite properties that have been set in the config file. - if (customize := hass.data.get(DATA_CUSTOMIZE)) and ( - custom := customize.get(entity_id) - ): - attr.update(custom) + try: + # Most of the time this will already be + # set and since try is near zero cost + # on py3.11+ its faster to assume it is + # set and catch the exception if it is not. + customize = hass.data[DATA_CUSTOMIZE] + except KeyError: + pass + else: + # Overwrite properties that have been set in the config file. + if custom := customize.get(entity_id): + attr.update(custom) if ( self._context_set is not None