Skip to content

Commit

Permalink
improve charging detection (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored Dec 13, 2021
1 parent 9118d7b commit e9f61e5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
18 changes: 14 additions & 4 deletions deebot_client/commands/charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
from typing import Any, Dict

from ..events import StatusEvent
from ..message import HandlingResult, HandlingState
from ..logging_filter import get_logger
from ..message import HandlingResult
from ..models import VacuumState
from .common import EventBus, _ExecuteCommand
from .const import CODE

_LOGGER = get_logger(__name__)


class Charge(_ExecuteCommand):
Expand All @@ -21,8 +25,14 @@ def _handle_body(cls, event_bus: EventBus, body: Dict[str, Any]) -> HandlingResu
:return: A message response
"""
response = super()._handle_body(event_bus, body)
if response.state == HandlingState.SUCCESS:
code = int(body.get(CODE, -1))
if code == 0:
event_bus.notify(StatusEvent(True, VacuumState.RETURNING))
return HandlingResult.success()

if code == 30007:
# bot is already charging
event_bus.notify(StatusEvent(True, VacuumState.DOCKED))
return HandlingResult.success()

return response
return super()._handle_body(event_bus, body)
5 changes: 3 additions & 2 deletions deebot_client/commands/charge_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from ..events import StatusEvent
from ..message import HandlingResult
from ..models import VacuumState
from .common import _CODE, EventBus, _NoArgsCommand
from .common import EventBus, _NoArgsCommand
from .const import CODE


class GetChargeState(_NoArgsCommand):
Expand All @@ -26,7 +27,7 @@ def _handle_body_data_dict(

@classmethod
def _handle_body(cls, event_bus: EventBus, body: Dict[str, Any]) -> HandlingResult:
if body.get(_CODE, 0) == 0:
if body.get(CODE, 0) == 0:
# Call this also if code is not in the body
return super()._handle_body(event_bus, body)

Expand Down
5 changes: 2 additions & 3 deletions deebot_client/commands/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
from ..events.event_bus import EventBus
from ..logging_filter import get_logger
from ..message import HandlingResult, HandlingState, Message
from .const import CODE

_LOGGER = get_logger(__name__)

_CODE = "code"


@dataclass(frozen=True)
class CommandResult(HandlingResult):
Expand Down Expand Up @@ -87,7 +86,7 @@ def _handle_body(cls, event_bus: EventBus, body: Dict[str, Any]) -> HandlingResu
:return: A message response
"""
# Success event looks like { "code": 0, "msg": "ok" }
if body.get(_CODE, -1) == 0:
if body.get(CODE, -1) == 0:
return HandlingResult.success()

_LOGGER.warning('Command "%s" was not successfully. body=%s', cls.name, body)
Expand Down
3 changes: 3 additions & 0 deletions deebot_client/commands/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Command constants module."""

CODE = "code"
21 changes: 21 additions & 0 deletions deebot_client/vacuum_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from .events import (
CleanLogEvent,
LifeSpanEvent,
PositionsEvent,
PositionType,
StatsEvent,
StatusEvent,
TotalStatsEvent,
Expand Down Expand Up @@ -52,6 +54,25 @@ def __init__(

self.map: Final[Map] = Map(self.execute_command, self.events)

async def on_pos(event: PositionsEvent) -> None:
if self._status == StatusEvent(True, VacuumState.DOCKED):
return

deebot = next(p for p in event.positions if p.type == PositionType.DEEBOT)

if deebot:
on_charger = filter(
lambda p: p.type == PositionType.CHARGER
and p.x == deebot.x
and p.y == deebot.y,
event.positions,
)
if on_charger:
# deebot on charger so the status should be docked... Checking
self.events.request_refresh(StatusEvent)

self.events.subscribe(PositionsEvent, on_pos)

async def on_status(event: StatusEvent) -> None:
last_status = self._status
self._status = event
Expand Down

0 comments on commit e9f61e5

Please sign in to comment.