Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mirobo: make sure config always exists #1207

Merged
merged 2 commits into from
Dec 3, 2021
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
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