Skip to content

Commit e0bb30f

Browse files
authored
Migrate Tuya fan (switch) to use wrapper class (home-assistant#156936)
1 parent e5ae58c commit e0bb30f

File tree

1 file changed

+20
-15
lines changed
  • homeassistant/components/tuya

1 file changed

+20
-15
lines changed

homeassistant/components/tuya/fan.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
from . import TuyaConfigEntry
2424
from .const import TUYA_DISCOVERY_NEW, DeviceCategory, DPCode, DPType
2525
from .entity import TuyaEntity
26-
from .models import DPCodeEnumWrapper, EnumTypeData, IntegerTypeData, find_dpcode
26+
from .models import (
27+
DPCodeBooleanWrapper,
28+
DPCodeEnumWrapper,
29+
EnumTypeData,
30+
IntegerTypeData,
31+
find_dpcode,
32+
)
2733
from .util import get_dpcode
2834

2935
_DIRECTION_DPCODES = (DPCode.FAN_DIRECTION,)
@@ -82,6 +88,9 @@ def async_discover_device(device_ids: list[str]) -> None:
8288
mode_wrapper=DPCodeEnumWrapper.find_dpcode(
8389
device, _MODE_DPCODES, prefer_function=True
8490
),
91+
switch_wrapper=DPCodeBooleanWrapper.find_dpcode(
92+
device, _SWITCH_DPCODES, prefer_function=True
93+
),
8594
)
8695
)
8796
async_add_entities(entities)
@@ -100,7 +109,6 @@ class TuyaFanEntity(TuyaEntity, FanEntity):
100109
_oscillate: DPCode | None = None
101110
_speed: IntegerTypeData | None = None
102111
_speeds: EnumTypeData | None = None
103-
_switch: DPCode | None = None
104112
_attr_name = None
105113

106114
def __init__(
@@ -109,13 +117,12 @@ def __init__(
109117
device_manager: Manager,
110118
*,
111119
mode_wrapper: DPCodeEnumWrapper | None,
120+
switch_wrapper: DPCodeBooleanWrapper | None,
112121
) -> None:
113122
"""Init Tuya Fan Device."""
114123
super().__init__(device, device_manager)
115124
self._mode_wrapper = mode_wrapper
116-
117-
self._switch = get_dpcode(self.device, _SWITCH_DPCODES)
118-
125+
self._switch_wrapper = switch_wrapper
119126
if mode_wrapper:
120127
self._attr_supported_features |= FanEntityFeature.PRESET_MODE
121128
self._attr_preset_modes = mode_wrapper.type_information.range
@@ -141,7 +148,7 @@ def __init__(
141148
):
142149
self._direction = enum_type
143150
self._attr_supported_features |= FanEntityFeature.DIRECTION
144-
if self._switch is not None:
151+
if switch_wrapper:
145152
self._attr_supported_features |= (
146153
FanEntityFeature.TURN_ON | FanEntityFeature.TURN_OFF
147154
)
@@ -181,22 +188,22 @@ def set_percentage(self, percentage: int) -> None:
181188
]
182189
)
183190

184-
def turn_off(self, **kwargs: Any) -> None:
191+
async def async_turn_off(self, **kwargs: Any) -> None:
185192
"""Turn the fan off."""
186-
self._send_command([{"code": self._switch, "value": False}])
193+
await self._async_send_dpcode_update(self._switch_wrapper, False)
187194

188-
def turn_on(
195+
async def async_turn_on(
189196
self,
190197
percentage: int | None = None,
191198
preset_mode: str | None = None,
192199
**kwargs: Any,
193200
) -> None:
194201
"""Turn on the fan."""
195-
if self._switch is None:
202+
if self._switch_wrapper is None:
196203
return
197204

198205
commands: list[dict[str, str | bool | int]] = [
199-
{"code": self._switch, "value": True}
206+
self._switch_wrapper.get_update_command(self.device, True)
200207
]
201208

202209
if percentage is not None and self._speed is not None:
@@ -221,7 +228,7 @@ def turn_on(
221228
commands.append(
222229
self._mode_wrapper.get_update_command(self.device, preset_mode)
223230
)
224-
self._send_command(commands)
231+
await self._async_send_commands(commands)
225232

226233
def oscillate(self, oscillating: bool) -> None:
227234
"""Oscillate the fan."""
@@ -232,9 +239,7 @@ def oscillate(self, oscillating: bool) -> None:
232239
@property
233240
def is_on(self) -> bool | None:
234241
"""Return true if fan is on."""
235-
if self._switch is None:
236-
return None
237-
return self.device.status.get(self._switch)
242+
return self._read_wrapper(self._switch_wrapper)
238243

239244
@property
240245
def current_direction(self) -> str | None:

0 commit comments

Comments
 (0)