How can I implement increment function for enum objects?
I am working with a Dyson fan and am trying to write a function that increases the fan speed. The FanSpeed ββenum has the following members. When I execute [print (i) for i in FanSpeed], I get:
FanSpeed.FAN_SPEED_1
FanSpeed.FAN_SPEED_2
FanSpeed.FAN_SPEED_3
FanSpeed.FAN_SPEED_4
FanSpeed.FAN_SPEED_5
FanSpeed.FAN_SPEED_6
FanSpeed.FAN_SPEED_7
FanSpeed.FAN_SPEED_8
FanSpeed.FAN_SPEED_9
FanSpeed.FAN_SPEED_10
So, the first object of the enumeration (above) is named FAN_SPEED_1 ( FanSpeed.FAN_SPEED_1.name
) and has the value "0001" ( FanSpeed.FAN_SPEED_1.value
) and so on.
Speed ββsetting function (up to 5 in this example):
fan.set_configuration(fan_mode=FanMode.FAN, fan_speed=FanSpeed.FAN_SPEED_5)
I can't figure out how to write a function that gets the current speed and then sets it one level higher. I tried:
1 / Treat the enumeration object as strings and replace the last character with a number (character type) that is above. However, this approach does not work for enumerated objects.
2 / Looking for some kind of increment or next function, but they don't seem to exist for enum objects.
source to share
So, use an example from the Python documentation:
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
current_color = Color.RED
new_color = Color(current_color.value + 1)
>>> new_color == Color.GREEN
True
Thanks, I haven't played around with the Enum type yet, so it was a good learning experience.
source to share