Skip to content

Commit 2ec63e4

Browse files
authored
Correctly map mode number to string. (#202)
1 parent b9aad79 commit 2ec63e4

File tree

4 files changed

+21
-46
lines changed

4 files changed

+21
-46
lines changed

custom_components/dreo/pydreo/pydreofanbase.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,11 @@ def preset_mode(self):
144144
if mode is None:
145145
mode = self._wind_type
146146

147-
if mode is None:
147+
str_value : str = Helpers.name_from_value(self._preset_modes, mode)
148+
if (str_value is None):
148149
return None
149-
150-
# If we can't match the preset mode, just return the first one.
151-
if mode > len(self.preset_modes):
152-
return self.preset_modes[0]
153-
154-
return self.preset_modes[mode - 1]
150+
151+
return str_value
155152

156153
@preset_mode.setter
157154
def preset_mode(self, value: str) -> None:
@@ -164,8 +161,9 @@ def preset_mode(self, value: str) -> None:
164161
else:
165162
raise NotImplementedError("Attempting to set preset_mode on a device that doesn't support.")
166163

167-
if value in self.preset_modes:
168-
self._send_command(key, self.preset_modes.index(value) + 1)
164+
numeric_value = Helpers.value_from_name(self._preset_modes, value)
165+
if numeric_value is not None:
166+
self._send_command(key, numeric_value)
169167
else:
170168
raise ValueError(f"Preset mode {value} is not in the acceptable list: {self.preset_modes}")
171169

custom_components/dreo/pydreo/pydreohumidifier.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,19 @@ def panel_sound(self, value: bool) -> None:
109109
@property
110110
def mode(self):
111111
"""Return the current mode."""
112-
mode = self._mode
113-
114-
# If we can't match the preset mode, just return the first one.
115-
if mode > len(self.modes):
116-
return self.modes[0]
117-
118-
return self.modes[mode - 1]
112+
113+
str_value : str = Helpers.name_from_value(self._modes, self._mode)
114+
if (str_value is None):
115+
return None
116+
return str_value
119117

120118
@mode.setter
121119
def mode(self, value: str) -> None:
122-
key: str = None
123-
124-
if value in self.modes:
125-
self._send_command(MODE_KEY, self.modes.index(value) + 1)
120+
numeric_value = Helpers.value_from_name(self._modes, value)
121+
if numeric_value is not None:
122+
self._send_command(MODE_KEY, numeric_value)
126123
else:
127-
raise ValueError(f"Preset mode {value} is not in the acceptable list: {self.modes}")
124+
raise ValueError(f"Preset mode {value} is not in the acceptable list: {self._modes}")
128125

129126
def update_state(self, state: dict):
130127
"""Process the state dictionary from the REST API."""

tests/dreo/integrationtests/test_helpers.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/pydreo/test_pydreohumidifier.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ def test_HHM001S(self): # pylint: disable=invalid-name
2525
assert humidifier.is_feature_supported('target_humidity') is True
2626
assert humidifier.humidity == 47
2727
assert humidifier.target_humidity == 60
28+
29+
with patch(PATCH_SEND_COMMAND) as mock_send_command:
30+
humidifier.mode = 'auto'
31+
mock_send_command.assert_called_once_with(humidifier, {MODE_KEY: 1})
32+

0 commit comments

Comments
 (0)