Skip to content

Commit 1bf713f

Browse files
epenetzweckj
andauthored
Set kw_only in Tuya TypeInformation (home-assistant#156804)
Co-authored-by: Josef Zweck <[email protected]>
1 parent 10c8ee4 commit 1bf713f

File tree

3 files changed

+59
-31
lines changed

3 files changed

+59
-31
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: str, valid_values: set[bool | float | int | str]
375+
self, dpcode: DPCode, 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 description.key
393+
dpcode = description.dpcode or DPCode(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/light.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,27 @@ class ColorTypeData:
146146

147147

148148
DEFAULT_COLOR_TYPE_DATA = ColorTypeData(
149-
h_type=IntegerTypeData(DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=360, step=1),
150-
s_type=IntegerTypeData(DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=255, step=1),
151-
v_type=IntegerTypeData(DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=255, step=1),
149+
h_type=IntegerTypeData(
150+
dpcode=DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=360, step=1
151+
),
152+
s_type=IntegerTypeData(
153+
dpcode=DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=255, step=1
154+
),
155+
v_type=IntegerTypeData(
156+
dpcode=DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=255, step=1
157+
),
152158
)
153159

154160
DEFAULT_COLOR_TYPE_DATA_V2 = ColorTypeData(
155-
h_type=IntegerTypeData(DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=360, step=1),
156-
s_type=IntegerTypeData(DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=1000, step=1),
157-
v_type=IntegerTypeData(DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=1000, step=1),
161+
h_type=IntegerTypeData(
162+
dpcode=DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=360, step=1
163+
),
164+
s_type=IntegerTypeData(
165+
dpcode=DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=1000, step=1
166+
),
167+
v_type=IntegerTypeData(
168+
dpcode=DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=1000, step=1
169+
),
158170
)
159171

160172
MAX_MIREDS = 500 # 2000 K
@@ -629,9 +641,15 @@ def __init__(
629641
# Fetch color data type information
630642
if function_data := json_loads_object(values):
631643
self._color_data_type = ColorTypeData(
632-
h_type=IntegerTypeData(dpcode, **cast(dict, function_data["h"])),
633-
s_type=IntegerTypeData(dpcode, **cast(dict, function_data["s"])),
634-
v_type=IntegerTypeData(dpcode, **cast(dict, function_data["v"])),
644+
h_type=IntegerTypeData(
645+
dpcode=dpcode, **cast(dict, function_data["h"])
646+
),
647+
s_type=IntegerTypeData(
648+
dpcode=dpcode, **cast(dict, function_data["s"])
649+
),
650+
v_type=IntegerTypeData(
651+
dpcode=dpcode, **cast(dict, function_data["v"])
652+
),
635653
)
636654
else:
637655
# If no type is found, use a default one

homeassistant/components/tuya/models.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,23 @@
1515
from .util import parse_dptype, remap_value
1616

1717

18-
@dataclass
18+
@dataclass(kw_only=True)
1919
class TypeInformation:
2020
"""Type information.
2121
2222
As provided by the SDK, from `device.function` / `device.status_range`.
2323
"""
2424

2525
dpcode: DPCode
26+
type_data: str | None = None
2627

2728
@classmethod
28-
def from_json(cls, dpcode: DPCode, data: str) -> Self | None:
29+
def from_json(cls, dpcode: DPCode, type_data: str) -> Self | None:
2930
"""Load JSON string and return a TypeInformation object."""
30-
return cls(dpcode)
31+
return cls(dpcode=dpcode, type_data=type_data)
3132

3233

33-
@dataclass
34+
@dataclass(kw_only=True)
3435
class IntegerTypeData(TypeInformation):
3536
"""Integer Type Data."""
3637

@@ -84,13 +85,14 @@ def remap_value_from(
8485
return remap_value(value, from_min, from_max, self.min, self.max, reverse)
8586

8687
@classmethod
87-
def from_json(cls, dpcode: DPCode, data: str) -> Self | None:
88+
def from_json(cls, dpcode: DPCode, type_data: str) -> Self | None:
8889
"""Load JSON string and return a IntegerTypeData object."""
89-
if not (parsed := cast(dict[str, Any] | None, json_loads_object(data))):
90+
if not (parsed := cast(dict[str, Any] | None, json_loads_object(type_data))):
9091
return None
9192

9293
return cls(
93-
dpcode,
94+
dpcode=dpcode,
95+
type_data=type_data,
9496
min=int(parsed["min"]),
9597
max=int(parsed["max"]),
9698
scale=int(parsed["scale"]),
@@ -99,32 +101,40 @@ def from_json(cls, dpcode: DPCode, data: str) -> Self | None:
99101
)
100102

101103

102-
@dataclass
104+
@dataclass(kw_only=True)
103105
class BitmapTypeInformation(TypeInformation):
104106
"""Bitmap type information."""
105107

106108
label: list[str]
107109

108110
@classmethod
109-
def from_json(cls, dpcode: DPCode, data: str) -> Self | None:
111+
def from_json(cls, dpcode: DPCode, type_data: str) -> Self | None:
110112
"""Load JSON string and return a BitmapTypeInformation object."""
111-
if not (parsed := json_loads_object(data)):
113+
if not (parsed := json_loads_object(type_data)):
112114
return None
113-
return cls(dpcode, **cast(dict[str, list[str]], parsed))
115+
return cls(
116+
dpcode=dpcode,
117+
type_data=type_data,
118+
**cast(dict[str, list[str]], parsed),
119+
)
114120

115121

116-
@dataclass
122+
@dataclass(kw_only=True)
117123
class EnumTypeData(TypeInformation):
118124
"""Enum Type Data."""
119125

120126
range: list[str]
121127

122128
@classmethod
123-
def from_json(cls, dpcode: DPCode, data: str) -> Self | None:
129+
def from_json(cls, dpcode: DPCode, type_data: str) -> Self | None:
124130
"""Load JSON string and return a EnumTypeData object."""
125-
if not (parsed := json_loads_object(data)):
131+
if not (parsed := json_loads_object(type_data)):
126132
return None
127-
return cls(dpcode, **cast(dict[str, list[str]], parsed))
133+
return cls(
134+
dpcode=dpcode,
135+
type_data=type_data,
136+
**cast(dict[str, list[str]], parsed),
137+
)
128138

129139

130140
_TYPE_INFORMATION_MAPPINGS: dict[DPType, type[TypeInformation]] = {
@@ -147,7 +157,7 @@ class DPCodeWrapper(ABC):
147157
native_unit: str | None = None
148158
suggested_unit: str | None = None
149159

150-
def __init__(self, dpcode: str) -> None:
160+
def __init__(self, dpcode: DPCode) -> None:
151161
"""Init DPCodeWrapper."""
152162
self.dpcode = dpcode
153163

@@ -190,7 +200,7 @@ class DPCodeTypeInformationWrapper[T: TypeInformation](DPCodeWrapper):
190200
DPTYPE: DPType
191201
type_information: T
192202

193-
def __init__(self, dpcode: str, type_information: T) -> None:
203+
def __init__(self, dpcode: DPCode, type_information: T) -> None:
194204
"""Init DPCodeWrapper."""
195205
super().__init__(dpcode)
196206
self.type_information = type_information
@@ -297,7 +307,7 @@ class DPCodeIntegerWrapper(DPCodeTypeInformationWrapper[IntegerTypeData]):
297307

298308
DPTYPE = DPType.INTEGER
299309

300-
def __init__(self, dpcode: str, type_information: IntegerTypeData) -> None:
310+
def __init__(self, dpcode: DPCode, type_information: IntegerTypeData) -> None:
301311
"""Init DPCodeIntegerWrapper."""
302312
super().__init__(dpcode, type_information)
303313
self.native_unit = type_information.unit
@@ -327,7 +337,7 @@ def _convert_value_to_raw_value(self, device: CustomerDevice, value: Any) -> Any
327337
class DPCodeBitmapBitWrapper(DPCodeWrapper):
328338
"""Simple wrapper for a specific bit in bitmap values."""
329339

330-
def __init__(self, dpcode: str, mask: int) -> None:
340+
def __init__(self, dpcode: DPCode, mask: int) -> None:
331341
"""Init DPCodeBitmapWrapper."""
332342
super().__init__(dpcode)
333343
self._mask = mask
@@ -428,7 +438,7 @@ def find_dpcode(
428438
and parse_dptype(current_definition.type) is dptype
429439
and (
430440
type_information := type_information_cls.from_json(
431-
dpcode, current_definition.values
441+
dpcode=dpcode, type_data=current_definition.values
432442
)
433443
)
434444
):

0 commit comments

Comments
 (0)