mirror of
https://github.com/home-assistant/core.git
synced 2026-06-06 23:46:56 +01:00
Adjust Tuya wrapper to return a command list (#157622)
This commit is contained in:
@@ -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)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user