diff --git a/homeassistant/components/tuya/climate.py b/homeassistant/components/tuya/climate.py index 1eaa3a82a88..dc8a77193da 100644 --- a/homeassistant/components/tuya/climate.py +++ b/homeassistant/components/tuya/climate.py @@ -345,14 +345,14 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): """Set new target hvac mode.""" commands = [] if self._switch_wrapper: - commands.append( - self._switch_wrapper.get_update_command( + commands.extend( + self._switch_wrapper.get_update_commands( self.device, hvac_mode != HVACMode.OFF ) ) if self._hvac_mode_wrapper and hvac_mode in self._hvac_to_tuya: - commands.append( - self._hvac_mode_wrapper.get_update_command( + commands.extend( + self._hvac_mode_wrapper.get_update_commands( self.device, self._hvac_to_tuya[hvac_mode] ) ) @@ -374,20 +374,20 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): """Set new target swing operation.""" commands = [] if self._swing_wrapper: - commands.append( - self._swing_wrapper.get_update_command( + commands.extend( + self._swing_wrapper.get_update_commands( self.device, swing_mode == SWING_ON ) ) if self._swing_v_wrapper: - commands.append( - self._swing_v_wrapper.get_update_command( + commands.extend( + self._swing_v_wrapper.get_update_commands( self.device, swing_mode in (SWING_BOTH, SWING_VERTICAL) ) ) if self._swing_h_wrapper: - commands.append( - self._swing_h_wrapper.get_update_command( + commands.extend( + self._swing_h_wrapper.get_update_commands( self.device, swing_mode in (SWING_BOTH, SWING_HORIZONTAL) ) ) diff --git a/homeassistant/components/tuya/cover.py b/homeassistant/components/tuya/cover.py index b94e27c168b..8753c0c757c 100644 --- a/homeassistant/components/tuya/cover.py +++ b/homeassistant/components/tuya/cover.py @@ -421,7 +421,7 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity): if self._set_position is not None: await self._async_send_commands( - [self._set_position.get_update_command(self.device, 100)] + self._set_position.get_update_commands(self.device, 100) ) async def async_close_cover(self, **kwargs: Any) -> None: @@ -434,7 +434,7 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity): if self._set_position is not None: await self._async_send_commands( - [self._set_position.get_update_command(self.device, 0)] + self._set_position.get_update_commands(self.device, 0) ) async def async_set_cover_position(self, **kwargs: Any) -> None: diff --git a/homeassistant/components/tuya/entity.py b/homeassistant/components/tuya/entity.py index ad38ffe2485..2780b8d1b42 100644 --- a/homeassistant/components/tuya/entity.py +++ b/homeassistant/components/tuya/entity.py @@ -84,5 +84,5 @@ class TuyaEntity(Entity): return await self.hass.async_add_executor_job( self._send_command, - [dpcode_wrapper.get_update_command(self.device, value)], + dpcode_wrapper.get_update_commands(self.device, value), ) diff --git a/homeassistant/components/tuya/fan.py b/homeassistant/components/tuya/fan.py index 9ba4a8ae629..a0180fa76d6 100644 --- a/homeassistant/components/tuya/fan.py +++ b/homeassistant/components/tuya/fan.py @@ -233,18 +233,16 @@ class TuyaFanEntity(TuyaEntity, FanEntity): if self._switch_wrapper is None: return - commands: list[dict[str, str | bool | int]] = [ - self._switch_wrapper.get_update_command(self.device, True) - ] + commands = self._switch_wrapper.get_update_commands(self.device, True) if percentage is not None and self._speed_wrapper is not None: - commands.append( - self._speed_wrapper.get_update_command(self.device, percentage) + commands.extend( + self._speed_wrapper.get_update_commands(self.device, percentage) ) if preset_mode is not None and self._mode_wrapper: - commands.append( - self._mode_wrapper.get_update_command(self.device, preset_mode) + commands.extend( + self._mode_wrapper.get_update_commands(self.device, preset_mode) ) await self._async_send_commands(commands) diff --git a/homeassistant/components/tuya/light.py b/homeassistant/components/tuya/light.py index 1f5cf586d8c..d9edb617ce1 100644 --- a/homeassistant/components/tuya/light.py +++ b/homeassistant/components/tuya/light.py @@ -720,25 +720,23 @@ class TuyaLightEntity(TuyaEntity, LightEntity): def turn_on(self, **kwargs: Any) -> None: """Turn on or control the light.""" - commands = [ - self._switch_wrapper.get_update_command(self.device, True), - ] + commands = self._switch_wrapper.get_update_commands(self.device, True) if self._color_mode_wrapper and ( ATTR_WHITE in kwargs or ATTR_COLOR_TEMP_KELVIN in kwargs ): - commands += [ - self._color_mode_wrapper.get_update_command( + commands.extend( + self._color_mode_wrapper.get_update_commands( self.device, WorkMode.WHITE ), - ] + ) if self._color_temp_wrapper and ATTR_COLOR_TEMP_KELVIN in kwargs: - commands += [ - self._color_temp_wrapper.get_update_command( + commands.extend( + self._color_temp_wrapper.get_update_commands( self.device, kwargs[ATTR_COLOR_TEMP_KELVIN] ) - ] + ) if self._color_data_wrapper and ( ATTR_HS_COLOR in kwargs @@ -750,11 +748,11 @@ class TuyaLightEntity(TuyaEntity, LightEntity): ) ): if self._color_mode_wrapper: - commands += [ - self._color_mode_wrapper.get_update_command( + commands.extend( + self._color_mode_wrapper.get_update_commands( self.device, WorkMode.COLOUR ), - ] + ) if not (brightness := kwargs.get(ATTR_BRIGHTNESS)): brightness = self.brightness or 0 @@ -762,11 +760,11 @@ class TuyaLightEntity(TuyaEntity, LightEntity): if not (color := kwargs.get(ATTR_HS_COLOR)): color = self.hs_color or (0, 0) - commands += [ - self._color_data_wrapper.get_update_command( + commands.extend( + self._color_data_wrapper.get_update_commands( self.device, (color, brightness) ), - ] + ) elif self._brightness_wrapper and ( ATTR_BRIGHTNESS in kwargs or ATTR_WHITE in kwargs @@ -776,9 +774,9 @@ class TuyaLightEntity(TuyaEntity, LightEntity): else: brightness = kwargs[ATTR_WHITE] - commands += [ - self._brightness_wrapper.get_update_command(self.device, brightness), - ] + commands.extend( + self._brightness_wrapper.get_update_commands(self.device, brightness), + ) self._send_command(commands) diff --git a/homeassistant/components/tuya/models.py b/homeassistant/components/tuya/models.py index e3b9c345ca7..9fd88cf0218 100644 --- a/homeassistant/components/tuya/models.py +++ b/homeassistant/components/tuya/models.py @@ -196,20 +196,24 @@ class DPCodeWrapper: def _convert_value_to_raw_value(self, device: CustomerDevice, value: Any) -> Any: """Convert a Home Assistant value back to a raw device value. - This is called by `get_update_command` to prepare the value for sending + This is called by `get_update_commands` to prepare the value for sending back to the device, and should be implemented in concrete classes if needed. """ raise NotImplementedError - def get_update_command(self, device: CustomerDevice, value: Any) -> dict[str, Any]: - """Get the update command for the dpcode. + def get_update_commands( + self, device: CustomerDevice, value: Any + ) -> list[dict[str, Any]]: + """Get the update commands for the dpcode. The Home Assistant value is converted back to a raw device value. """ - return { - "code": self.dpcode, - "value": self._convert_value_to_raw_value(device, value), - } + return [ + { + "code": self.dpcode, + "value": self._convert_value_to_raw_value(device, value), + } + ] class DPCodeTypeInformationWrapper[T: TypeInformation](DPCodeWrapper):