Skip to content

Commit ff91c57

Browse files
authored
Adjust Tuya wrapper to return a command list (home-assistant#157622)
1 parent 3d2b506 commit ff91c57

File tree

6 files changed

+45
-45
lines changed

6 files changed

+45
-45
lines changed

homeassistant/components/tuya/climate.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,14 @@ async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
345345
"""Set new target hvac mode."""
346346
commands = []
347347
if self._switch_wrapper:
348-
commands.append(
349-
self._switch_wrapper.get_update_command(
348+
commands.extend(
349+
self._switch_wrapper.get_update_commands(
350350
self.device, hvac_mode != HVACMode.OFF
351351
)
352352
)
353353
if self._hvac_mode_wrapper and hvac_mode in self._hvac_to_tuya:
354-
commands.append(
355-
self._hvac_mode_wrapper.get_update_command(
354+
commands.extend(
355+
self._hvac_mode_wrapper.get_update_commands(
356356
self.device, self._hvac_to_tuya[hvac_mode]
357357
)
358358
)
@@ -374,20 +374,20 @@ async def async_set_swing_mode(self, swing_mode: str) -> None:
374374
"""Set new target swing operation."""
375375
commands = []
376376
if self._swing_wrapper:
377-
commands.append(
378-
self._swing_wrapper.get_update_command(
377+
commands.extend(
378+
self._swing_wrapper.get_update_commands(
379379
self.device, swing_mode == SWING_ON
380380
)
381381
)
382382
if self._swing_v_wrapper:
383-
commands.append(
384-
self._swing_v_wrapper.get_update_command(
383+
commands.extend(
384+
self._swing_v_wrapper.get_update_commands(
385385
self.device, swing_mode in (SWING_BOTH, SWING_VERTICAL)
386386
)
387387
)
388388
if self._swing_h_wrapper:
389-
commands.append(
390-
self._swing_h_wrapper.get_update_command(
389+
commands.extend(
390+
self._swing_h_wrapper.get_update_commands(
391391
self.device, swing_mode in (SWING_BOTH, SWING_HORIZONTAL)
392392
)
393393
)

homeassistant/components/tuya/cover.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ async def async_open_cover(self, **kwargs: Any) -> None:
421421

422422
if self._set_position is not None:
423423
await self._async_send_commands(
424-
[self._set_position.get_update_command(self.device, 100)]
424+
self._set_position.get_update_commands(self.device, 100)
425425
)
426426

427427
async def async_close_cover(self, **kwargs: Any) -> None:
@@ -434,7 +434,7 @@ async def async_close_cover(self, **kwargs: Any) -> None:
434434

435435
if self._set_position is not None:
436436
await self._async_send_commands(
437-
[self._set_position.get_update_command(self.device, 0)]
437+
self._set_position.get_update_commands(self.device, 0)
438438
)
439439

440440
async def async_set_cover_position(self, **kwargs: Any) -> None:

homeassistant/components/tuya/entity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ async def _async_send_wrapper_updates(
8484
return
8585
await self.hass.async_add_executor_job(
8686
self._send_command,
87-
[dpcode_wrapper.get_update_command(self.device, value)],
87+
dpcode_wrapper.get_update_commands(self.device, value),
8888
)

homeassistant/components/tuya/fan.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,16 @@ async def async_turn_on(
233233
if self._switch_wrapper is None:
234234
return
235235

236-
commands: list[dict[str, str | bool | int]] = [
237-
self._switch_wrapper.get_update_command(self.device, True)
238-
]
236+
commands = self._switch_wrapper.get_update_commands(self.device, True)
239237

240238
if percentage is not None and self._speed_wrapper is not None:
241-
commands.append(
242-
self._speed_wrapper.get_update_command(self.device, percentage)
239+
commands.extend(
240+
self._speed_wrapper.get_update_commands(self.device, percentage)
243241
)
244242

245243
if preset_mode is not None and self._mode_wrapper:
246-
commands.append(
247-
self._mode_wrapper.get_update_command(self.device, preset_mode)
244+
commands.extend(
245+
self._mode_wrapper.get_update_commands(self.device, preset_mode)
248246
)
249247
await self._async_send_commands(commands)
250248

homeassistant/components/tuya/light.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -720,25 +720,23 @@ def is_on(self) -> bool | None:
720720

721721
def turn_on(self, **kwargs: Any) -> None:
722722
"""Turn on or control the light."""
723-
commands = [
724-
self._switch_wrapper.get_update_command(self.device, True),
725-
]
723+
commands = self._switch_wrapper.get_update_commands(self.device, True)
726724

727725
if self._color_mode_wrapper and (
728726
ATTR_WHITE in kwargs or ATTR_COLOR_TEMP_KELVIN in kwargs
729727
):
730-
commands += [
731-
self._color_mode_wrapper.get_update_command(
728+
commands.extend(
729+
self._color_mode_wrapper.get_update_commands(
732730
self.device, WorkMode.WHITE
733731
),
734-
]
732+
)
735733

736734
if self._color_temp_wrapper and ATTR_COLOR_TEMP_KELVIN in kwargs:
737-
commands += [
738-
self._color_temp_wrapper.get_update_command(
735+
commands.extend(
736+
self._color_temp_wrapper.get_update_commands(
739737
self.device, kwargs[ATTR_COLOR_TEMP_KELVIN]
740738
)
741-
]
739+
)
742740

743741
if self._color_data_wrapper and (
744742
ATTR_HS_COLOR in kwargs
@@ -750,23 +748,23 @@ def turn_on(self, **kwargs: Any) -> None:
750748
)
751749
):
752750
if self._color_mode_wrapper:
753-
commands += [
754-
self._color_mode_wrapper.get_update_command(
751+
commands.extend(
752+
self._color_mode_wrapper.get_update_commands(
755753
self.device, WorkMode.COLOUR
756754
),
757-
]
755+
)
758756

759757
if not (brightness := kwargs.get(ATTR_BRIGHTNESS)):
760758
brightness = self.brightness or 0
761759

762760
if not (color := kwargs.get(ATTR_HS_COLOR)):
763761
color = self.hs_color or (0, 0)
764762

765-
commands += [
766-
self._color_data_wrapper.get_update_command(
763+
commands.extend(
764+
self._color_data_wrapper.get_update_commands(
767765
self.device, (color, brightness)
768766
),
769-
]
767+
)
770768

771769
elif self._brightness_wrapper and (
772770
ATTR_BRIGHTNESS in kwargs or ATTR_WHITE in kwargs
@@ -776,9 +774,9 @@ def turn_on(self, **kwargs: Any) -> None:
776774
else:
777775
brightness = kwargs[ATTR_WHITE]
778776

779-
commands += [
780-
self._brightness_wrapper.get_update_command(self.device, brightness),
781-
]
777+
commands.extend(
778+
self._brightness_wrapper.get_update_commands(self.device, brightness),
779+
)
782780

783781
self._send_command(commands)
784782

homeassistant/components/tuya/models.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,20 +196,24 @@ def read_device_status(self, device: CustomerDevice) -> Any | None:
196196
def _convert_value_to_raw_value(self, device: CustomerDevice, value: Any) -> Any:
197197
"""Convert a Home Assistant value back to a raw device value.
198198
199-
This is called by `get_update_command` to prepare the value for sending
199+
This is called by `get_update_commands` to prepare the value for sending
200200
back to the device, and should be implemented in concrete classes if needed.
201201
"""
202202
raise NotImplementedError
203203

204-
def get_update_command(self, device: CustomerDevice, value: Any) -> dict[str, Any]:
205-
"""Get the update command for the dpcode.
204+
def get_update_commands(
205+
self, device: CustomerDevice, value: Any
206+
) -> list[dict[str, Any]]:
207+
"""Get the update commands for the dpcode.
206208
207209
The Home Assistant value is converted back to a raw device value.
208210
"""
209-
return {
210-
"code": self.dpcode,
211-
"value": self._convert_value_to_raw_value(device, value),
212-
}
211+
return [
212+
{
213+
"code": self.dpcode,
214+
"value": self._convert_value_to_raw_value(device, value),
215+
}
216+
]
213217

214218

215219
class DPCodeTypeInformationWrapper[T: TypeInformation](DPCodeWrapper):

0 commit comments

Comments
 (0)