Skip to content

Commit

Permalink
more flexible motor motion parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
siku2 committed Aug 23, 2023
1 parent 597b1a6 commit e0b2209
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion custom_components/dingz/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async def _handle_mqtt_motor(self, msg: mqtt.ReceiveMessage) -> None:
position=payload["position"],
goal=payload.get("goal"),
lamella=payload["lamella"],
motion=MotorMotion(payload["motion"]),
motion=MotorMotion.parse(payload.get("motion", MotorMotion.STOPPED)),
)
)

Expand Down Expand Up @@ -254,11 +254,33 @@ class ButtonNotification(InternalNotification):


class MotorMotion(IntEnum):
UNKNOWN = -1
STOPPED = 0
OPENING = 1
CLOSING = 2
CALIBRATING = 3

@classmethod
def parse(cls, value: str | int | "MotorMotion") -> "MotorMotion":
if isinstance(value, MotorMotion):
return value

if not isinstance(value, int):
# motion is supposed to be an int according to the documentation, but apparently it isn't.
# but to cover our bases we support both
try:
value = int(value)
except ValueError as exc:
_LOGGER.warn(
f"failed to convert motor motion ({value}) to enum value: {exc}"
)
value = -1
try:
return MotorMotion(value)
except ValueError:
_LOGGER.warn(f"unknown motor motion: {value}")
return MotorMotion.UNKNOWN


@dataclasses.dataclass(slots=True, kw_only=True)
class MotorStateNotification(InternalNotification):
Expand Down

0 comments on commit e0b2209

Please sign in to comment.