Skip to content

Commit

Permalink
mirobo: make sure config always exists (#1207)
Browse files Browse the repository at this point in the history
* mirobo: make sure config always exists

* Add test
  • Loading branch information
rytilahti authored Dec 3, 2021
1 parent 1f3da25 commit f443710
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
14 changes: 14 additions & 0 deletions miio/integrations/vacuum/roborock/tests/test_mirobo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from click.testing import CliRunner

from ..vacuum_cli import cli


def test_config_read(mocker):
"""Make sure config file is being read."""
x = mocker.patch("miio.integrations.vacuum.roborock.vacuum_cli._read_config")
runner = CliRunner()
runner.invoke(
cli, ["--ip", "127.0.0.1", "--token", "ffffffffffffffffffffffffffffffff"]
)

x.assert_called()
23 changes: 16 additions & 7 deletions miio/integrations/vacuum/roborock/vacuum_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@
pass_dev = click.make_pass_decorator(Device, ensure=True)


def _read_config(file):
"""Return sequence id information."""
config = {"seq": 0, "manual_seq": 0}
with contextlib.suppress(FileNotFoundError, TypeError, ValueError), open(
file, "r"
) as f:
config = json.load(f)

return config


@click.group(invoke_without_command=True, cls=ExceptionHandlerGroup)
@click.option("--ip", envvar="MIROBO_IP", callback=validate_ip)
@click.option("--token", envvar="MIROBO_TOKEN", callback=validate_token)
Expand Down Expand Up @@ -61,13 +72,11 @@ def cli(ctx, ip: str, token: str, debug: int, id_file: str):
click.echo("You have to give ip and token!")
sys.exit(-1)

with contextlib.suppress(FileNotFoundError, TypeError, ValueError), open(
id_file, "r"
) as f:
x = json.load(f)
start_id = x.get("seq", 0)
manual_seq = x.get("manual_seq", 0)
_LOGGER.debug("Read stored sequence ids: %s", x)
config = _read_config(id_file)

start_id = config["seq"]
manual_seq = config["manual_seq"]
_LOGGER.debug("Using config: %s", config)

vac = RoborockVacuum(ip, token, start_id, debug)

Expand Down

0 comments on commit f443710

Please sign in to comment.