Skip to content

Commit ac5316e

Browse files
authored
Remove duplicate code in tuya find_dpcode (home-assistant#156019)
1 parent 2a2599d commit ac5316e

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

homeassistant/components/tuya/models.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ def find_dpcode(
110110
*,
111111
prefer_function: bool = False,
112112
dptype: DPType,
113-
) -> EnumTypeData | IntegerTypeData | None:
113+
) -> TypeInformation | None:
114114
"""Find type information for a matching DP code available for this device."""
115-
if dptype not in (DPType.ENUM, DPType.INTEGER):
116-
raise NotImplementedError("Only ENUM and INTEGER types are supported")
115+
if not (type_information_cls := _TYPE_INFORMATION_MAPPINGS.get(dptype)):
116+
raise NotImplementedError(f"find_dpcode not supported for {dptype}")
117117

118118
if dpcodes is None:
119119
return None
@@ -131,33 +131,35 @@ def find_dpcode(
131131

132132
for dpcode in dpcodes:
133133
for device_specs in lookup_tuple:
134-
if not (
134+
if (
135135
(current_definition := device_specs.get(dpcode))
136136
and current_definition.type == dptype
137-
):
138-
continue
139-
if dptype is DPType.ENUM:
140-
if not (
141-
enum_type := EnumTypeData.from_json(
142-
dpcode, current_definition.values
143-
)
144-
):
145-
continue
146-
return enum_type
147-
if dptype is DPType.INTEGER:
148-
if not (
149-
integer_type := IntegerTypeData.from_json(
137+
and (
138+
type_information := type_information_cls.from_json(
150139
dpcode, current_definition.values
151140
)
152-
):
153-
continue
154-
return integer_type
141+
)
142+
):
143+
return type_information
155144

156145
return None
157146

158147

159148
@dataclass
160-
class IntegerTypeData:
149+
class TypeInformation:
150+
"""Type information.
151+
152+
As provided by the SDK, from `device.function` / `device.status_range`.
153+
"""
154+
155+
@classmethod
156+
def from_json(cls, dpcode: DPCode, data: str) -> Self | None:
157+
"""Load JSON string and return a TypeInformation object."""
158+
raise NotImplementedError("from_json is not implemented for this type")
159+
160+
161+
@dataclass
162+
class IntegerTypeData(TypeInformation):
161163
"""Integer Type Data."""
162164

163165
dpcode: DPCode
@@ -229,7 +231,7 @@ def from_json(cls, dpcode: DPCode, data: str) -> IntegerTypeData | None:
229231

230232

231233
@dataclass
232-
class EnumTypeData:
234+
class EnumTypeData(TypeInformation):
233235
"""Enum Type Data."""
234236

235237
dpcode: DPCode
@@ -243,6 +245,12 @@ def from_json(cls, dpcode: DPCode, data: str) -> EnumTypeData | None:
243245
return cls(dpcode, **parsed)
244246

245247

248+
_TYPE_INFORMATION_MAPPINGS: dict[DPType, type[TypeInformation]] = {
249+
DPType.ENUM: EnumTypeData,
250+
DPType.INTEGER: IntegerTypeData,
251+
}
252+
253+
246254
class ComplexValue:
247255
"""Complex value (for JSON/RAW parsing)."""
248256

0 commit comments

Comments
 (0)