-
-
Notifications
You must be signed in to change notification settings - Fork 149
Add XML command "GetLifeSpan" #575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
68d652c
Add XML command "GetLifeSpan"
flubshi 8264083
fix XML command "TypeError: 'classmethod' object is not callable"
flubshi 17be319
Fix some type errors
flubshi 7654370
Fix some type errors
flubshi b4d4028
[pre-commit.ci] auto fixes from pre-commit.com hooks
flubshi 748545d
Merge branch 'dev' into xml_life_span
edenhaus b8351f5
Review
edenhaus f31fb66
Merge branch 'dev' into xml_life_span
edenhaus 9d987c5
fix
edenhaus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,49 @@ | ||
| """Life span commands.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from deebot_client.events import LifeSpan, LifeSpanEvent | ||
| 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 GetLifeSpan(XmlCommandWithMessageHandling): | ||
| """GetLifeSpan command.""" | ||
|
|
||
| NAME = "GetLifeSpan" | ||
|
|
||
| def __init__(self, life_span: LifeSpan) -> None: | ||
| super().__init__({"type": life_span.xml_value}) | ||
|
|
||
| @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 (component_type := xml.attrib.get("type")) is None | ||
| or (left_str := xml.attrib.get("left")) is None | ||
| or (total_str := xml.attrib.get("total")) is None | ||
| ): | ||
| return HandlingResult.analyse() | ||
|
|
||
| percent = 0.0 | ||
| left = int(left_str) | ||
| total = int(total_str) | ||
| if total > 0: | ||
| percent = round((left / total) * 100, 2) | ||
|
|
||
| event_bus.notify( | ||
| LifeSpanEvent(LifeSpan.from_xml(component_type), percent, left) | ||
| ) | ||
| return HandlingResult.success() |
This file contains hidden or 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 hidden or 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,59 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from deebot_client.command import CommandResult | ||
| from deebot_client.commands.xml import GetLifeSpan | ||
| from deebot_client.events import LifeSpan, LifeSpanEvent | ||
| from deebot_client.message import HandlingState | ||
| from tests.commands import assert_command | ||
|
|
||
| from . import get_request_xml | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("component_type", "lifespan_type", "left", "total", "expected_event"), | ||
| [ | ||
| ("Brush", LifeSpan.BRUSH, 50, 100, LifeSpanEvent(LifeSpan.BRUSH, 50, 50)), | ||
| ( | ||
| "DustCaseHeap", | ||
| LifeSpan.DUST_CASE_HEAP, | ||
| 50, | ||
| 200, | ||
| LifeSpanEvent(LifeSpan.DUST_CASE_HEAP, 25, 50), | ||
| ), | ||
| ( | ||
| "SideBrush", | ||
| LifeSpan.SIDE_BRUSH, | ||
| 25, | ||
| 200, | ||
| LifeSpanEvent(LifeSpan.SIDE_BRUSH, 12.5, 25), | ||
| ), | ||
| ], | ||
| ) | ||
| async def test_get_life_span( | ||
| component_type: str, | ||
| lifespan_type: LifeSpan, | ||
| left: int, | ||
| total: int, | ||
| expected_event: LifeSpanEvent, | ||
| ) -> None: | ||
| json = get_request_xml( | ||
| f"<ctl ret='ok' type='{component_type}' left='{left}' total='{total}'/>" | ||
| ) | ||
| await assert_command(GetLifeSpan(lifespan_type), json, expected_event) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "xml", | ||
| ["<ctl ret='error'/>", "<ctl ret='ok' type='SideBrush' left='123' />"], | ||
| ids=["error", "no_state"], | ||
| ) | ||
| async def test_get_life_span_error(xml: str) -> None: | ||
| json = get_request_xml(xml) | ||
| await assert_command( | ||
| GetLifeSpan(LifeSpan.BRUSH), | ||
| json, | ||
| None, | ||
| command_result=CommandResult(HandlingState.ANALYSE_LOGGED), | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.