Skip to content

Commit

Permalink
Fix some minor linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
loopj committed Jun 17, 2023
1 parent 5befbe5 commit 2c62435
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 37 deletions.
69 changes: 35 additions & 34 deletions src/aiovantage/command_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ async def command(
async with self._command_lock:
# Send the request
try:
self._logger.debug(f"Sending command: {request}")
self._logger.debug("Sending command: %s", request)
self._writer.write(f"{request}\n".encode())
await self._writer.drain()
except OSError as exc:
Expand All @@ -265,20 +265,17 @@ async def command(
response = await asyncio.wait_for(
self._response_queue.get(), timeout=self._read_timeout
)
self._logger.debug(f"Received response: {response}")
self._logger.debug("Received response: %s", response)
except asyncio.TimeoutError as exc:
raise ClientTimeoutError("Timeout waiting for response") from exc

# Handle exception fetched from the queue
if isinstance(response, CommandError):
# R:ERROR responses
raise response
elif isinstance(response, (OSError, asyncio.IncompleteReadError)):
# Connection errors, or EOF when reading response
# Re-raise connection errors, EOF, etc. as a connection error
if isinstance(response, (OSError, asyncio.IncompleteReadError)):
raise ClientConnectionError("Connection error") from response
elif isinstance(response, Exception):
# Other unexpected errors
raise

# Re-raise all other exceptions, including "R:ERROR" responses
if isinstance(response, Exception):
raise response

return response

Expand All @@ -297,13 +294,13 @@ async def events(self) -> AsyncIterator[str]:
while True:
event = await queue.get()

# Handle exception fetched from the queue
# Re-raise connection errors, EOF, etc. as a connection error
if isinstance(event, (OSError, asyncio.IncompleteReadError)):
# Connection errors, or EOF when reading response
raise ClientConnectionError("Connection error") from event
elif isinstance(event, Exception):
# Other unexpected errors
raise

# Re-raise all other exceptions
if isinstance(event, Exception):
raise event

yield event
finally:
Expand All @@ -330,9 +327,9 @@ async def _message_handler(self) -> None:
self._put_event(message)
else:
self._response_buffer.append(message)
except Exception as e:
self._put_response(e, warn=False)
self._put_event(e)
except Exception as exc:
self._put_response(exc, warn=False)
self._put_event(exc)
break

# Explicitly close the connection
Expand All @@ -347,8 +344,10 @@ def _put_event(self, event: Union[str, Exception]) -> None:
if queue.full():
dropped_event = queue.get_nowait()
self._logger.warning(
f"Event queue full trying to put '{event}', dropping oldest event"
f"'{dropped_event}' to make room."
"Event queue full trying to put '%s', "
"dropping oldest event '%s' to make room.",
event,
dropped_event,
)

queue.put_nowait(event)
Expand All @@ -362,14 +361,16 @@ def _put_response(
if not self._response_queue.empty():
old_response = self._response_queue.get_nowait()
self._logger.error(
f"Response queue not empty when trying to put '{response}', "
f"dropping previous response '{old_response}'."
"Response queue not empty when trying to put '%s', "
"dropping previous response '%s'.",
response,
old_response,
)

self._response_queue.put_nowait(response)
elif warn:
self._logger.error(
f"Discarding response message, no command waiting: {response}"
"Discarding response message, no command waiting: %s", response
)


Expand Down Expand Up @@ -459,8 +460,8 @@ async def connection(self, retry: bool = False) -> CommandConnection:
await asyncio.wait_for(
self._lock.acquire(), timeout=(None if retry else self._conn_timeout)
)
except asyncio.TimeoutError:
raise ClientTimeoutError("Timeout waiting for connection")
except asyncio.TimeoutError as exc:
raise ClientTimeoutError("Timeout waiting for connection") from exc

try:
if self._connection is None or self._connection.closed:
Expand Down Expand Up @@ -745,14 +746,15 @@ async def _create_connection(self, retry: bool = False) -> CommandConnection:
reconnect_wait = min(2 * connect_attempts, 600)

self._logger.debug(
f"Connection to {self._host} failed"
f" - retrying in {reconnect_wait} seconds"
"Connection to controller failed - retrying in %d seconds",
reconnect_wait,
)

if connect_attempts % 10 == 0:
self._logger.warning(
f"{connect_attempts} attempts to (re)connect to {self._host} failed"
f" - this may indicate a problem with the connection"
"%d attempts to (re)connect to controller failed"
" - This might be an indication of connection issues.",
connect_attempts,
)

await asyncio.sleep(reconnect_wait)
Expand Down Expand Up @@ -799,20 +801,19 @@ async def _event_handler(self) -> None:

# Start processing events
async for event in conn.events():
self._logger.debug(f"Received event: {event}")
self._logger.debug("Received event: %s", event)

if event.startswith("S:"):
# Parse a status message
status_type, id_str, *args = tokenize_response(event)
status_type = status_type[2:]
id = int(id_str)

# Notify subscribers
self._emit(
{
"tag": EventType.STATUS,
"status_type": status_type,
"id": id,
"id": int(id_str),
"args": args,
},
)
Expand All @@ -828,7 +829,7 @@ async def _event_handler(self) -> None:
},
)
else:
self._logger.warning(f"Received unexpected event: {event}")
self._logger.warning("Received unexpected event: %s", event)

except ClientConnectionError:
pass
Expand Down
2 changes: 1 addition & 1 deletion src/aiovantage/command_client/interfaces/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ def parse_get_level_status(cls, args: Sequence[str]) -> int:

# STATUS ADD <id>
# -> S:STATUS <id> Sensor.GetLevel <level>
return int(args[0])
return int(args[0])
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ class ObjectChoice:
for cls in ALL_OBJECT_TYPES
],
},
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from aiovantage.config_client.xml_dataclass import xml_element


@dataclass
class GetVersion:
interface: ClassVar[str] = "IIntrospection"
Expand Down
3 changes: 2 additions & 1 deletion src/aiovantage/config_client/methods/login/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from aiovantage.config_client.xml_dataclass import xml_element


@dataclass
class Login:
interface: ClassVar[str] = "ILogin"
Expand All @@ -12,4 +13,4 @@ class Login:
@dataclass
class Params:
user: str = xml_element("User")
password: str = xml_element("Password")
password: str = xml_element("Password")
1 change: 1 addition & 0 deletions src/aiovantage/config_client/objects/blind.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .location_object import LocationObject


@dataclass
class Blind(LocationObject):
parent_id: Optional[int] = xml_element("Parent", default=None)
Expand Down
1 change: 1 addition & 0 deletions src/aiovantage/config_client/objects/gmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from .system_object import SystemObject


@dataclass
class GMem(SystemObject):
@dataclass
Expand Down
2 changes: 2 additions & 0 deletions src/aiovantage/config_client/xml_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ def xml_element(name: str, **kwargs: Any) -> Any:

return field(metadata=metadata, **kwargs)


def xml_text(**kwargs: Any) -> Any:
metadata = {}
metadata.update(kwargs.pop("metadata", {}))
metadata.update({"type": "Text"})

return field(metadata=metadata, **kwargs)


def xml_tag_from_class(cls: Type[Any]) -> str:
"""Get the XML tag name for a class."""

Expand Down

0 comments on commit 2c62435

Please sign in to comment.