Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
fix setting correctly state and available
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus committed Jul 15, 2021
1 parent c7e4dd7 commit 0a6075a
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions deebotozmo/vacuum_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
verify_ssl
)

self.vacuum_status: Optional[VacuumState] = None
self.status: StatusEvent = StatusEvent(False, None)
self.fw_version: Optional[str] = None

self._map = Map(self.execute_command)
Expand Down Expand Up @@ -77,28 +77,36 @@ def map(self) -> Map:
return self._map

async def execute_command(self, command: Command):
if command.name == CleanResume.name and self.vacuum_status != "STATE_PAUSED":
if command.name == CleanResume.name and self.status.state != "STATE_PAUSED":
command = CleanStart()

async with self._semaphore:
response = await self.json.send_command(command, self.vacuum)

await self.handle(command.name, response)

def set_status(self, *, available: bool = True, status: Optional[VacuumState] = None):
self.statusEvents.notify(StatusEvent(available, status))
def set_available(self, available: bool):
status = StatusEvent(available, self.status.state)
self._set_status(status)

last_status = self.vacuum_status
def _set_state(self, state: VacuumState):
self._set_status(StatusEvent(True, state))

if not available:
self.vacuum_status = None
elif status is not None:
self.vacuum_status = status
def _set_status(self, status: StatusEvent):
_LOGGER.debug(f"Calling _set_status with {status}")

if last_status is None and available:
last_status = self.status

if self.status == status:
_LOGGER.debug("Status still the same... Skipping")
return
else:
self.status = status
self.statusEvents.notify(status)

if (not last_status.available) and status.available:
# bot was unavailable
if status is None:
self.statusEvents.request_refresh()
self.statusEvents.request_refresh()
self.errorEvents.request_refresh()
self.fanSpeedEvents.request_refresh()
self.cleanLogsEvents.request_refresh()
Expand Down Expand Up @@ -215,7 +223,7 @@ async def _handle_error(self, event: dict, event_data: dict):
description = ERROR_CODES.get(error)
if error != 0:
_LOGGER.warning(f"Bot in error-state: code={error}, description={description}")
self.set_status(status=VacuumState.STATE_ERROR)
self._set_state(VacuumState.STATE_ERROR)
self.errorEvents.notify(ErrorEvent(error, description))
else:
_LOGGER.warning(f"Could not process error event with received data: {event}")
Expand Down Expand Up @@ -248,11 +256,11 @@ async def _handle_charge_state_requested(self, event_body: dict):
status = VacuumState.STATE_ERROR

if status:
self.set_status(status=status)
self._set_state(status)

async def _handle_charge_state(self, event_data: dict):
if event_data.get("isCharging") == 1:
self.set_status(status=VacuumState.STATE_DOCKED)
self._set_state(VacuumState.STATE_DOCKED)

async def _handle_life_span(self, event_data: dict):
component: dict
Expand Down Expand Up @@ -314,7 +322,7 @@ async def _handle_clean_info(self, event_data: dict):
status = VacuumState.STATE_RETURNING

if status:
self.set_status(status=status)
self._set_state(status)

if self.vacuum_status == VacuumState.STATE_DOCKED:
if self.status.state == VacuumState.STATE_DOCKED:
self.cleanLogsEvents.request_refresh()

0 comments on commit 0a6075a

Please sign in to comment.