11from __future__ import annotations
22
3+ from typing import TYPE_CHECKING
4+ from unittest .mock import patch
5+
36import pytest
47
58from deebot_client .command import CommandResult
6- from deebot_client .commands .xml import GetLifeSpan
9+ from deebot_client .commands .xml import GetLifeSpan , ResetLifeSpan
710from deebot_client .events import LifeSpan , LifeSpanEvent
811from deebot_client .message import HandlingState
912from tests .commands import assert_command
1013
11- from . import get_request_xml
14+ from . import assert_execute_command , get_request_xml , get_success_body
15+
16+ if TYPE_CHECKING :
17+ from deebot_client .event_bus import EventBus
1218
1319
1420@pytest .mark .parametrize (
1521 ("component_type" , "lifespan_type" , "left" , "total" , "expected_event" ),
1622 [
1723 ("Brush" , LifeSpan .BRUSH , 50 , 100 , LifeSpanEvent (LifeSpan .BRUSH , 50 , 50 )),
24+ (
25+ "Brush" ,
26+ LifeSpan .BRUSH .xml_value ,
27+ 50 ,
28+ 100 ,
29+ LifeSpanEvent (LifeSpan .BRUSH , 50 , 50 ),
30+ ),
1831 (
1932 "DustCaseHeap" ,
2033 LifeSpan .DUST_CASE_HEAP ,
3346)
3447async def test_get_life_span (
3548 component_type : str ,
36- lifespan_type : LifeSpan ,
49+ lifespan_type : LifeSpan | str ,
3750 left : int ,
3851 total : int ,
3952 expected_event : LifeSpanEvent ,
4053) -> None :
41- json = get_request_xml (
54+ xml = get_request_xml (
4255 f"<ctl ret='ok' type='{ component_type } ' left='{ left } ' total='{ total } '/>"
4356 )
44- await assert_command (GetLifeSpan (lifespan_type ), json , expected_event )
57+ await assert_command (GetLifeSpan (lifespan_type ), xml , expected_event )
4558
4659
4760@pytest .mark .parametrize (
@@ -57,3 +70,30 @@ async def test_get_life_span_error(xml: str) -> None:
5770 None ,
5871 command_result = CommandResult (HandlingState .ANALYSE_LOGGED ),
5972 )
73+
74+
75+ @pytest .mark .parametrize (
76+ ("command" , "args" ),
77+ [
78+ (ResetLifeSpan (LifeSpan .FILTER ), {"type" : LifeSpan .FILTER .xml_value }),
79+ (ResetLifeSpan (LifeSpan .FILTER .xml_value ), {"type" : LifeSpan .FILTER .xml_value }),
80+ (
81+ ResetLifeSpan .create_from_mqtt (b'<ctl type="Brush" />' ),
82+ {"type" : LifeSpan .BRUSH .xml_value },
83+ ),
84+ ],
85+ )
86+ async def test_ResetLifeSpan (command : ResetLifeSpan , args : dict [str , str ]) -> None :
87+ await assert_execute_command (command , args )
88+
89+
90+ def test_ResetLifeSpan_invokes_refresh (event_bus : EventBus ) -> None :
91+ command = ResetLifeSpan (LifeSpan .FILTER )
92+ success_response = get_success_body ()
93+
94+ with patch .object (
95+ event_bus , "request_refresh" , return_value = None
96+ ) as mock_request_refresh :
97+ command .handle_mqtt_p2p (event_bus , success_response )
98+
99+ mock_request_refresh .assert_called_with (LifeSpanEvent )
0 commit comments