From b69cc29a78714ef465c4aefb97ca08e011cce8fb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 5 Sep 2023 15:45:45 -0500 Subject: [PATCH] Switch lambda to attrgetter in zha (#99660) --- homeassistant/components/zha/core/registries.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/zha/core/registries.py b/homeassistant/components/zha/core/registries.py index 03fdc7e37c1..713d10ddf70 100644 --- a/homeassistant/components/zha/core/registries.py +++ b/homeassistant/components/zha/core/registries.py @@ -4,6 +4,7 @@ from __future__ import annotations import collections from collections.abc import Callable import dataclasses +from operator import attrgetter from typing import TYPE_CHECKING, TypeVar import attr @@ -111,6 +112,8 @@ CLIENT_CLUSTER_HANDLER_REGISTRY: DictRegistry[ ] = DictRegistry() ZIGBEE_CLUSTER_HANDLER_REGISTRY: DictRegistry[type[ClusterHandler]] = DictRegistry() +WEIGHT_ATTR = attrgetter("weight") + def set_or_callable(value) -> frozenset[str] | Callable: """Convert single str or None to a set. Pass through callables and sets.""" @@ -294,7 +297,7 @@ class ZHAEntityRegistry: ) -> tuple[type[ZhaEntity] | None, list[ClusterHandler]]: """Match a ZHA ClusterHandler to a ZHA Entity class.""" matches = self._strict_registry[component] - for match in sorted(matches, key=lambda x: x.weight, reverse=True): + for match in sorted(matches, key=WEIGHT_ATTR, reverse=True): if match.strict_matched(manufacturer, model, cluster_handlers, quirk_class): claimed = match.claim_cluster_handlers(cluster_handlers) return self._strict_registry[component][match], claimed @@ -315,7 +318,7 @@ class ZHAEntityRegistry: all_claimed: set[ClusterHandler] = set() for component, stop_match_groups in self._multi_entity_registry.items(): for stop_match_grp, matches in stop_match_groups.items(): - sorted_matches = sorted(matches, key=lambda x: x.weight, reverse=True) + sorted_matches = sorted(matches, key=WEIGHT_ATTR, reverse=True) for match in sorted_matches: if match.strict_matched( manufacturer, model, cluster_handlers, quirk_class @@ -349,7 +352,7 @@ class ZHAEntityRegistry: stop_match_groups, ) in self._config_diagnostic_entity_registry.items(): for stop_match_grp, matches in stop_match_groups.items(): - sorted_matches = sorted(matches, key=lambda x: x.weight, reverse=True) + sorted_matches = sorted(matches, key=WEIGHT_ATTR, reverse=True) for match in sorted_matches: if match.strict_matched( manufacturer, model, cluster_handlers, quirk_class