Skip to content
Open
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
79 changes: 76 additions & 3 deletions roborock/devices/traits/v1/consumeable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
from enum import StrEnum
from typing import Any, Self

from roborock.const import (
FILTER_REPLACE_TIME,
MAIN_BRUSH_REPLACE_TIME,
SIDE_BRUSH_REPLACE_TIME,
)
from roborock.data import Consumable
from roborock.devices.traits.common import DpsDataConverter, TraitUpdateListener
from roborock.devices.traits.v1 import common
Expand All @@ -22,6 +27,72 @@

_DPS_CONVERTER = DpsDataConverter.from_dataclass(Consumable)

_CONSUMABLE_LIFE_SPECS: dict[RoborockDataProtocol, tuple[str, int]] = {
RoborockDataProtocol.MAIN_BRUSH_WORK_TIME: ("main_brush_work_time", MAIN_BRUSH_REPLACE_TIME),
RoborockDataProtocol.SIDE_BRUSH_WORK_TIME: ("side_brush_work_time", SIDE_BRUSH_REPLACE_TIME),
RoborockDataProtocol.FILTER_WORK_TIME: ("filter_work_time", FILTER_REPLACE_TIME),
}


def _parse_life_percent(raw: Any) -> int | None:
"""Parse a pushed DPS remaining-life percentage in ``[0, 100]``."""
# The pushed value is raw JSON, so narrow it to an integer before converting.
if isinstance(raw, int) and not isinstance(raw, bool):
value = raw
elif isinstance(raw, str):
try:
value = int(raw)
except ValueError:
return None
else:
return None
if 0 <= value <= 100:
return value
return None


def _life_percent_from_work_time(work_time: int, replace_time: int) -> int:
"""Return remaining life percentage (0-100) matching V1 firmware calculation."""
return max(0, min(100, 100 - (work_time * 100) // replace_time))


def _work_time_from_life_percent(life_percent: int, replace_time: int) -> int:
"""Convert remaining life percentage (0-100) to elapsed work time in seconds."""
return ((100 - life_percent) * replace_time) // 100


def _convert_consumable_dps(
consumable: Consumable,
decoded_dps: dict[RoborockDataProtocol, Any],
) -> dict[RoborockDataProtocol, Any]:
"""Convert V1 DPS 125-127 remaining-life percentages into work time in seconds.

V1 devices push remaining life percentages (0-100) on DPS 125-127 while
``get_consumable`` reports cumulative work time in seconds. Keep cached
polled seconds when the pushed percentage matches the current wear bucket
or reports higher life below 100% (e.g. a delayed MQTT push arriving after
a newer local RPC poll); 100% still applies external resets once worn.
"""
if not any(protocol in _CONSUMABLE_LIFE_SPECS for protocol in decoded_dps):
return decoded_dps
converted: dict[RoborockDataProtocol, Any] = {}
for protocol, raw in decoded_dps.items():
if (spec := _CONSUMABLE_LIFE_SPECS.get(protocol)) is None:
converted[protocol] = raw
continue
life_percent = _parse_life_percent(raw)
if life_percent is None:
_LOGGER.debug("Ignoring invalid consumable life value %r for %s", raw, protocol.name)
continue
field_name, replace_time = spec
current_work_time: int | None = getattr(consumable, field_name)
if current_work_time is not None:
current_life = _life_percent_from_work_time(current_work_time, replace_time)
if life_percent == current_life or current_life < life_percent < 100:
continue
converted[protocol] = _work_time_from_life_percent(life_percent, replace_time)
return converted


class ConsumableAttribute(StrEnum):
"""Enum for consumable attributes."""
Expand Down Expand Up @@ -65,8 +136,10 @@ async def reset_consumable(self, consumable: ConsumableAttribute) -> None:
def update_from_dps(self, decoded_dps: dict[RoborockDataProtocol, Any]) -> None:
"""Update the trait from data protocol push message data.

This handles unsolicited status updates pushed by the device
via RoborockDataProtocol codes (e.g. STATE=121, BATTERY=122).
This handles unsolicited consumable updates pushed by the device via
RoborockDataProtocol codes 125-127, which report remaining life
percentages (0-100) rather than elapsed work time in seconds.
"""
if _DPS_CONVERTER.update_from_dps(self, decoded_dps):
converted_dps = _convert_consumable_dps(self, decoded_dps)
if converted_dps and _DPS_CONVERTER.update_from_dps(self, converted_dps):
self._notify_update()
6 changes: 3 additions & 3 deletions roborock/roborock_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class RoborockDataProtocol(RoborockEnum):
BATTERY = 122
FAN_POWER = 123
WATER_BOX_MODE = 124
MAIN_BRUSH_WORK_TIME = 125
SIDE_BRUSH_WORK_TIME = 126
FILTER_WORK_TIME = 127
MAIN_BRUSH_WORK_TIME = 125 # remaining life percentage (0-100), schema code main_brush_life
SIDE_BRUSH_WORK_TIME = 126 # remaining life percentage (0-100), schema code side_brush_life
FILTER_WORK_TIME = 127 # remaining life percentage (0-100), schema code filter_life
ADDITIONAL_PROPS = 128
TASK_COMPLETE = 130
TASK_CANCEL_LOW_POWER = 131
Expand Down
Loading