Skip to content

Commit

Permalink
make flake8 happy, fix some issues reported by landscape
Browse files Browse the repository at this point in the history
  • Loading branch information
rytilahti committed Sep 6, 2017
1 parent b4e1945 commit 9424964
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 43 deletions.
41 changes: 21 additions & 20 deletions mirobo/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datetime
import socket
import logging
from typing import Any, List
from typing import Any, List # noqa: F401

from .protocol import Message

Expand Down Expand Up @@ -63,11 +63,11 @@ def do_discover(self):
self._device_ts = m.header.value.ts
if self.debug > 1:
_LOGGER.debug(m)
_LOGGER.debug("Discovered %s %s with ts: %s, token: %s" % (
self._devtype,
self._serial,
self._device_ts,
codecs.encode(m.checksum, 'hex')))
_LOGGER.debug("Discovered %s %s with ts: %s, token: %s",
self._devtype,
self._serial,
self._device_ts,
codecs.encode(m.checksum, 'hex'))
else:
_LOGGER.error("Unable to discover a device at address %s", self.ip)
raise DeviceException("Unable to discover the device %s" % self.ip)
Expand Down Expand Up @@ -98,15 +98,15 @@ def discover(addr: str=None) -> Any:
try:
data, addr = s.recvfrom(1024)
m = Message.parse(data) # type: Message
_LOGGER.debug("Got a response: %s" % m)
_LOGGER.debug("Got a response: %s", m)
if not is_broadcast:
return m

if addr[0] not in seen_addrs:
_LOGGER.info(" IP %s: %s - token: %s" % (
addr[0],
m.header.value.devtype,
codecs.encode(m.checksum, 'hex')))
_LOGGER.info(" IP %s: %s - token: %s",
addr[0],
m.header.value.devtype,
codecs.encode(m.checksum, 'hex'))
seen_addrs.append(addr[0])
except socket.timeout:
if is_broadcast:
Expand Down Expand Up @@ -138,7 +138,7 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any:
'checksum': 0}
ctx = {'token': self.token}
m = Message.build(msg, ctx)
_LOGGER.debug("%s:%s >>: %s" % (self.ip, self.port, cmd))
_LOGGER.debug("%s:%s >>: %s", self.ip, self.port, cmd)
if self.debug > 1:
_LOGGER.debug("send (timeout %s): %s",
self._timeout, Message.parse(m, ctx))
Expand All @@ -149,21 +149,22 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any:
try:
s.sendto(m, (self.ip, self.port))
except OSError as ex:
_LOGGER.error("failed to send msg: %s" % ex)
_LOGGER.error("failed to send msg: %s", ex)
raise DeviceException from ex

try:
data, addr = s.recvfrom(1024)
m = Message.parse(data, ctx)
self._device_ts = m.header.value.ts
if self.debug > 1:
_LOGGER.debug("recv: %s" % m)
_LOGGER.debug("recv: %s", m)

self.__id = m.data.value["id"]
_LOGGER.debug("%s:%s (ts: %s, id: %s) << %s" % (self.ip, self.port,
m.header.value.ts,
m.data.value["id"],
m.data.value))
_LOGGER.debug("%s:%s (ts: %s, id: %s) << %s",
self.ip, self.port,
m.header.value.ts,
m.data.value["id"],
m.data.value)
try:
return m.data.value["result"]
except KeyError:
Expand All @@ -172,9 +173,9 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any:
_LOGGER.error("Got error when receiving: %s", ex)
if retry_count > 0:
_LOGGER.warning("Retrying with incremented id, "
"retries left: %s" % retry_count)
"retries left: %s", retry_count)
self.__id += 100
return self.send(command, parameters, retry_count-1)
return self.send(command, parameters, retry_count - 1)
raise DeviceException from ex

def raw_command(self, cmd, params):
Expand Down
14 changes: 8 additions & 6 deletions mirobo/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import codecs
from mirobo import (Device, Vacuum, Plug, PlugV1, Strip, AirPurifier, Ceil,
PhilipsEyecare, ChuangmiIr)
from typing import Union, Callable, Dict
from typing import Union, Callable, Dict # noqa: F401


_LOGGER = logging.getLogger(__name__)
Expand All @@ -31,18 +31,20 @@ def _check_if_supported(self, info, addr):
m = dev.do_discover()
dev.token = m.checksum
_LOGGER.info(
"Found supported '%s' at %s:%s (%s) token: %s" %
(v.__name__, addr, info.port, name,
codecs.encode(dev.token, 'hex')))
"Found supported '%s' at %s:%s (%s) token: %s",
v.__name__,
addr, info.port,
name,
codecs.encode(dev.token, 'hex'))
return dev
elif callable(v):
_LOGGER.info(v(info))
dev = Device(ip=addr)
_LOGGER.info("token: %s" % codecs.encode(
_LOGGER.info("token: %s", codecs.encode(
dev.do_discover().checksum, 'hex'))
return None
_LOGGER.warning("Found unsupported device %s at %s, "
"please report to developers" % (name, addr))
"please report to developers", name, addr)
return None

def add_service(self, zeroconf, type, name):
Expand Down
2 changes: 1 addition & 1 deletion mirobo/plug_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import ast
import sys
import ipaddress
from typing import Any
from typing import Any # noqa: F401

if sys.version_info < (3, 4):
print("To use this script you need python 3.4 or newer, got %s" %
Expand Down
2 changes: 1 addition & 1 deletion mirobo/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _decode(self, obj, context):
# pp(context)
decrypted = Utils.decrypt(obj, context['_']['token'])
decrypted = decrypted.rstrip(b"\x00")
except Exception as ex:
except Exception:
_LOGGER.debug("Unable to decrypt, returning raw bytes.")
return obj

Expand Down
12 changes: 6 additions & 6 deletions mirobo/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def status(self) -> VacuumStatus:

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

def log_upload_status(self):
# {"result": [{"log_upload_status": 7}], "id": 1}
Expand All @@ -105,8 +105,8 @@ def consumable_status(self) -> ConsumableStatus:

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

def map(self):
"""Return map token."""
Expand Down Expand Up @@ -143,9 +143,9 @@ 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"])
# return self.send(
# "set_timer", [["ts", ["cron_line", ["start_clean", ""]]]])
# return self.send("upd_timer", ["ts", "on"])

def dnd_status(self):
"""Returns do-not-disturb status."""
Expand Down
16 changes: 7 additions & 9 deletions mirobo/vacuum_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import ipaddress
from pprint import pformat as pf
from typing import Any
from typing import Any # noqa: F401


if sys.version_info < (3, 4):
Expand Down Expand Up @@ -71,10 +71,9 @@ def cli(ctx, ip: str, token: str, debug: int, id_file: str):
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)
_LOGGER.debug("Read stored sequence ids: %s", x)
except (FileNotFoundError, TypeError) as ex:
_LOGGER.error("Unable to read the stored msgid: %s" % ex)
pass
_LOGGER.error("Unable to read the stored msgid: %s", ex)

vac = mirobo.Vacuum(ip, token, start_id, debug)

Expand All @@ -95,7 +94,7 @@ def cleanup(vac: mirobo.Vacuum, **kwargs):
return
id_file = kwargs['id_file']
seqs = {'seq': vac.raw_id, 'manual_seq': vac.manual_seqnum}
_LOGGER.debug("Writing %s to %s" % (seqs, id_file))
_LOGGER.debug("Writing %s to %s", seqs, id_file)
with open(id_file, 'w') as f:
json.dump(seqs, f)

Expand Down Expand Up @@ -195,15 +194,15 @@ def manual(vac: mirobo.Vacuum):
# if not vac.manual_mode and command :


@manual.command()
@manual.command() # noqa: F811 # redefinition of start
@pass_dev
def start(vac: mirobo.Vacuum):
"""Activate the manual mode."""
click.echo("Activating manual controls")
return vac.manual_start()


@manual.command()
@manual.command() # noqa: F811 # redefinition of stop
@pass_dev
def stop(vac: mirobo.Vacuum):
"""Deactivate the manual mode."""
Expand Down Expand Up @@ -303,7 +302,6 @@ def timer(vac: mirobo.Vacuum, timer):
if timer:
raise NotImplementedError()
# vac.set_timer(x)
pass
else:
timers = vac.timer()
for idx, timer in enumerate(timers):
Expand Down Expand Up @@ -341,7 +339,7 @@ def info(vac: mirobo.Vacuum):
res = vac.info()

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


@cli.command()
Expand Down

0 comments on commit 9424964

Please sign in to comment.