Skip to content

Commit 956a294

Browse files
authored
Migrate Tuya fan (oscillate) to use wrapper class (home-assistant#156946)
1 parent 1a23610 commit 956a294

File tree

1 file changed

+11
-18
lines changed
  • homeassistant/components/tuya

1 file changed

+11
-18
lines changed

homeassistant/components/tuya/fan.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,9 @@
2121
)
2222

2323
from . import TuyaConfigEntry
24-
from .const import TUYA_DISCOVERY_NEW, DeviceCategory, DPCode, DPType
24+
from .const import TUYA_DISCOVERY_NEW, DeviceCategory, DPCode
2525
from .entity import TuyaEntity
26-
from .models import (
27-
DPCodeBooleanWrapper,
28-
DPCodeEnumWrapper,
29-
DPCodeIntegerWrapper,
30-
EnumTypeData,
31-
find_dpcode,
32-
)
26+
from .models import DPCodeBooleanWrapper, DPCodeEnumWrapper, DPCodeIntegerWrapper
3327
from .util import get_dpcode
3428

3529
_DIRECTION_DPCODES = (DPCode.FAN_DIRECTION,)
@@ -153,6 +147,9 @@ def async_discover_device(device_ids: list[str]) -> None:
153147
mode_wrapper=DPCodeEnumWrapper.find_dpcode(
154148
device, _MODE_DPCODES, prefer_function=True
155149
),
150+
oscillate_wrapper=DPCodeBooleanWrapper.find_dpcode(
151+
device, _OSCILLATE_DPCODES, prefer_function=True
152+
),
156153
speed_wrapper=_get_speed_wrapper(device),
157154
switch_wrapper=DPCodeBooleanWrapper.find_dpcode(
158155
device, _SWITCH_DPCODES, prefer_function=True
@@ -171,7 +168,6 @@ def async_discover_device(device_ids: list[str]) -> None:
171168
class TuyaFanEntity(TuyaEntity, FanEntity):
172169
"""Tuya Fan Device."""
173170

174-
_oscillate: DPCode | None = None
175171
_attr_name = None
176172

177173
def __init__(
@@ -181,13 +177,15 @@ def __init__(
181177
*,
182178
direction_wrapper: _DirectionEnumWrapper | None,
183179
mode_wrapper: DPCodeEnumWrapper | None,
180+
oscillate_wrapper: DPCodeBooleanWrapper | None,
184181
speed_wrapper: _FanSpeedEnumWrapper | _FanSpeedIntegerWrapper | None,
185182
switch_wrapper: DPCodeBooleanWrapper | None,
186183
) -> None:
187184
"""Init Tuya Fan Device."""
188185
super().__init__(device, device_manager)
189186
self._direction_wrapper = direction_wrapper
190187
self._mode_wrapper = mode_wrapper
188+
self._oscillate_wrapper = oscillate_wrapper
191189
self._speed_wrapper = speed_wrapper
192190
self._switch_wrapper = switch_wrapper
193191

@@ -199,8 +197,7 @@ def __init__(
199197
self._attr_supported_features |= FanEntityFeature.SET_SPEED
200198
self._attr_speed_count = speed_wrapper.get_speed_count()
201199

202-
if dpcode := get_dpcode(self.device, _OSCILLATE_DPCODES):
203-
self._oscillate = dpcode
200+
if oscillate_wrapper:
204201
self._attr_supported_features |= FanEntityFeature.OSCILLATE
205202

206203
if direction_wrapper:
@@ -251,11 +248,9 @@ async def async_turn_on(
251248
)
252249
await self._async_send_commands(commands)
253250

254-
def oscillate(self, oscillating: bool) -> None:
251+
async def async_oscillate(self, oscillating: bool) -> None:
255252
"""Oscillate the fan."""
256-
if self._oscillate is None:
257-
return
258-
self._send_command([{"code": self._oscillate, "value": oscillating}])
253+
await self._async_send_dpcode_update(self._oscillate_wrapper, oscillating)
259254

260255
@property
261256
def is_on(self) -> bool | None:
@@ -270,9 +265,7 @@ def current_direction(self) -> str | None:
270265
@property
271266
def oscillating(self) -> bool | None:
272267
"""Return true if the fan is oscillating."""
273-
if self._oscillate is None:
274-
return None
275-
return self.device.status.get(self._oscillate)
268+
return self._read_wrapper(self._oscillate_wrapper)
276269

277270
@property
278271
def preset_mode(self) -> str | None:

0 commit comments

Comments
 (0)