diff --git a/pyproject.toml b/pyproject.toml index ef8d0c4..f39c41c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 diff --git a/roombapy/entry_points.py b/roombapy/entry_points.py index 7439e64..09c02a4 100644 --- a/roombapy/entry_points.py +++ b/roombapy/entry_points.py @@ -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 @@ -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(): diff --git a/roombapy/getpassword.py b/roombapy/getpassword.py index 43434ad..4946fd2 100644 --- a/roombapy/getpassword.py +++ b/roombapy/getpassword.py @@ -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): diff --git a/roombapy/remote_client.py b/roombapy/remote_client.py index d4a55cf..aaa4e45 100644 --- a/roombapy/remote_client.py +++ b/roombapy/remote_client.py @@ -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) @@ -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 diff --git a/roombapy/roomba.py b/roombapy/roomba.py index 25d75d6..a347661 100755 --- a/roombapy/roomba.py +++ b/roombapy/roomba.py @@ -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): diff --git a/roombapy/roomba_info.py b/roombapy/roomba_info.py index 6e60bb7..24aa4ed 100644 --- a/roombapy/roomba_info.py +++ b/roombapy/roomba_info.py @@ -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