From 7a1a7c2aec6266c73e46e33c2b18cdc4897dc8b1 Mon Sep 17 00:00:00 2001 From: Duvan Jamid Vargas Date: Tue, 24 Nov 2026 15:05:51 -0500 Subject: [PATCH] ijai: extend Pro2Vacuum (ijai.vacuum.v3) with map info and more controls Add read-only map info (map_status, get_maps) and more of the controls exposed by the MIoT spec of ijai.vacuum.v3: cleaning modes, room cleaning, water level, mop route, repeat, tank vibration, volume, locate, DND, consumable reset and manual control. Extend status() with the matching readable properties. Co-Authored-By: Claude Sonnet 5.5 --- miio/integrations/ijai/vacuum/pro2vacuum.py | 304 +++++++++++++++++- .../ijai/vacuum/test_pro2vacuum.py | 159 ++++++++- 2 files changed, 461 insertions(+), 2 deletions(-) diff --git a/miio/integrations/ijai/vacuum/pro2vacuum.py b/miio/integrations/ijai/vacuum/pro2vacuum.py index af5335c52..c65691346 100644 --- a/miio/integrations/ijai/vacuum/pro2vacuum.py +++ b/miio/integrations/ijai/vacuum/pro2vacuum.py @@ -1,11 +1,13 @@ +import json import logging -from datetime import timedelta +from datetime import time, timedelta from enum import Enum import click from miio.click_common import EnumType, command, format_output from miio.devicestatus import sensor, setting +from miio.exceptions import DeviceException from miio.miot_device import DeviceStatus, MiotDevice _LOGGER = logging.getLogger(__name__) @@ -27,6 +29,10 @@ }, # 0 - Global, 1 - Mop, 2 - Edge, 3 - Area, 4 - Point, 5 - Remote, 6 - Explore, 7 - Room, 8 - Floor "start": {"siid": 2, "aiid": 1}, "stop": {"siid": 2, "aiid": 2}, + "start_sweep": {"siid": 2, "aiid": 3}, # Start Only Sweep + "start_sweep_mop": {"siid": 2, "aiid": 5}, # Start Sweep Mop + "start_mop": {"siid": 2, "aiid": 6}, # Start Mop + "start_room_sweep": {"siid": 2, "aiid": 7}, # in: piid 10 (Room IDs, string) # Battery (siid=3) "battery": {"siid": 3, "piid": 1}, # [0, 100] step 1 "home": {"siid": 3, "aiid": 1}, # Start Charge @@ -48,9 +54,43 @@ "current_language": {"siid": 7, "piid": 21}, # string "clean_time": {"siid": 7, "piid": 22}, # [0, 120] step 1 "clean_area": {"siid": 7, "piid": 23}, # [0, 1200] step 1 + "repeat_state": {"siid": 7, "piid": 1}, # 0 - off, 1 - on (clean twice) + "door_state": {"siid": 7, "piid": 3}, # 0 - none, 1 - dust box, 2 - water tank + "mop_route": {"siid": 7, "piid": 7}, # 0 - bow style, 1 - Y style + "time_zone": {"siid": 7, "piid": 20}, # [-99999, 99999] step 1 + "tank_shake": {"siid": 7, "piid": 48}, # 0 - off, 1 - on + "shake_shift": {"siid": 7, "piid": 50}, # 1 - low, 2 - mid, 3 - high + # Alarm (siid=4) + "volume": {"siid": 4, "piid": 2}, # [0, 10] step 1 + # Do not disturb (siid=12) + "dnd_enable": {"siid": 12, "piid": 1}, + "dnd_start_hour": {"siid": 12, "piid": 2}, + "dnd_start_minute": {"siid": 12, "piid": 3}, + "dnd_end_hour": {"siid": 12, "piid": 4}, + "dnd_end_minute": {"siid": 12, "piid": 5}, + # Actions + "reset_consumable": {"siid": 7, "aiid": 1}, # in: piid 17 (consumable-index) + "set_dnd": {"siid": 12, "aiid": 1}, # in: piids 1-6 } } +# Map service (siid=10), read-only access. Kept out of _MAPPINGS on purpose so +# that status() does not request these properties on every poll. +_MAP_SIID = 10 +_MAP_PROPS = { + "current_map_id": {"siid": _MAP_SIID, "piid": 2}, # uint32 + "map_count": {"siid": _MAP_SIID, "piid": 3}, # uint8 [0, 5] + "has_new_map": {"siid": _MAP_SIID, "piid": 19}, # uint8 0 - no, 1 - yes +} +_ROOM_IDS_PIID = 10 # Robot Cleaner (siid=2) Room IDs, string +_ALARM_SIID = 4 +_ALARM_PLAY_PIID = 1 +_SWEEP_SIID = 7 +_DIRECTION_PIID = 16 +_CONSUMABLE_INDEX_PIID = 17 +_MAP_AIID_GET_MAP_LIST = 1 +_MAP_OUT_PIID_MAP_LIST = 4 + ERROR_CODES: dict[int, str] = {2105: "Fully charged"} @@ -113,6 +153,34 @@ class MopRoute(Enum): YStyle = 1 +class Direction(Enum): + """Manual (remote control) movement, siid=7 piid=16.""" + + Forward = 1 + Left = 2 + Right = 3 + Backward = 4 + Stop = 5 + Exit = 10 + + +class Consumable(Enum): + """Consumable index used by reset_consumable (siid=7 piid=17).""" + + MainBrush = 1 + SideBrush = 2 + Filter = 3 + Mop = 4 + + +class TankShake(Enum): + """Intensity of the water tank vibration (siid=7 piid=50).""" + + Low = 1 + Medium = 2 + High = 3 + + class Pro2Status(DeviceStatus): """Container for status reports from Mi Robot Vacuum-Mop 2 Pro.""" @@ -264,6 +332,94 @@ def current_language(self) -> str: """Current Language.""" return self.data["current_language"] + @property + @sensor("Clean Twice") + def repeat_state(self) -> bool: + """Whether the area is cleaned twice.""" + return bool(self.data["repeat_state"]) + + @property + @sensor("Door State") + def door_state(self) -> DoorState: + """Which tank (none, dust box, water tank) is attached.""" + return DoorState(self.data["door_state"]) + + @property + @sensor("Mop Route") + def mop_route(self) -> MopRoute: + """Mopping route.""" + return MopRoute(self.data["mop_route"]) + + @property + def time_zone(self) -> int: + """Time zone offset configured on the device (seconds).""" + return self.data["time_zone"] + + @property + @sensor("Tank Shake") + def tank_shake(self) -> bool: + """Whether the water tank vibration is enabled.""" + return bool(self.data["tank_shake"]) + + @property + @sensor("Tank Shake Level") + def shake_shift(self) -> TankShake: + """Intensity of the water tank vibration.""" + return TankShake(self.data["shake_shift"]) + + @property + @sensor("Volume", unit="%") + def volume(self) -> int: + """Voice volume [0, 10].""" + return self.data["volume"] + + @property + @sensor("Do Not Disturb") + def dnd_enabled(self) -> bool: + """Whether do-not-disturb is enabled.""" + return bool(self.data["dnd_enable"]) + + @property + def dnd_start(self) -> time: + """Do-not-disturb start time.""" + return time(self.data["dnd_start_hour"], self.data["dnd_start_minute"]) + + @property + def dnd_end(self) -> time: + """Do-not-disturb end time.""" + return time(self.data["dnd_end_hour"], self.data["dnd_end_minute"]) + + +class Pro2MapStatus(DeviceStatus): + """Container for the map information of a Mi Robot Vacuum-Mop 2 Pro.""" + + def __init__(self, data): + """Response (MIoT format) of the map service (siid=10) + + Example:: + [ + {'did': 'current_map_id', 'siid': 10, 'piid': 2, 'code': 0, 'value': 1700000002}, + {'did': 'map_count', 'siid': 10, 'piid': 3, 'code': 0, 'value': 4}, + {'did': 'has_new_map', 'siid': 10, 'piid': 19, 'code': 0, 'value': 0}, + ] + """ + self.data = data + + @property + def current_map_id(self) -> int: + """Id of the currently active map.""" + return self.data["current_map_id"] + + @property + def map_count(self) -> int: + """Number of stored maps.""" + return self.data["map_count"] + + @property + def has_new_map(self) -> bool: + """True if the device has a map that was not uploaded yet.""" + return bool(self.data["has_new_map"]) + class Pro2Vacuum(MiotDevice): """Support for Mi Robot Vacuum-Mop 2 Pro (ijai.vacuum.v3).""" @@ -282,6 +438,31 @@ def status(self) -> Pro2Status: } ) + @command() + def map_status(self) -> Pro2MapStatus: + """Return the current map id, the number of maps and the new-map flag.""" + props = [{"did": k, **v} for k, v in _MAP_PROPS.items()] + return Pro2MapStatus( + { + prop["did"]: prop["value"] if prop["code"] == 0 else None + for prop in self.get_properties( + props, property_getter="get_properties", max_properties=10 + ) + } + ) + + @command() + def get_maps(self) -> list[dict]: + """Return the stored maps as a list of dicts with name, id and cur keys. + + ``cur`` is true for the map that is currently active. + """ + res = self.call_action_by(_MAP_SIID, _MAP_AIID_GET_MAP_LIST) + for out in res.get("out", []): + if out.get("piid") == _MAP_OUT_PIID_MAP_LIST: + return json.loads(out["value"]) + raise DeviceException(f"Unexpected response to get-map-list: {res}") + @command() def home(self): """Go Home.""" @@ -297,6 +478,34 @@ def stop(self): """Stop Cleaning.""" return self.call_action_from_mapping("stop") + @command() + def start_sweep(self) -> None: + """Start cleaning in sweep-only mode.""" + return self.call_action_from_mapping("start_sweep") + + @command() + def start_sweep_mop(self) -> None: + """Start cleaning in sweep-and-mop mode.""" + return self.call_action_from_mapping("start_sweep_mop") + + @command() + def start_mop(self) -> None: + """Start cleaning in mop-only mode.""" + return self.call_action_from_mapping("start_mop") + + @command(click.argument("room_ids", type=int, nargs=-1, required=True)) + def start_rooms(self, room_ids: list[int]) -> None: + """Start cleaning the given rooms. + + Room ids are the ones used by the map of the device (e.g. 10, 11, ...). + """ + if not room_ids: + raise ValueError("At least one room id is required") + rooms = ",".join(str(int(r)) for r in room_ids) + return self.call_action_from_mapping( + "start_room_sweep", [{"piid": _ROOM_IDS_PIID, "value": rooms}] + ) + @command( click.argument("fan_speed", type=EnumType(FanSpeedMode)), default_output=format_output("Setting fan speed to {fan_speed}"), @@ -318,3 +527,96 @@ def set_fan_speed_preset(self, speed_preset: int) -> None: f"Invalid preset speed {speed_preset}, not in: {self.fan_speed_presets().values()}" ) return self.set_property("fan_speed", speed_preset) + + @command( + click.argument("sweep_mode", type=EnumType(SweepMode)), + default_output=format_output("Setting sweep mode to {sweep_mode}"), + ) + def set_sweep_mode(self, sweep_mode: SweepMode): + """Set the sweep mode (sweep, sweep and mop, mop).""" + return self.set_property("sweep_mode", sweep_mode.value) + + @command( + click.argument("water_level", type=EnumType(WaterLevel)), + default_output=format_output("Setting water level to {water_level}"), + ) + def set_water_level(self, water_level: WaterLevel): + """Set the water level used when mopping.""" + return self.set_property("water_level", water_level.value) + + @command( + click.argument("mop_route", type=EnumType(MopRoute)), + default_output=format_output("Setting mop route to {mop_route}"), + ) + def set_mop_route(self, mop_route: MopRoute): + """Set the mopping route (bow style or Y style).""" + return self.set_property("mop_route", mop_route.value) + + @command(click.argument("enable", type=bool)) + def set_repeat(self, enable: bool): + """Enable or disable cleaning the area twice.""" + return self.set_property("repeat_state", int(enable)) + + @command(click.argument("enable", type=bool)) + def set_tank_shake(self, enable: bool): + """Enable or disable the water tank vibration.""" + return self.set_property("tank_shake", int(enable)) + + @command(click.argument("level", type=EnumType(TankShake))) + def set_tank_shake_level(self, level: TankShake): + """Set the intensity of the water tank vibration.""" + return self.set_property("shake_shift", level.value) + + @command(click.argument("volume", type=click.IntRange(0, 10))) + def set_volume(self, volume: int): + """Set the voice volume [0, 10].""" + return self.set_property("volume", volume) + + @command() + def locate(self): + """Play the "I am here" sound (siid=4 piid=1).""" + return self.set_property_by(_ALARM_SIID, _ALARM_PLAY_PIID, 1) + + @command(click.argument("consumable", type=EnumType(Consumable))) + def reset_consumable(self, consumable: Consumable): + """Reset the lifetime counter of a consumable.""" + return self.call_action_from_mapping( + "reset_consumable", + [{"piid": _CONSUMABLE_INDEX_PIID, "value": consumable.value}], + ) + + @command( + click.argument("enable", type=bool), + click.argument("start", type=click.DateTime(["%H:%M"]), required=False), + click.argument("end", type=click.DateTime(["%H:%M"]), required=False), + ) + def set_dnd(self, enable: bool, start=None, end=None): + """Configure do-not-disturb. + + ``start`` and ``end`` are ``datetime.time`` (or ``HH:MM`` on the CLI). If they + are omitted, the currently configured times are kept. + """ + status = self.status() + start = start.time() if hasattr(start, "time") else start + end = end.time() if hasattr(end, "time") else end + start = start or status.dnd_start + end = end or status.dnd_end + return self.call_action_from_mapping( + "set_dnd", + [ + {"piid": 1, "value": int(enable)}, + {"piid": 2, "value": start.hour}, + {"piid": 3, "value": start.minute}, + {"piid": 4, "value": end.hour}, + {"piid": 5, "value": end.minute}, + {"piid": 6, "value": status.time_zone}, + ], + ) + + @command(click.argument("direction", type=EnumType(Direction))) + def manual_control(self, direction: Direction): + """Move the robot manually (remote control mode). + + Send ``Direction.Exit`` to leave the manual mode. + """ + return self.set_property_by(_SWEEP_SIID, _DIRECTION_PIID, direction.value) diff --git a/miio/integrations/ijai/vacuum/test_pro2vacuum.py b/miio/integrations/ijai/vacuum/test_pro2vacuum.py index ee936f78d..3bdb8a264 100644 --- a/miio/integrations/ijai/vacuum/test_pro2vacuum.py +++ b/miio/integrations/ijai/vacuum/test_pro2vacuum.py @@ -1,18 +1,25 @@ import datetime +import json from unittest import TestCase import pytest +from miio.exceptions import DeviceException from miio.tests.dummies import DummyMiotDevice from .pro2vacuum import ( ERROR_CODES, MI_ROBOT_VACUUM_MOP_PRO_2, + Consumable, DeviceState, + Direction, + DoorState, FanSpeedMode, + MopRoute, Pro2Vacuum, SweepMode, SweepType, + TankShake, WaterLevel, ) @@ -36,15 +43,41 @@ "current_language": "en_US", "clean_time": 5, "clean_area": 8, + "repeat_state": 0, + "door_state": 1, + "mop_route": 1, + "time_zone": 18000, + "tank_shake": 1, + "shake_shift": 2, + "volume": 7, + "dnd_enable": 1, + "dnd_start_hour": 20, + "dnd_start_minute": 30, + "dnd_end_hour": 9, + "dnd_end_minute": 0, + # map service (siid=10) + "current_map_id": 1700000002, + "map_count": 2, + "has_new_map": 0, } +_MAP_LIST = [ + {"name": "Floor 1", "id": 1700000001, "cur": False}, + {"name": "Floor 2", "id": 1700000002, "cur": True}, +] + class DummyPRO2Vacuum(DummyMiotDevice, Pro2Vacuum): def __init__(self, *args, **kwargs): self._model = MI_ROBOT_VACUUM_MOP_PRO_2 - self.state = _INITIAL_STATE_PRO2 + self.state = dict(_INITIAL_STATE_PRO2) super().__init__(*args, **kwargs) + def call_action_by(self, siid, aiid, params=None): + if (siid, aiid) == (10, 1): # get-map-list + return {"code": 0, "out": [{"piid": 4, "value": json.dumps(_MAP_LIST)}]} + return super().call_action_by(siid, aiid, params) + @pytest.fixture def dummypro2vacuum(request): @@ -93,6 +126,49 @@ def test_status(self): ) assert status.current_language == _INITIAL_STATE_PRO2["current_language"] + def test_map_status(self): + status = self.device.map_status() + assert status.current_map_id == _INITIAL_STATE_PRO2["current_map_id"] + assert status.map_count == _INITIAL_STATE_PRO2["map_count"] + assert status.has_new_map is False + + def test_get_maps(self): + maps = self.device.get_maps() + assert maps == _MAP_LIST + assert [m["id"] for m in maps if m["cur"]] == [ + _INITIAL_STATE_PRO2["current_map_id"] + ] + + def _record_actions(self): + calls = [] + self.device.call_action_by = lambda siid, aiid, params=None: calls.append( + (siid, aiid, params) + ) + return calls + + def test_start_modes(self): + calls = self._record_actions() + self.device.start_sweep() + self.device.start_sweep_mop() + self.device.start_mop() + assert calls == [(2, 3, None), (2, 5, None), (2, 6, None)] + + def test_start_rooms(self): + calls = self._record_actions() + self.device.start_rooms([10, 12]) + assert calls == [(2, 7, [{"piid": 10, "value": "10,12"}])] + + def test_start_rooms_requires_rooms(self): + calls = self._record_actions() + with pytest.raises(ValueError): + self.device.start_rooms([]) + assert calls == [] + + def test_get_maps_unexpected_response(self): + self.device.call_action_by = lambda *args, **kwargs: {"code": 0} + with pytest.raises(DeviceException): + self.device.get_maps() + def test_fanspeed_presets(self): presets = self.device.fan_speed_presets() for item in FanSpeedMode: @@ -110,3 +186,84 @@ def test_set_fan_speed(self): self.device.set_fan_speed(speed) status = self.device.status() assert status.fan_speed == FanSpeedMode(speed) + + def test_extended_status(self): + status = self.device.status() + assert status.repeat_state is False + assert status.door_state == DoorState.DustBox + assert status.mop_route == MopRoute.YStyle + assert status.time_zone == 18000 + assert status.tank_shake is True + assert status.shake_shift == TankShake.Medium + assert status.volume == 7 + assert status.dnd_enabled is True + assert status.dnd_start == datetime.time(20, 30) + assert status.dnd_end == datetime.time(9, 0) + + def test_setters(self): + self.device.set_sweep_mode(SweepMode.Mop) + self.device.set_water_level(WaterLevel.Low) + self.device.set_mop_route(MopRoute.BowStyle) + self.device.set_repeat(True) + self.device.set_tank_shake(False) + self.device.set_tank_shake_level(TankShake.High) + self.device.set_volume(3) + status = self.device.status() + assert status.sweep_mode == SweepMode.Mop + assert status.water_level == WaterLevel.Low + assert status.mop_route == MopRoute.BowStyle + assert status.repeat_state is True + assert status.tank_shake is False + assert status.shake_shift == TankShake.High + assert status.volume == 3 + + def test_volume_out_of_range(self): + # The CLI validates the range; the python API passes the value through. + self.device.set_volume(10) + assert self.device.status().volume == 10 + + def test_locate_and_manual_control(self): + calls = [] + self.device.set_property_by = lambda siid, piid, value, *a, **k: calls.append( + (siid, piid, value) + ) + self.device.locate() + self.device.manual_control(Direction.Forward) + self.device.manual_control(Direction.Exit) + assert calls == [(4, 1, 1), (7, 16, 1), (7, 16, 10)] + + def test_reset_consumable(self): + calls = self._record_actions() + self.device.reset_consumable(Consumable.Filter) + assert calls == [(7, 1, [{"piid": 17, "value": 3}])] + + def test_set_dnd(self): + calls = self._record_actions() + self.device.set_dnd(False, datetime.time(21, 15), datetime.time(7, 5)) + self.device.set_dnd(True) # keeps the configured times + assert calls == [ + ( + 12, + 1, + [ + {"piid": 1, "value": 0}, + {"piid": 2, "value": 21}, + {"piid": 3, "value": 15}, + {"piid": 4, "value": 7}, + {"piid": 5, "value": 5}, + {"piid": 6, "value": 18000}, + ], + ), + ( + 12, + 1, + [ + {"piid": 1, "value": 1}, + {"piid": 2, "value": 20}, + {"piid": 3, "value": 30}, + {"piid": 4, "value": 9}, + {"piid": 5, "value": 0}, + {"piid": 6, "value": 18000}, + ], + ), + ]