Skip to content

Commit 5fce08d

Browse files
authored
Remove getattr in Tuya find_dpcode function (home-assistant#155941)
1 parent c0db966 commit 5fce08d

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

homeassistant/components/tuya/models.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,33 +53,31 @@ def find_dpcode(
5353
elif not isinstance(dpcodes, tuple):
5454
dpcodes = (dpcodes,)
5555

56-
order = ["status_range", "function"]
57-
if prefer_function:
58-
order = ["function", "status_range"]
56+
lookup_tuple = (
57+
(device.function, device.status_range)
58+
if prefer_function
59+
else (device.status_range, device.function)
60+
)
5961

6062
for dpcode in dpcodes:
61-
for key in order:
62-
if dpcode not in getattr(device, key):
63-
continue
64-
if (
65-
dptype == DPType.ENUM
66-
and getattr(device, key)[dpcode].type == DPType.ENUM
63+
for device_specs in lookup_tuple:
64+
if not (
65+
(current_definition := device_specs.get(dpcode))
66+
and current_definition.type == dptype
6767
):
68+
continue
69+
if dptype is DPType.ENUM:
6870
if not (
6971
enum_type := EnumTypeData.from_json(
70-
dpcode, getattr(device, key)[dpcode].values
72+
dpcode, current_definition.values
7173
)
7274
):
7375
continue
7476
return enum_type
75-
76-
if (
77-
dptype == DPType.INTEGER
78-
and getattr(device, key)[dpcode].type == DPType.INTEGER
79-
):
77+
if dptype is DPType.INTEGER:
8078
if not (
8179
integer_type := IntegerTypeData.from_json(
82-
dpcode, getattr(device, key)[dpcode].values
80+
dpcode, current_definition.values
8381
)
8482
):
8583
continue

0 commit comments

Comments
 (0)