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

Commit 1774493

Browse files
committed
use method to set status
1 parent 3536e01 commit 1774493

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

deebotozmo/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class BatteryEvent:
3838
@dataclass
3939
class StatusEvent:
4040
available: bool
41-
state: VacuumState
41+
state: Optional[VacuumState]
4242

4343

4444
@dataclass

deebotozmo/vacuum_bot.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ async def execute_command(self, command: Command):
8585

8686
await self.handle(command.name, response)
8787

88+
def set_status(self, *, available: bool = True, status: Optional[VacuumState] = None):
89+
self.statusEvents.notify(StatusEvent(available, status))
90+
91+
last_status = self.vacuum_status
92+
93+
if not available:
94+
self.vacuum_status = None
95+
elif status is not None:
96+
self.vacuum_status = status
97+
98+
if last_status is None and available:
99+
# bot was unavailable
100+
if status is None:
101+
self.statusEvents.request_refresh()
102+
self.errorEvents.request_refresh()
103+
self.fanSpeedEvents.request_refresh()
104+
self.cleanLogsEvents.request_refresh()
105+
self.waterEvents.request_refresh()
106+
self.batteryEvents.request_refresh()
107+
self.statsEvents.request_refresh()
108+
self.lifespanEvents.request_refresh()
109+
88110
# ---------------------------- EVENT HANDLING ----------------------------
89111

90112
async def handle(self, event_name: str, event: dict, requested: bool = True) -> None:
@@ -193,8 +215,7 @@ async def _handle_error(self, event: dict, event_data: dict):
193215
description = ERROR_CODES.get(error)
194216
if error != 0:
195217
_LOGGER.warning(f"Bot in error-state: code={error}, description={description}")
196-
self.vacuum_status = VacuumState.STATE_ERROR
197-
self.statusEvents.notify(StatusEvent(True, self.vacuum_status))
218+
self.set_status(status=VacuumState.STATE_ERROR)
198219
self.errorEvents.notify(ErrorEvent(error, description))
199220
else:
200221
_LOGGER.warning(f"Could not process error event with received data: {event}")
@@ -227,13 +248,11 @@ async def _handle_charge_state_requested(self, event_body: dict):
227248
status = VacuumState.STATE_ERROR
228249

229250
if status:
230-
self.vacuum_status = status
231-
self.statusEvents.notify(StatusEvent(True, status))
251+
self.set_status(status=status)
232252

233253
async def _handle_charge_state(self, event_data: dict):
234254
if event_data.get("isCharging") == 1:
235-
self.vacuum_status = VacuumState.STATE_DOCKED
236-
self.statusEvents.notify(StatusEvent(True, self.vacuum_status))
255+
self.set_status(status=VacuumState.STATE_DOCKED)
237256

238257
async def _handle_life_span(self, event_data: dict):
239258
component: dict
@@ -295,8 +314,7 @@ async def _handle_clean_info(self, event_data: dict):
295314
status = VacuumState.STATE_RETURNING
296315

297316
if status:
298-
self.vacuum_status = status
299-
self.statusEvents.notify(StatusEvent(True, status))
317+
self.set_status(status=status)
300318

301319
if self.vacuum_status == VacuumState.STATE_DOCKED:
302320
self.cleanLogsEvents.request_refresh()

0 commit comments

Comments
 (0)