Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions deebot_client/commands/json/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from datetime import UTC, datetime
import time
from types import MappingProxyType
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -45,7 +45,7 @@ def _get_payload(self) -> dict[str, Any] | list[Any]:
payload = {
"header": {
"pri": "1",
"ts": datetime.now(tz=UTC).timestamp(),
"ts": time.time(),
"tzm": 480,
"ver": "0.0.50",
}
Expand Down
22 changes: 12 additions & 10 deletions deebot_client/event_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import asyncio
from datetime import UTC, datetime, timedelta
import threading
from typing import TYPE_CHECKING, Any, Final, TypeVar

from .events import AvailabilityEvent, Event, StateEvent
Expand Down Expand Up @@ -69,7 +68,6 @@ def __init__(
capabilities: Capabilities,
) -> None:
self._event_processing_dict: dict[type[Event], _EventProcessingData[Any]] = {}
self._lock = threading.Lock()
self._tasks: set[asyncio.Future[Any]] = set()

self._execute_command: Final = execute_command
Expand Down Expand Up @@ -208,16 +206,20 @@ async def _call_refresh_function(self, event_class: type[T]) -> None:
def _get_or_create_event_processing_data(
self, event_class: type[T]
) -> _EventProcessingData[T]:
with self._lock:
event_processing_data = self._event_processing_dict.get(event_class, None)
# Python's GIL protects dict.get() and dict.__setitem__() as atomic operations
# So we don't need a lock here - at worst we create the object twice
Comment on lines +209 to +210
Copy link

Copilot AI Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment claims dict operations are atomic under GIL, but this is misleading. While individual dict operations are atomic, the check-then-set pattern (lines 211-220) is not atomic and can still have race conditions where multiple threads create the same object.

Copilot uses AI. Check for mistakes.
event_processing_data = self._event_processing_dict.get(event_class, None)

if event_processing_data is None:
event_processing_data = _EventProcessingData(
self._capabilities.get_refresh_commands(event_class)
)
self._event_processing_dict[event_class] = event_processing_data
if event_processing_data is None:
event_processing_data = _EventProcessingData(
self._capabilities.get_refresh_commands(event_class)
)
# Use setdefault to handle race condition - only one will "win"
event_processing_data = self._event_processing_dict.setdefault(
event_class, event_processing_data
)

return event_processing_data
return event_processing_data

def get_last_event(
self,
Expand Down
220 changes: 111 additions & 109 deletions deebot_client/hardware/2o4lnm.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,125 +102,127 @@
water_info,
)
from deebot_client.models import StaticDeviceInfo
from deebot_client.util import short_name

from . import DEVICES

DEVICES[short_name(__name__)] = StaticDeviceInfo(
DataType.JSON,
Capabilities(
device_type=DeviceType.VACUUM,
availability=CapabilityEvent(
AvailabilityEvent, [GetBattery(is_available_check=True)]
),
battery=CapabilityEvent(BatteryEvent, [GetBattery()]),
charge=CapabilityExecute(Charge),
clean=CapabilityClean(
action=CapabilityCleanAction(command=Clean, area=CleanArea),
continuous=CapabilitySetEnable(
ContinuousCleaningEvent,
[GetContinuousCleaning()],
SetContinuousCleaning,
),
count=CapabilitySet(CleanCountEvent, [GetCleanCount()], SetCleanCount),
log=CapabilityEvent(CleanLogEvent, [GetCleanLogs()]),
preference=CapabilitySetEnable(
CleanPreferenceEvent, [GetCleanPreference()], SetCleanPreference
def get_device_info() -> StaticDeviceInfo:
"""Get device info for this model."""
return StaticDeviceInfo(
DataType.JSON,
Capabilities(
device_type=DeviceType.VACUUM,
availability=CapabilityEvent(
AvailabilityEvent, [GetBattery(is_available_check=True)]
),
),
custom=CapabilityCustomCommand(
event=CustomCommandEvent, get=[], set=CustomCommand
),
error=CapabilityEvent(ErrorEvent, [GetError()]),
fan_speed=CapabilitySetTypes(
event=FanSpeedEvent,
get=[GetFanSpeed()],
set=SetFanSpeed,
types=(
FanSpeedLevel.QUIET,
FanSpeedLevel.NORMAL,
FanSpeedLevel.MAX,
FanSpeedLevel.MAX_PLUS,
),
),
life_span=CapabilityLifeSpan(
types=(
LifeSpan.BRUSH,
LifeSpan.FILTER,
LifeSpan.SIDE_BRUSH,
LifeSpan.UNIT_CARE,
LifeSpan.ROUND_MOP,
battery=CapabilityEvent(BatteryEvent, [GetBattery()]),
charge=CapabilityExecute(Charge),
clean=CapabilityClean(
action=CapabilityCleanAction(command=Clean, area=CleanArea),
continuous=CapabilitySetEnable(
ContinuousCleaningEvent,
[GetContinuousCleaning()],
SetContinuousCleaning,
),
count=CapabilitySet(CleanCountEvent, [GetCleanCount()], SetCleanCount),
log=CapabilityEvent(CleanLogEvent, [GetCleanLogs()]),
preference=CapabilitySetEnable(
CleanPreferenceEvent, [GetCleanPreference()], SetCleanPreference
),
),
event=LifeSpanEvent,
get=[
GetLifeSpan(
[
LifeSpan.BRUSH,
LifeSpan.FILTER,
LifeSpan.SIDE_BRUSH,
LifeSpan.UNIT_CARE,
LifeSpan.ROUND_MOP,
]
)
],
reset=ResetLifeSpan,
),
map=CapabilityMap(
cached_info=CapabilityEvent(CachedMapInfoEvent, [GetCachedMapInfo()]),
changed=CapabilityEvent(MapChangedEvent, []),
major=CapabilitySet(MajorMapEvent, [GetMajorMap()], SetMajorMap),
minor=CapabilityExecute(GetMinorMap),
multi_state=CapabilitySetEnable(
MultimapStateEvent, [GetMultimapState()], SetMultimapState
custom=CapabilityCustomCommand(
event=CustomCommandEvent, get=[], set=CustomCommand
),
position=CapabilityEvent(PositionsEvent, [GetPos()]),
relocation=CapabilityExecute(SetRelocationState),
rooms=CapabilityEvent(RoomsEvent, [GetCachedMapInfo()]),
set=CapabilityExecute(GetMapSet),
trace=CapabilityEvent(MapTraceEvent, [GetMapTrace()]),
),
network=CapabilityEvent(NetworkInfoEvent, [GetNetInfo()]),
play_sound=CapabilityExecute(PlaySound),
settings=CapabilitySettings(
advanced_mode=CapabilitySetEnable(
AdvancedModeEvent, [GetAdvancedMode()], SetAdvancedMode
error=CapabilityEvent(ErrorEvent, [GetError()]),
fan_speed=CapabilitySetTypes(
event=FanSpeedEvent,
get=[GetFanSpeed()],
set=SetFanSpeed,
types=(
FanSpeedLevel.QUIET,
FanSpeedLevel.NORMAL,
FanSpeedLevel.MAX,
FanSpeedLevel.MAX_PLUS,
),
),
carpet_auto_fan_boost=CapabilitySetEnable(
CarpetAutoFanBoostEvent,
[GetCarpetAutoFanBoost()],
SetCarpetAutoFanBoost,
life_span=CapabilityLifeSpan(
types=(
LifeSpan.BRUSH,
LifeSpan.FILTER,
LifeSpan.SIDE_BRUSH,
LifeSpan.UNIT_CARE,
LifeSpan.ROUND_MOP,
),
event=LifeSpanEvent,
get=[
GetLifeSpan(
[
LifeSpan.BRUSH,
LifeSpan.FILTER,
LifeSpan.SIDE_BRUSH,
LifeSpan.UNIT_CARE,
LifeSpan.ROUND_MOP,
]
)
],
reset=ResetLifeSpan,
),
sweep_mode=CapabilitySetEnable(
SweepModeEvent, [GetSweepMode()], SetSweepMode
map=CapabilityMap(
cached_info=CapabilityEvent(CachedMapInfoEvent, [GetCachedMapInfo()]),
changed=CapabilityEvent(MapChangedEvent, []),
major=CapabilitySet(MajorMapEvent, [GetMajorMap()], SetMajorMap),
minor=CapabilityExecute(GetMinorMap),
multi_state=CapabilitySetEnable(
MultimapStateEvent, [GetMultimapState()], SetMultimapState
),
position=CapabilityEvent(PositionsEvent, [GetPos()]),
relocation=CapabilityExecute(SetRelocationState),
rooms=CapabilityEvent(RoomsEvent, [GetCachedMapInfo()]),
set=CapabilityExecute(GetMapSet),
trace=CapabilityEvent(MapTraceEvent, [GetMapTrace()]),
),
true_detect=CapabilitySetEnable(
TrueDetectEvent, [GetTrueDetect()], SetTrueDetect
network=CapabilityEvent(NetworkInfoEvent, [GetNetInfo()]),
play_sound=CapabilityExecute(PlaySound),
settings=CapabilitySettings(
advanced_mode=CapabilitySetEnable(
AdvancedModeEvent, [GetAdvancedMode()], SetAdvancedMode
),
carpet_auto_fan_boost=CapabilitySetEnable(
CarpetAutoFanBoostEvent,
[GetCarpetAutoFanBoost()],
SetCarpetAutoFanBoost,
),
sweep_mode=CapabilitySetEnable(
SweepModeEvent, [GetSweepMode()], SetSweepMode
),
true_detect=CapabilitySetEnable(
TrueDetectEvent, [GetTrueDetect()], SetTrueDetect
),
voice_assistant=CapabilitySetEnable(
VoiceAssistantStateEvent,
[GetVoiceAssistantState()],
SetVoiceAssistantState,
),
volume=CapabilitySet(VolumeEvent, [GetVolume()], SetVolume),
),
voice_assistant=CapabilitySetEnable(
VoiceAssistantStateEvent,
[GetVoiceAssistantState()],
SetVoiceAssistantState,
state=CapabilityEvent(StateEvent, [GetChargeState(), GetCleanInfo()]),
stats=CapabilityStats(
clean=CapabilityEvent(StatsEvent, [GetStats()]),
report=CapabilityEvent(ReportStatsEvent, []),
total=CapabilityEvent(TotalStatsEvent, [GetTotalStats()]),
),
volume=CapabilitySet(VolumeEvent, [GetVolume()], SetVolume),
),
state=CapabilityEvent(StateEvent, [GetChargeState(), GetCleanInfo()]),
stats=CapabilityStats(
clean=CapabilityEvent(StatsEvent, [GetStats()]),
report=CapabilityEvent(ReportStatsEvent, []),
total=CapabilityEvent(TotalStatsEvent, [GetTotalStats()]),
),
water=CapabilityWater(
amount=CapabilitySetTypes(
event=water_info.WaterAmountEvent,
get=[GetWaterInfo()],
set=SetWaterInfo,
types=(
water_info.WaterAmount.LOW,
water_info.WaterAmount.MEDIUM,
water_info.WaterAmount.HIGH,
water=CapabilityWater(
amount=CapabilitySetTypes(
event=water_info.WaterAmountEvent,
get=[GetWaterInfo()],
set=SetWaterInfo,
types=(
water_info.WaterAmount.LOW,
water_info.WaterAmount.MEDIUM,
water_info.WaterAmount.HIGH,
),
),
mop_attached=CapabilityEvent(
water_info.MopAttachedEvent, [GetWaterInfo()]
),
),
mop_attached=CapabilityEvent(water_info.MopAttachedEvent, [GetWaterInfo()]),
),
),
)
)
Loading