Skip to content

Commit

Permalink
Implement some more commands
Browse files Browse the repository at this point in the history
Commands added:
* --version to output the library version
* info: returns generic information about devices, e.g., its model, mac, IP and firmware version
* serial_number
* timezone: to get and set the timezone
* sound: querying current voice settings

Thanks https://github.com/marcelrv/XiaomiRobotVacuumProtocol and some forum posts.
  • Loading branch information
rytilahti committed Aug 1, 2017
1 parent 3f4ee1c commit 5af95d1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
26 changes: 26 additions & 0 deletions mirobo/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ class DeviceException(Exception):
pass


class DeviceInfo:
def __init__(self, data):
self.data = data

def __repr__(self):
return "%s v%s (%s) @ %s - token: %s" % (self.data["model"],
self.data["fw_ver"],
self.data["mac"],
self.netif["localIp"],
self.data["token"])
@property
def netif(self):
return self.data["netif"]

@property
def ap(self):
return self.data["ap"]

@property
def raw(self):
return self.data


class Device:
def __init__(self, ip: str, token: str,
start_id: int=0, debug: int=0) -> None:
Expand Down Expand Up @@ -144,6 +167,9 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any:
return self.send(command, parameters, retry_count-1)
raise DeviceException from ex

def info(self):
return DeviceInfo(self.send("miIO.info", []))

@property
def _id(self) -> int:
"""Returns running id."""
Expand Down
30 changes: 28 additions & 2 deletions mirobo/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import time
from typing import List

from .containers import (VacuumStatus, ConsumableStatus,
CleaningSummary, CleaningDetails, Timer)
from .vacuumcontainers import (VacuumStatus, ConsumableStatus,
CleaningSummary, CleaningDetails, Timer)
from .device import Device, DeviceException

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -86,6 +86,10 @@ def status(self) -> VacuumStatus:
"""Return status of the vacuum."""
return VacuumStatus(self.send("get_status")[0])

def enable_log_upload(self):
raise NotImplementedError("unknown parameters")
return self.send("enable_log_upload")

def log_upload_status(self):
# {"result": [{"log_upload_status": 7}], "id": 1}
return self.send("get_log_upload_status")
Expand All @@ -94,6 +98,11 @@ def consumable_status(self) -> ConsumableStatus:
"""Return information about consumables."""
return ConsumableStatus(self.send("get_consumable")[0])

def consumable_reset(self):
"""Reset consumable information."""
raise NotImplementedError()
self.send("reset_consumable", ["unknown"])

def map(self):
"""Return map token."""
# returns ['retry'] without internet
Expand Down Expand Up @@ -129,6 +138,7 @@ def set_timer(self, details):
# how to create timers/change values?
# ['ts', 'on'] to enable
raise NotImplementedError()
return self.send("set_timer", [["ts",["cron_line",["start_clean",""]]]])
return self.send("upd_timer", ["ts", "on"])

def dnd_status(self):
Expand Down Expand Up @@ -156,6 +166,22 @@ def fan_speed(self):
"""Return fan speed."""
return self.send("get_custom_mode")[0]

def sound_info(self):
"""Get voice settings."""
return self.send("get_current_sound")

def serial_number(self):
"""Get serial number."""
return self.send("get_serial_number")[0]["serial_number"]

def timezone(self):
"""Get the timezone."""
return self.send("get_timezone")[0]

def set_timezone(self, new_zone):
"""Set the timezone."""
return self.send("set_timezone", [new_zone])[0] == 'ok'

def raw_command(self, cmd, params):
"""Send a raw command to the robot."""
return self.send(cmd, params)
34 changes: 34 additions & 0 deletions mirobo/vacuum_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import json
import ipaddress
from pprint import pformat as pf
from typing import Any

if sys.version_info < (3, 4):
Expand Down Expand Up @@ -40,6 +41,7 @@ def validate_token(ctx, param, value):
@click.option('-d', '--debug', default=False, count=True)
@click.option('--id-file', type=click.Path(dir_okay=False, writable=True),
default='/tmp/python-mirobo.seq')
@click.version_option()
@click.pass_context
def cli(ctx, ip: str, token: str, debug: int, id_file: str):
"""A tool to command Xiaomi Vacuum robot."""
Expand Down Expand Up @@ -320,6 +322,15 @@ def map(vac: mirobo.Vacuum):
click.echo(vac.map())


@cli.command()
@pass_dev
def info(vac: mirobo.Vacuum):
"""Returns info"""
res = vac.info()

click.echo(res)
_LOGGER.debug("Full response: %s" % pf(res.raw))

@cli.command()
@pass_dev
def cleaning_history(vac: mirobo.Vacuum):
Expand All @@ -341,6 +352,29 @@ def cleaning_history(vac: mirobo.Vacuum):
click.echo()


@cli.command()
@pass_dev
def sound(vac: mirobo.Vacuum):
"""Query sound settings."""
click.echo(vac.sound_info())

@cli.command()
@pass_dev
def serial_number(vac: mirobo.Vacuum):
"""Query serial number."""
click.echo("Serial#: %s" % vac.serial_number())

@cli.command()
@click.argument('tz', required=False)
@pass_dev
def timezone(vac: mirobo.Vacuum, tz=None):
"""Query or set the timezone."""
if tz is not None:
click.echo("Setting timezone to: %s" % tz)
click.echo(vac.set_timezone(tz))
else:
click.echo("Timezone: %s" % vac.timezone())

@cli.command()
@click.argument('cmd', required=True)
@click.argument('parameters', required=False)
Expand Down

0 comments on commit 5af95d1

Please sign in to comment.