Skip to content

Commit 88b373a

Browse files
authored
Migrate Tuya climate (swing) to use wrapper class (home-assistant#156938)
1 parent dea2f37 commit 88b373a

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

homeassistant/components/tuya/climate.py

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
IntegerTypeData,
3434
find_dpcode,
3535
)
36-
from .util import get_dpcode
3736

3837
TUYA_HVAC_TO_HA = {
3938
"auto": HVACMode.HEAT_COOL,
@@ -124,6 +123,15 @@ def async_discover_device(device_ids: list[str]) -> None:
124123
hvac_mode_wrapper=DPCodeEnumWrapper.find_dpcode(
125124
device, DPCode.MODE, prefer_function=True
126125
),
126+
swing_wrapper=DPCodeBooleanWrapper.find_dpcode(
127+
device, (DPCode.SWING, DPCode.SHAKE), prefer_function=True
128+
),
129+
swing_h_wrapper=DPCodeBooleanWrapper.find_dpcode(
130+
device, DPCode.SWITCH_HORIZONTAL, prefer_function=True
131+
),
132+
swing_v_wrapper=DPCodeBooleanWrapper.find_dpcode(
133+
device, DPCode.SWITCH_VERTICAL, prefer_function=True
134+
),
127135
switch_wrapper=DPCodeBooleanWrapper.find_dpcode(
128136
device, DPCode.SWITCH, prefer_function=True
129137
),
@@ -160,6 +168,9 @@ def __init__(
160168
current_humidity_wrapper: _RoundedIntegerWrapper | None,
161169
fan_mode_wrapper: DPCodeEnumWrapper | None,
162170
hvac_mode_wrapper: DPCodeEnumWrapper | None,
171+
swing_wrapper: DPCodeBooleanWrapper | None,
172+
swing_h_wrapper: DPCodeBooleanWrapper | None,
173+
swing_v_wrapper: DPCodeBooleanWrapper | None,
163174
switch_wrapper: DPCodeBooleanWrapper | None,
164175
target_humidity_wrapper: _RoundedIntegerWrapper | None,
165176
) -> None:
@@ -171,6 +182,9 @@ def __init__(
171182
self._current_humidity_wrapper = current_humidity_wrapper
172183
self._fan_mode_wrapper = fan_mode_wrapper
173184
self._hvac_mode_wrapper = hvac_mode_wrapper
185+
self._swing_wrapper = swing_wrapper
186+
self._swing_h_wrapper = swing_h_wrapper
187+
self._swing_v_wrapper = swing_v_wrapper
174188
self._switch_wrapper = switch_wrapper
175189
self._target_humidity_wrapper = target_humidity_wrapper
176190

@@ -282,24 +296,16 @@ def __init__(
282296
self._attr_fan_modes = fan_mode_wrapper.type_information.range
283297

284298
# Determine swing modes
285-
if get_dpcode(
286-
self.device,
287-
(
288-
DPCode.SHAKE,
289-
DPCode.SWING,
290-
DPCode.SWITCH_HORIZONTAL,
291-
DPCode.SWITCH_VERTICAL,
292-
),
293-
):
299+
if swing_wrapper or swing_h_wrapper or swing_v_wrapper:
294300
self._attr_supported_features |= ClimateEntityFeature.SWING_MODE
295301
self._attr_swing_modes = [SWING_OFF]
296-
if get_dpcode(self.device, (DPCode.SHAKE, DPCode.SWING)):
302+
if swing_wrapper:
297303
self._attr_swing_modes.append(SWING_ON)
298304

299-
if get_dpcode(self.device, DPCode.SWITCH_HORIZONTAL):
305+
if swing_h_wrapper:
300306
self._attr_swing_modes.append(SWING_HORIZONTAL)
301307

302-
if get_dpcode(self.device, DPCode.SWITCH_VERTICAL):
308+
if swing_v_wrapper:
303309
self._attr_swing_modes.append(SWING_VERTICAL)
304310

305311
if switch_wrapper:
@@ -336,30 +342,29 @@ async def async_set_humidity(self, humidity: int) -> None:
336342
"""Set new target humidity."""
337343
await self._async_send_dpcode_update(self._target_humidity_wrapper, humidity)
338344

339-
def set_swing_mode(self, swing_mode: str) -> None:
345+
async def async_set_swing_mode(self, swing_mode: str) -> None:
340346
"""Set new target swing operation."""
341-
# The API accepts these all at once and will ignore the codes
342-
# that don't apply to the device being controlled.
343-
self._send_command(
344-
[
345-
{
346-
"code": DPCode.SHAKE,
347-
"value": swing_mode == SWING_ON,
348-
},
349-
{
350-
"code": DPCode.SWING,
351-
"value": swing_mode == SWING_ON,
352-
},
353-
{
354-
"code": DPCode.SWITCH_VERTICAL,
355-
"value": swing_mode in (SWING_BOTH, SWING_VERTICAL),
356-
},
357-
{
358-
"code": DPCode.SWITCH_HORIZONTAL,
359-
"value": swing_mode in (SWING_BOTH, SWING_HORIZONTAL),
360-
},
361-
]
362-
)
347+
commands = []
348+
if self._swing_wrapper:
349+
commands.append(
350+
self._swing_wrapper.get_update_command(
351+
self.device, swing_mode == SWING_ON
352+
)
353+
)
354+
if self._swing_v_wrapper:
355+
commands.append(
356+
self._swing_v_wrapper.get_update_command(
357+
self.device, swing_mode in (SWING_BOTH, SWING_VERTICAL)
358+
)
359+
)
360+
if self._swing_h_wrapper:
361+
commands.append(
362+
self._swing_h_wrapper.get_update_command(
363+
self.device, swing_mode in (SWING_BOTH, SWING_HORIZONTAL)
364+
)
365+
)
366+
if commands:
367+
await self._async_send_commands(commands)
363368

364369
def set_temperature(self, **kwargs: Any) -> None:
365370
"""Set new target temperature."""
@@ -458,13 +463,11 @@ def fan_mode(self) -> str | None:
458463
@property
459464
def swing_mode(self) -> str:
460465
"""Return swing mode."""
461-
if any(
462-
self.device.status.get(dpcode) for dpcode in (DPCode.SHAKE, DPCode.SWING)
463-
):
466+
if self._read_wrapper(self._swing_wrapper):
464467
return SWING_ON
465468

466-
horizontal = self.device.status.get(DPCode.SWITCH_HORIZONTAL)
467-
vertical = self.device.status.get(DPCode.SWITCH_VERTICAL)
469+
horizontal = self._read_wrapper(self._swing_h_wrapper)
470+
vertical = self._read_wrapper(self._swing_v_wrapper)
468471
if horizontal and vertical:
469472
return SWING_BOTH
470473
if horizontal:

0 commit comments

Comments
 (0)