Skip to content

Commit 39fa109

Browse files
committed
Refactor converter to module function
1 parent 0fec7db commit 39fa109

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

pysmartthings/device.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def hex_to_hs(color_hex: str) -> (int, int):
5353
return round(hsv[0] * 100, 3), round(hsv[1] * 100, 3)
5454

5555

56+
def bool_to_value(attribute: str, value: bool) -> str:
57+
"""Convert bool value to ON/OFF value of given attribute."""
58+
return ATTRIBUTE_ON_VALUES[attribute] if value else ATTRIBUTE_OFF_VALUES[attribute]
59+
60+
5661
class Command:
5762
"""Define common commands."""
5863

@@ -216,13 +221,6 @@ def is_on(self, attribute: str) -> bool:
216221
return bool(self._attributes[attribute].value)
217222
return self._attributes[attribute].value == ATTRIBUTE_ON_VALUES[attribute]
218223

219-
@staticmethod
220-
def bool_to_value(attribute: str, value: bool) -> str:
221-
"""Convert bool value to ON/OFF value of given attribute."""
222-
return (
223-
ATTRIBUTE_ON_VALUES[attribute] if value else ATTRIBUTE_OFF_VALUES[attribute]
224-
)
225-
226224
def update_attribute_value(self, attribute: str, value):
227225
"""Update the value of an attribute while maintaining unit and data."""
228226
status = self._attributes[attribute]
@@ -332,7 +330,7 @@ def switch(self) -> bool:
332330
@switch.setter
333331
def switch(self, value: bool):
334332
"""Set the value of the switch attribute."""
335-
status_value = DeviceStatusBase.bool_to_value(Attribute.switch, value)
333+
status_value = bool_to_value(Attribute.switch, value)
336334
self.update_attribute_value(Attribute.switch, status_value)
337335

338336
@property
@@ -620,7 +618,7 @@ def mute(self) -> bool:
620618
@mute.setter
621619
def mute(self, value: bool):
622620
"""Set the mute attribute."""
623-
status_value = DeviceStatusBase.bool_to_value(Attribute.mute, value)
621+
status_value = bool_to_value(Attribute.mute, value)
624622
self.update_attribute_value(Attribute.mute, status_value)
625623

626624
@property
@@ -673,7 +671,7 @@ def playback_shuffle(self) -> bool:
673671
@playback_shuffle.setter
674672
def playback_shuffle(self, value: bool):
675673
"""Set the playbackShuffle attribute."""
676-
status_value = DeviceStatusBase.bool_to_value(Attribute.playback_shuffle, value)
674+
status_value = bool_to_value(Attribute.playback_shuffle, value)
677675
self.update_attribute_value(Attribute.playback_shuffle, status_value)
678676

679677
@property
@@ -1300,9 +1298,7 @@ async def set_playback_shuffle(
13001298
self, shuffle: bool, set_status: bool = False, *, component_id: str = "main"
13011299
) -> bool:
13021300
"""Call the setPlaybackShuffle command."""
1303-
shuffle_value = DeviceStatusBase.bool_to_value(
1304-
Attribute.playback_shuffle, shuffle
1305-
)
1301+
shuffle_value = bool_to_value(Attribute.playback_shuffle, shuffle)
13061302
result = await self.command(
13071303
component_id,
13081304
Capability.media_playback_shuffle,

0 commit comments

Comments
 (0)