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

Fix EM* FBT003 TRY* #252

Merged
merged 2 commits into from
Feb 21, 2024
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
7 changes: 0 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,7 @@ ignore = [
"D203", # Conflicts with other rules
"D213", # Conflicts with other rules
"DTZ005",
"EM101",
"EM102",
"FBT002",
"FBT003",
"TRY002",
"TRY003",
"TRY300",
"TRY400",
"COM812", # Conflicts with other rules
"ISC001", # Conflicts with other rules
"PLR2004", # Just annoying, not really useful
Expand Down
17 changes: 13 additions & 4 deletions roombapy/entry_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import sys

from roombapy import RoombaFactory
from roombapy import RoombaConnectionError, RoombaFactory
from roombapy.discovery import RoombaDiscovery
from roombapy.getpassword import RoombaPassword

Expand Down Expand Up @@ -61,19 +61,28 @@ def connect():
pass


class ValidationError(Exception):
"""Validation error."""

def __init__(self, *, field: str) -> None:
"""Initialize the exception."""
super().__init__(f"{field} cannot be null")


def _validate_ip(ip):
if ip is None:
raise Exception("ip cannot be null")
raise ValidationError(field="IP")


def _validate_password(ip):
if ip is None:
raise Exception("password cannot be null")
raise ValidationError(field="Password")


def _validate_roomba_info(roomba_info):
if roomba_info is None:
raise Exception("cannot find roomba")
msg = "cannot find roomba"
raise RoombaConnectionError(msg)


def _wait_for_input():
Expand Down
3 changes: 2 additions & 1 deletion roombapy/getpassword.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ def _get_response(self):
if len(raw_data) >= 2:
response_length = struct.unpack("B", raw_data[1:2])[0]
self.server_socket.close()
return raw_data
except socket.timeout:
self.log.warning("Socket timeout")
return None
except OSError as e:
self.log.debug("Socket error: %s", e)
return None
else:
return raw_data


def _decode_password(data):
Expand Down
9 changes: 4 additions & 5 deletions roombapy/remote_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ def connect(self):
)
try:
self._open_mqtt_connection()
except Exception:
self.log.exception("Can't connect to %s", self.address)
else:
return True
except Exception as e: # noqa: BLE001
self.log.error(
"Can't connect to %s, error: %s", self.address, e
)
attempt += 1

self.log.error("Unable to connect to %s", self.address)
Expand Down Expand Up @@ -121,7 +120,7 @@ def _get_mqtt_client(self):
self.log.debug("Setting TLS certificate")
ssl_context = generate_tls_context()
mqtt_client.tls_set_context(ssl_context)
mqtt_client.tls_insecure_set(True)
mqtt_client.tls_insecure_set(True) # noqa: FBT003

return mqtt_client

Expand Down
5 changes: 2 additions & 3 deletions roombapy/roomba.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ def connect(self):
def _connect(self):
is_connected = self.remote_client.connect()
if not is_connected:
raise RoombaConnectionError(
f"Unable to connect to Roomba at {self.remote_client.address}"
)
msg = f"Unable to connect to Roomba at {self.remote_client.address}"
raise RoombaConnectionError(msg)
return is_connected

def disconnect(self):
Expand Down
9 changes: 6 additions & 3 deletions roombapy/roomba_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ class RoombaInfo(BaseModel):
def hostname_validator(cls, value: str) -> str:
"""Validate the hostname."""
if "-" not in value:
raise ValueError(f"hostname does not contain a dash: {value}")
msg = f"hostname does not contain a dash: {value}"
raise ValueError(msg)
model_name, blid = value.split("-")
if blid == "":
raise ValueError(f"empty blid: {value}")
msg = f"empty blid: {value}"
raise ValueError(msg)
if model_name.lower() not in {"roomba", "irobot"}:
raise ValueError(f"unsupported model in hostname: {value}")
msg = f"unsupported model in hostname: {value}"
raise ValueError(msg)
return value

@cached_property
Expand Down