Switch lambda to attrgetter in zha (#99660)

This commit is contained in:
J. Nick Koston 2023-09-05 15:45:45 -05:00 committed by GitHub
parent a1359c1ce3
commit b69cc29a78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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