-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathclean.py
161 lines (122 loc) · 4.95 KB
/
clean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""Clean commands."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from deebot_client.events import StateEvent
from deebot_client.logging_filter import get_logger
from deebot_client.message import HandlingResult, MessageBodyDataDict
from deebot_client.models import ApiDeviceInfo, CleanAction, CleanMode, State
from .common import ExecuteCommand, JsonCommandWithMessageHandling
if TYPE_CHECKING:
from deebot_client.authentication import Authenticator
from deebot_client.command import CommandResult
from deebot_client.event_bus import EventBus
_LOGGER = get_logger(__name__)
class Clean(ExecuteCommand):
"""Clean command."""
NAME = "clean"
def __init__(self, action: CleanAction) -> None:
super().__init__(self._get_args(action))
async def _execute(
self,
authenticator: Authenticator,
device_info: ApiDeviceInfo,
event_bus: EventBus,
) -> tuple[CommandResult, dict[str, Any]]:
"""Execute command."""
state = event_bus.get_last_event(StateEvent)
if state and isinstance(self._args, dict):
if (
self._args["act"] == CleanAction.RESUME.value
and state.state != State.PAUSED
):
self._args = self._get_args(CleanAction.START)
elif (
self._args["act"] == CleanAction.START.value
and state.state == State.PAUSED
):
self._args = self._get_args(CleanAction.RESUME)
return await super()._execute(authenticator, device_info, event_bus)
def _get_args(self, action: CleanAction) -> dict[str, Any]:
args = {"act": action.value}
if action == CleanAction.START:
args["type"] = CleanMode.AUTO.value
return args
class CleanArea(Clean):
"""Clean area command."""
def __init__(self, mode: CleanMode, area: str, cleanings: int = 1) -> None:
self._additional_args = {
"type": mode.value,
"content": area,
"count": cleanings,
}
super().__init__(CleanAction.START)
def _get_args(self, action: CleanAction) -> dict[str, Any]:
args = super()._get_args(action)
if action == CleanAction.START:
args.update(self._additional_args)
return args
class CleanV2(Clean):
"""Clean V2 command."""
NAME = "clean_V2"
def _get_args(self, action: CleanAction) -> dict[str, Any]:
content: dict[str, str] = {}
args = {"act": action.value, "content": content}
match action:
case CleanAction.START:
content["type"] = CleanMode.AUTO.value
case CleanAction.STOP | CleanAction.PAUSE:
content["type"] = ""
return args
class CleanAreaV2(CleanV2):
"""Clean area command."""
def __init__(self, mode: CleanMode, area: str, _: int = 1) -> None:
self._additional_content = {"type": mode.value, "value": area}
super().__init__(CleanAction.START)
def _get_args(self, action: CleanAction) -> dict[str, Any]:
args = super()._get_args(action)
if action == CleanAction.START:
args["content"].update(self._additional_content)
return args
class GetCleanInfo(JsonCommandWithMessageHandling, MessageBodyDataDict):
"""Get clean info command."""
NAME = "getCleanInfo"
@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
"""
status: State | None = None
state = data.get("state")
if data.get("trigger") == "alert":
status = State.ERROR
elif state == "clean":
clean_state = data.get("cleanState", {})
motion_state = clean_state.get("motionState")
if motion_state == "working":
status = State.CLEANING
elif motion_state == "pause":
status = State.PAUSED
elif motion_state == "goCharging":
status = State.RETURNING
clean_type = clean_state.get("type")
content = clean_state.get("content", {})
if "type" in content:
clean_type = content.get("type")
if clean_type == "customArea":
area_values = content
if "value" in content:
area_values = content.get("value")
_LOGGER.debug("Last custom area values (x1,y1,x2,y2): %s", area_values)
elif state == "goCharging":
status = State.RETURNING
elif state == "idle":
status = State.IDLE
if status:
event_bus.notify(StateEvent(status))
return HandlingResult.success()
return HandlingResult.analyse()
class GetCleanInfoV2(GetCleanInfo):
"""Get clean info v2 command."""
NAME = "getCleanInfo_V2"