Skip to content

Commit

Permalink
Add xml command "GetFanSpeed" (#457)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
flubshi and pre-commit-ci[bot] authored Apr 8, 2024
1 parent e3d96f1 commit 2f478b3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions deebot_client/commands/xml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .charge_state import GetChargeState
from .error import GetError
from .fan_speed import GetFanSpeed
from .pos import GetPos

if TYPE_CHECKING:
Expand All @@ -16,6 +17,7 @@
__all__ = [
"GetChargeState",
"GetError",
"GetFanSpeed",
"GetPos",
]

Expand Down
44 changes: 44 additions & 0 deletions deebot_client/commands/xml/fan_speed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""FanSpeed command module."""

from __future__ import annotations

from typing import TYPE_CHECKING

from deebot_client.events import FanSpeedEvent, FanSpeedLevel
from deebot_client.message import HandlingResult

from .common import XmlCommandWithMessageHandling

if TYPE_CHECKING:
from xml.etree.ElementTree import Element

from deebot_client.event_bus import EventBus


class GetFanSpeed(XmlCommandWithMessageHandling):
"""GetFanSpeed command."""

name = "GetCleanSpeed"

@classmethod
def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult:
"""Handle xml message and notify the correct event subscribers.
:return: A message response
"""
if xml.attrib.get("ret") != "ok" or not (speed := xml.attrib.get("speed")):
return HandlingResult.analyse()

event: FanSpeedEvent | None = None

match speed.lower():
case "standard":
event = FanSpeedEvent(FanSpeedLevel.NORMAL)
case "strong":
event = FanSpeedEvent(FanSpeedLevel.MAX)

if event:
event_bus.notify(event)
return HandlingResult.success()

return HandlingResult.analyse()
44 changes: 44 additions & 0 deletions tests/commands/xml/test_fan_speed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from deebot_client.command import CommandResult
from deebot_client.commands.xml import GetFanSpeed
from deebot_client.events import FanSpeedEvent, FanSpeedLevel
from deebot_client.message import HandlingState
from tests.commands import assert_command

from . import get_request_xml

if TYPE_CHECKING:
from deebot_client.events.base import Event


@pytest.mark.parametrize(
("speed", "expected_event"),
[
("standard", FanSpeedEvent(FanSpeedLevel.NORMAL)),
("strong", FanSpeedEvent(FanSpeedLevel.MAX)),
],
ids=["standard", "strong"],
)
async def test_get_fan_speed(speed: str, expected_event: Event) -> None:
json = get_request_xml(f"<ctl ret='ok' speed='{speed}'/>")
await assert_command(GetFanSpeed(), json, expected_event)


@pytest.mark.parametrize(
"xml",
["<ctl ret='error'/>", "<ctl ret='ok' speed='invalid'/>"],
ids=["error", "no_state"],
)
async def test_get_fan_speed_error(xml: str) -> None:
json = get_request_xml(xml)
await assert_command(
GetFanSpeed(),
json,
None,
command_result=CommandResult(HandlingState.ANALYSE_LOGGED),
)

0 comments on commit 2f478b3

Please sign in to comment.