-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add onBattery * add battery tests * decrease target to 80%
- Loading branch information
Showing
9 changed files
with
120 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ coverage: | |
status: | ||
patch: | ||
default: | ||
target: 100% | ||
target: 80% |
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 |
---|---|---|
@@ -1,23 +1,9 @@ | ||
"""Battery commands.""" | ||
from typing import Any | ||
from ..messages import OnBattery | ||
from .common import _NoArgsCommand | ||
|
||
from ..events import BatteryEvent | ||
from ..message import HandlingResult | ||
from .common import EventBus, _NoArgsCommand | ||
|
||
|
||
class GetBattery(_NoArgsCommand): | ||
class GetBattery(OnBattery, _NoArgsCommand): | ||
"""Get battery command.""" | ||
|
||
name = "getBattery" | ||
|
||
@classmethod | ||
def _handle_body_data_dict( | ||
cls, event_bus: EventBus, data: dict[str, Any] | ||
) -> HandlingResult: | ||
"""Handle message->body->data and notify the correct event subscribers. | ||
:return: A message response | ||
""" | ||
event_bus.notify(BatteryEvent(data["value"])) | ||
return HandlingResult.success() |
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 |
---|---|---|
@@ -1,23 +1,17 @@ | ||
"""Messages module.""" | ||
|
||
|
||
from typing import Dict, Type | ||
|
||
from ..commands import COMMANDS_WITH_HANDLING | ||
from ..message import Message | ||
from ..messages.stats import ReportStats | ||
from .battery import OnBattery | ||
|
||
# fmt: off | ||
# ordered by file asc | ||
_MESSAGES: list[type[Message]] = [ | ||
OnBattery, | ||
|
||
ReportStats | ||
] | ||
# fmt: on | ||
|
||
MESSAGES: dict[str, type[Message]] = { | ||
message.name: message | ||
for message in ( | ||
_MESSAGES | ||
+ [cmd for cmd in COMMANDS_WITH_HANDLING.values() if issubclass(cmd, Message)] | ||
) | ||
} | ||
MESSAGES: dict[str, type[Message]] = {message.name: message for message in _MESSAGES} |
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,23 @@ | ||
"""Battery messages.""" | ||
from typing import Any | ||
|
||
from ..events import BatteryEvent | ||
from ..events.event_bus import EventBus | ||
from ..message import HandlingResult, Message | ||
|
||
|
||
class OnBattery(Message): | ||
"""On battery message.""" | ||
|
||
name = "onBattery" | ||
|
||
@classmethod | ||
def _handle_body_data_dict( | ||
cls, event_bus: EventBus, data: dict[str, Any] | ||
) -> HandlingResult: | ||
"""Handle message->body->data and notify the correct event subscribers. | ||
:return: A message response | ||
""" | ||
event_bus.notify(BatteryEvent(data["value"])) | ||
return HandlingResult.success() |
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,18 @@ | ||
from typing import Any | ||
from unittest.mock import Mock | ||
|
||
from deebot_client.commands import CommandWithHandling | ||
from deebot_client.events import Event | ||
from deebot_client.events.event_bus import EventBus | ||
from deebot_client.message import HandlingState | ||
|
||
|
||
def assert_command_requested( | ||
command: CommandWithHandling, data: dict[str, Any], expected_event: Event | ||
): | ||
event_bus = Mock(spec_set=EventBus) | ||
|
||
result = command.handle_requested(event_bus, data) | ||
|
||
assert result.state == HandlingState.SUCCESS | ||
event_bus.notify.assert_called_once_with(expected_event) |
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,29 @@ | ||
import pytest | ||
|
||
from deebot_client.commands import GetBattery | ||
from deebot_client.events import BatteryEvent | ||
from tests.commands import assert_command_requested | ||
|
||
|
||
@pytest.mark.parametrize("percentage", [0, 49, 100]) | ||
def test_get_battery_requested(percentage: int): | ||
data = { | ||
"id": "ALZf", | ||
"ret": "ok", | ||
"resp": { | ||
"header": { | ||
"pri": 1, | ||
"tzm": 480, | ||
"ts": "1304623069888", | ||
"ver": "0.0.1", | ||
"fwVer": "1.8.2", | ||
"hwVer": "0.1.1", | ||
}, | ||
"body": { | ||
"code": 0, | ||
"msg": "ok", | ||
"data": {"value": percentage, "isLow": 1 if percentage < 20 else 0}, | ||
}, | ||
}, | ||
} | ||
assert_command_requested(GetBattery(), data, BatteryEvent(percentage)) |
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,15 @@ | ||
from typing import Any | ||
from unittest.mock import Mock | ||
|
||
from deebot_client.events import Event | ||
from deebot_client.events.event_bus import EventBus | ||
from deebot_client.message import HandlingState, Message | ||
|
||
|
||
def assert_message(message: type[Message], data: dict[str, Any], expected_event: Event): | ||
event_bus = Mock(spec_set=EventBus) | ||
|
||
result = message.handle(event_bus, data) | ||
|
||
assert result.state == HandlingState.SUCCESS | ||
event_bus.notify.assert_called_once_with(expected_event) |
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,22 @@ | ||
import pytest | ||
|
||
from deebot_client.events import BatteryEvent | ||
from deebot_client.messages import OnBattery | ||
from tests.messages import assert_message | ||
|
||
|
||
@pytest.mark.parametrize("percentage", [0, 49, 100]) | ||
def test_getBattery(percentage: int): | ||
data = { | ||
"header": { | ||
"pri": 1, | ||
"tzm": 480, | ||
"ts": "1304637391896", | ||
"ver": "0.0.1", | ||
"fwVer": "1.8.2", | ||
"hwVer": "0.1.1", | ||
}, | ||
"body": {"data": {"value": percentage, "isLow": 1 if percentage < 20 else 0}}, | ||
} | ||
|
||
assert_message(OnBattery, data, BatteryEvent(percentage)) |