-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add xml command "GetFanSpeed" (#457)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
e3d96f1
commit 2f478b3
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) |