Skip to content

Commit f564b8c

Browse files
authored
Remove unnecessary instanciating in Tuya find_dpcode (home-assistant#157473)
1 parent ce6bfde commit f564b8c

File tree

4 files changed

+34
-34
lines changed

4 files changed

+34
-34
lines changed

homeassistant/components/tuya/binary_sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ class _CustomDPCodeWrapper(DPCodeWrapper):
372372
_valid_values: set[bool | float | int | str]
373373

374374
def __init__(
375-
self, dpcode: DPCode, valid_values: set[bool | float | int | str]
375+
self, dpcode: str, valid_values: set[bool | float | int | str]
376376
) -> None:
377377
"""Init CustomDPCodeBooleanWrapper."""
378378
super().__init__(dpcode)
@@ -390,7 +390,7 @@ def _get_dpcode_wrapper(
390390
description: TuyaBinarySensorEntityDescription,
391391
) -> DPCodeWrapper | None:
392392
"""Get DPCode wrapper for an entity description."""
393-
dpcode = description.dpcode or DPCode(description.key)
393+
dpcode = description.dpcode or description.key
394394
if description.bitmap_key is not None:
395395
return DPCodeBitmapBitWrapper.find_dpcode(
396396
device, dpcode, bitmap_key=description.bitmap_key

homeassistant/components/tuya/humidifier.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def _has_a_valid_dpcode(
4949
device: CustomerDevice, description: TuyaHumidifierEntityDescription
5050
) -> bool:
5151
"""Check if the device has at least one valid DP code."""
52-
properties_to_check: list[DPCode | tuple[DPCode, ...] | None] = [
52+
properties_to_check: list[str | tuple[str, ...] | None] = [
5353
# Main control switch
54-
description.dpcode or DPCode(description.key),
54+
description.dpcode or description.key,
5555
# Other humidity properties
5656
description.current_humidity,
5757
description.humidity,
@@ -107,7 +107,7 @@ def async_discover_device(device_ids: list[str]) -> None:
107107
),
108108
switch_wrapper=DPCodeBooleanWrapper.find_dpcode(
109109
device,
110-
description.dpcode or DPCode(description.key),
110+
description.dpcode or description.key,
111111
prefer_function=True,
112112
),
113113
target_humidity_wrapper=_RoundedIntegerWrapper.find_dpcode(

homeassistant/components/tuya/models.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from homeassistant.util.json import json_loads, json_loads_object
1212

13-
from .const import LOGGER, DPCode, DPType
13+
from .const import LOGGER, DPType
1414
from .util import parse_dptype, remap_value
1515

1616
# Dictionary to track logged warnings to avoid spamming logs
@@ -39,11 +39,11 @@ class TypeInformation:
3939
As provided by the SDK, from `device.function` / `device.status_range`.
4040
"""
4141

42-
dpcode: DPCode
42+
dpcode: str
4343
type_data: str | None = None
4444

4545
@classmethod
46-
def from_json(cls, dpcode: DPCode, type_data: str) -> Self | None:
46+
def from_json(cls, dpcode: str, type_data: str) -> Self | None:
4747
"""Load JSON string and return a TypeInformation object."""
4848
return cls(dpcode=dpcode, type_data=type_data)
4949

@@ -102,7 +102,7 @@ def remap_value_from(
102102
return remap_value(value, from_min, from_max, self.min, self.max, reverse)
103103

104104
@classmethod
105-
def from_json(cls, dpcode: DPCode, type_data: str) -> Self | None:
105+
def from_json(cls, dpcode: str, type_data: str) -> Self | None:
106106
"""Load JSON string and return a IntegerTypeData object."""
107107
if not (parsed := cast(dict[str, Any] | None, json_loads_object(type_data))):
108108
return None
@@ -125,7 +125,7 @@ class BitmapTypeInformation(TypeInformation):
125125
label: list[str]
126126

127127
@classmethod
128-
def from_json(cls, dpcode: DPCode, type_data: str) -> Self | None:
128+
def from_json(cls, dpcode: str, type_data: str) -> Self | None:
129129
"""Load JSON string and return a BitmapTypeInformation object."""
130130
if not (parsed := json_loads_object(type_data)):
131131
return None
@@ -143,7 +143,7 @@ class EnumTypeData(TypeInformation):
143143
range: list[str]
144144

145145
@classmethod
146-
def from_json(cls, dpcode: DPCode, type_data: str) -> Self | None:
146+
def from_json(cls, dpcode: str, type_data: str) -> Self | None:
147147
"""Load JSON string and return a EnumTypeData object."""
148148
if not (parsed := json_loads_object(type_data)):
149149
return None
@@ -175,7 +175,7 @@ class DPCodeWrapper:
175175
native_unit: str | None = None
176176
suggested_unit: str | None = None
177177

178-
def __init__(self, dpcode: DPCode) -> None:
178+
def __init__(self, dpcode: str) -> None:
179179
"""Init DPCodeWrapper."""
180180
self.dpcode = dpcode
181181

@@ -222,7 +222,7 @@ class DPCodeTypeInformationWrapper[T: TypeInformation](DPCodeWrapper):
222222
DPTYPE: DPType
223223
type_information: T
224224

225-
def __init__(self, dpcode: DPCode, type_information: T) -> None:
225+
def __init__(self, dpcode: str, type_information: T) -> None:
226226
"""Init DPCodeWrapper."""
227227
super().__init__(dpcode)
228228
self.type_information = type_information
@@ -231,7 +231,7 @@ def __init__(self, dpcode: DPCode, type_information: T) -> None:
231231
def find_dpcode(
232232
cls,
233233
device: CustomerDevice,
234-
dpcodes: str | DPCode | tuple[DPCode, ...] | None,
234+
dpcodes: str | tuple[str, ...] | None,
235235
*,
236236
prefer_function: bool = False,
237237
) -> Self | None:
@@ -340,7 +340,7 @@ class DPCodeIntegerWrapper(DPCodeTypeInformationWrapper[IntegerTypeData]):
340340

341341
DPTYPE = DPType.INTEGER
342342

343-
def __init__(self, dpcode: DPCode, type_information: IntegerTypeData) -> None:
343+
def __init__(self, dpcode: str, type_information: IntegerTypeData) -> None:
344344
"""Init DPCodeIntegerWrapper."""
345345
super().__init__(dpcode, type_information)
346346
self.native_unit = type_information.unit
@@ -380,7 +380,7 @@ def read_device_status(self, device: CustomerDevice) -> str | None:
380380
class DPCodeBitmapBitWrapper(DPCodeWrapper):
381381
"""Simple wrapper for a specific bit in bitmap values."""
382382

383-
def __init__(self, dpcode: DPCode, mask: int) -> None:
383+
def __init__(self, dpcode: str, mask: int) -> None:
384384
"""Init DPCodeBitmapWrapper."""
385385
super().__init__(dpcode)
386386
self._mask = mask
@@ -395,7 +395,7 @@ def read_device_status(self, device: CustomerDevice) -> bool | None:
395395
def find_dpcode(
396396
cls,
397397
device: CustomerDevice,
398-
dpcodes: str | DPCode | tuple[DPCode, ...],
398+
dpcodes: str | tuple[str, ...],
399399
*,
400400
bitmap_key: str,
401401
) -> Self | None:
@@ -412,7 +412,7 @@ def find_dpcode(
412412
@overload
413413
def find_dpcode(
414414
device: CustomerDevice,
415-
dpcodes: str | DPCode | tuple[DPCode, ...] | None,
415+
dpcodes: str | tuple[str, ...] | None,
416416
*,
417417
prefer_function: bool = False,
418418
dptype: Literal[DPType.BITMAP],
@@ -422,7 +422,7 @@ def find_dpcode(
422422
@overload
423423
def find_dpcode(
424424
device: CustomerDevice,
425-
dpcodes: str | DPCode | tuple[DPCode, ...] | None,
425+
dpcodes: str | tuple[str, ...] | None,
426426
*,
427427
prefer_function: bool = False,
428428
dptype: Literal[DPType.ENUM],
@@ -432,7 +432,7 @@ def find_dpcode(
432432
@overload
433433
def find_dpcode(
434434
device: CustomerDevice,
435-
dpcodes: str | DPCode | tuple[DPCode, ...] | None,
435+
dpcodes: str | tuple[str, ...] | None,
436436
*,
437437
prefer_function: bool = False,
438438
dptype: Literal[DPType.INTEGER],
@@ -442,7 +442,7 @@ def find_dpcode(
442442
@overload
443443
def find_dpcode(
444444
device: CustomerDevice,
445-
dpcodes: str | DPCode | tuple[DPCode, ...] | None,
445+
dpcodes: str | tuple[str, ...] | None,
446446
*,
447447
prefer_function: bool = False,
448448
dptype: Literal[DPType.BOOLEAN, DPType.JSON, DPType.RAW],
@@ -451,7 +451,7 @@ def find_dpcode(
451451

452452
def find_dpcode(
453453
device: CustomerDevice,
454-
dpcodes: str | DPCode | tuple[DPCode, ...] | None,
454+
dpcodes: str | tuple[str, ...] | None,
455455
*,
456456
prefer_function: bool = False,
457457
dptype: DPType,
@@ -463,9 +463,7 @@ def find_dpcode(
463463
if dpcodes is None:
464464
return None
465465

466-
if isinstance(dpcodes, str):
467-
dpcodes = (DPCode(dpcodes),)
468-
elif not isinstance(dpcodes, tuple):
466+
if not isinstance(dpcodes, tuple):
469467
dpcodes = (dpcodes,)
470468

471469
lookup_tuple = (

homeassistant/components/tuya/util.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@
2020

2121

2222
def get_dpcode(
23-
device: CustomerDevice, dpcodes: str | DPCode | tuple[DPCode, ...] | None
24-
) -> DPCode | None:
23+
device: CustomerDevice, dpcodes: str | tuple[str, ...] | None
24+
) -> str | None:
2525
"""Get the first matching DPCode from the device or return None."""
2626
if dpcodes is None:
2727
return None
2828

29-
if isinstance(dpcodes, DPCode):
29+
if not isinstance(dpcodes, tuple):
3030
dpcodes = (dpcodes,)
31-
elif isinstance(dpcodes, str):
32-
dpcodes = (DPCode(dpcodes),)
3331

3432
for dpcode in dpcodes:
3533
if (
@@ -70,19 +68,23 @@ class ActionDPCodeNotFoundError(ServiceValidationError):
7068
"""Custom exception for action DP code not found errors."""
7169

7270
def __init__(
73-
self, device: CustomerDevice, expected: str | DPCode | tuple[DPCode, ...] | None
71+
self, device: CustomerDevice, expected: str | tuple[str, ...] | None
7472
) -> None:
7573
"""Initialize the error with device and expected DP codes."""
7674
if expected is None:
7775
expected = () # empty tuple for no expected codes
78-
elif isinstance(expected, str):
79-
expected = (DPCode(expected),)
76+
elif not isinstance(expected, tuple):
77+
expected = (expected,)
8078

8179
super().__init__(
8280
translation_domain=DOMAIN,
8381
translation_key="action_dpcode_not_found",
8482
translation_placeholders={
85-
"expected": str(sorted([dp.value for dp in expected])),
83+
"expected": str(
84+
sorted(
85+
[dp.value if isinstance(dp, DPCode) else dp for dp in expected]
86+
)
87+
),
8688
"available": str(sorted(device.function.keys())),
8789
},
8890
)

0 commit comments

Comments
 (0)