From beb66a4e7afd818d2c2a21b2e5dc79cb96bd8f5c Mon Sep 17 00:00:00 2001 From: Slawomir Skowron Date: Tue, 7 May 2024 15:10:11 +0200 Subject: [PATCH] Fix all checks to pass (#29) * First part of fixes for checks to pass * Second part of fixes for checks to pass * Third part of fixes for checks to pass * 3.11 - remove Text and replace by str * Add python 3.12 tests * Revert "Add python 3.12 tests" This reverts commit 9ce60cb9f4d466ecd05c52d5d4003dfcd1de91f0. --- aiocomfoconnect/__main__.py | 27 +++++++++++++++------------ aiocomfoconnect/bridge.py | 19 ++++++++++++------- aiocomfoconnect/comfoconnect.py | 5 +++-- aiocomfoconnect/const.py | 2 ++ aiocomfoconnect/discovery.py | 5 +++-- aiocomfoconnect/exceptions.py | 9 +++++++++ aiocomfoconnect/properties.py | 1 + aiocomfoconnect/sensors.py | 1 + aiocomfoconnect/util.py | 1 + 9 files changed, 47 insertions(+), 23 deletions(-) diff --git a/aiocomfoconnect/__main__.py b/aiocomfoconnect/__main__.py index ad979bd..899ae67 100644 --- a/aiocomfoconnect/__main__.py +++ b/aiocomfoconnect/__main__.py @@ -1,4 +1,5 @@ """ aiocomfoconnect CLI application """ + from __future__ import annotations import argparse @@ -14,7 +15,9 @@ from aiocomfoconnect.exceptions import ( AioComfoConnectNotConnected, AioComfoConnectTimeout, + BridgeNotFoundException, ComfoConnectNotAllowed, + UnknownActionException, ) from aiocomfoconnect.properties import Property from aiocomfoconnect.sensors import SENSORS @@ -61,7 +64,7 @@ async def main(args): await run_set_flow_for_speed(args.host, args.uuid, args.speed, args.flow) else: - raise Exception("Unknown action: " + args.action) + raise UnknownActionException("Unknown action: " + args.action) async def run_discover(host: str = None): @@ -78,7 +81,7 @@ async def run_register(host: str, uuid: str, name: str, pin: int): # Discover bridge so we know the UUID bridges = await discover_bridges(host) if not bridges: - raise Exception("No bridge found") + raise BridgeNotFoundException("No bridge found") # Connect to the bridge comfoconnect = ComfoConnect(bridges[0].host, bridges[0].uuid) @@ -117,7 +120,7 @@ async def run_deregister(host: str, uuid: str, uuid2: str): # Discover bridge so we know the UUID bridges = await discover_bridges(host) if not bridges: - raise Exception("No bridge found") + raise BridgeNotFoundException("No bridge found") # Connect to the bridge comfoconnect = ComfoConnect(bridges[0].host, bridges[0].uuid) @@ -145,7 +148,7 @@ async def run_set_speed(host: str, uuid: str, speed: Literal["away", "low", "med # Discover bridge so we know the UUID bridges = await discover_bridges(host) if not bridges: - raise Exception("No bridge found") + raise BridgeNotFoundException("No bridge found") # Connect to the bridge comfoconnect = ComfoConnect(bridges[0].host, bridges[0].uuid) @@ -165,7 +168,7 @@ async def run_set_mode(host: str, uuid: str, mode: Literal["auto", "manual"]): # Discover bridge so we know the UUID bridges = await discover_bridges(host) if not bridges: - raise Exception("No bridge found") + raise BridgeNotFoundException("No bridge found") # Connect to the bridge comfoconnect = ComfoConnect(bridges[0].host, bridges[0].uuid) @@ -185,7 +188,7 @@ async def run_set_comfocool(host: str, uuid: str, mode: Literal["auto", "off"]): # Discover bridge so we know the UUID bridges = await discover_bridges(host) if not bridges: - raise Exception("No bridge found") + raise BridgeNotFoundException("No bridge found") # Connect to the bridge comfoconnect = ComfoConnect(bridges[0].host, bridges[0].uuid) @@ -205,7 +208,7 @@ async def run_set_boost(host: str, uuid: str, mode: Literal["on", "off"], timeou # Discover bridge so we know the UUID bridges = await discover_bridges(host) if not bridges: - raise Exception("No bridge found") + raise BridgeNotFoundException("No bridge found") # Connect to the bridge comfoconnect = ComfoConnect(bridges[0].host, bridges[0].uuid) @@ -225,7 +228,7 @@ async def run_show_sensors(host: str, uuid: str): # Discover bridge so we know the UUID bridges = await discover_bridges(host) if not bridges: - raise Exception("No bridge found") + raise BridgeNotFoundException("No bridge found") def alarm_callback(node_id, errors): """Print alarm updates.""" @@ -277,7 +280,7 @@ async def run_show_sensor(host: str, uuid: str, sensor: int, follow=False): # Discover bridge so we know the UUID bridges = await discover_bridges(host) if not bridges: - raise Exception("No bridge found") + raise BridgeNotFoundException("No bridge found") def sensor_callback(sensor_, value): """Print sensor update.""" @@ -331,7 +334,7 @@ async def run_get_property(host: str, uuid: str, node_id: int, unit: int, subuni # Discover bridge so we know the UUID bridges = await discover_bridges(host) if not bridges: - raise Exception("No bridge found") + raise BridgeNotFoundException("No bridge found") # Connect to the bridge comfoconnect = ComfoConnect(bridges[0].host, bridges[0].uuid) @@ -351,7 +354,7 @@ async def run_get_flow_for_speed(host: str, uuid: str, speed: Literal["away", "l # Discover bridge so we know the UUID bridges = await discover_bridges(host) if not bridges: - raise Exception("No bridge found") + raise BridgeNotFoundException("No bridge found") # Connect to the bridge comfoconnect = ComfoConnect(bridges[0].host, bridges[0].uuid) @@ -371,7 +374,7 @@ async def run_set_flow_for_speed(host: str, uuid: str, speed: Literal["away", "l # Discover bridge so we know the UUID bridges = await discover_bridges(host) if not bridges: - raise Exception("No bridge found") + raise BridgeNotFoundException("No bridge found") # Connect to the bridge comfoconnect = ComfoConnect(bridges[0].host, bridges[0].uuid) diff --git a/aiocomfoconnect/bridge.py b/aiocomfoconnect/bridge.py index 1a7879c..25e3e5f 100644 --- a/aiocomfoconnect/bridge.py +++ b/aiocomfoconnect/bridge.py @@ -1,4 +1,5 @@ """ ComfoConnect Bridge API """ + from __future__ import annotations import asyncio @@ -30,6 +31,10 @@ TIMEOUT = 5 +class SelfDeregistrationError(Exception): + """Exception raised when trying to deregister self.""" + + class EventBus: """An event bus for async replies.""" @@ -95,7 +100,7 @@ async def _connect(self, uuid: str): self._reader, self._writer = await asyncio.wait_for(asyncio.open_connection(self.host, self.PORT), TIMEOUT) except asyncio.TimeoutError as exc: _LOGGER.warning("Timeout while connecting to bridge %s", self.host) - raise AioComfoConnectTimeout from exc + raise AioComfoConnectTimeout("Timeout while connecting to bridge") from exc self._reference = 1 self._local_uuid = uuid @@ -111,9 +116,9 @@ async def _read_messages(): # We are shutting down. Return to stop the background task return False - except AioComfoConnectNotConnected: + except AioComfoConnectNotConnected as exc: # We have been disconnected - raise + raise AioComfoConnectNotConnected("We have been disconnected") from exc read_task = self._loop.create_task(_read_messages()) _LOGGER.debug("Connected to bridge %s", self.host) @@ -169,7 +174,7 @@ async def _send(self, request, request_type, params: dict = None, reply: bool = except asyncio.TimeoutError as exc: _LOGGER.warning("Timeout while waiting for response from bridge") await self._disconnect() - raise AioComfoConnectTimeout from exc + raise AioComfoConnectTimeout("Timeout while waiting for response from bridge") from exc async def _read(self) -> Message: # Read packet size @@ -241,10 +246,10 @@ async def _process_message(self): else: _LOGGER.warning("Unhandled message type %s: %s", message.cmd.type, message) - except asyncio.exceptions.IncompleteReadError: + except asyncio.exceptions.IncompleteReadError as exc: _LOGGER.info("The connection was closed.") await self._disconnect() - raise AioComfoConnectNotConnected + raise AioComfoConnectNotConnected("The connection was closed.") from exc except ComfoConnectError as exc: if exc.message.cmd.reference: @@ -300,7 +305,7 @@ def cmd_deregister_app(self, uuid: str) -> Awaitable[Message]: """Remove the specified app from the registration list.""" _LOGGER.debug("DeregisterAppRequest") if uuid == self._local_uuid: - raise Exception("You should not deregister yourself.") + raise SelfDeregistrationError("You should not deregister yourself.") # pylint: disable=no-member return self._send( diff --git a/aiocomfoconnect/comfoconnect.py b/aiocomfoconnect/comfoconnect.py index c807168..74ce8e2 100644 --- a/aiocomfoconnect/comfoconnect.py +++ b/aiocomfoconnect/comfoconnect.py @@ -1,4 +1,5 @@ """ ComfoConnect Bridge API abstraction """ + from __future__ import annotations import asyncio @@ -21,13 +22,13 @@ UNIT_SCHEDULE, UNIT_TEMPHUMCONTROL, UNIT_VENTILATIONCONFIG, + ComfoCoolMode, PdoType, VentilationBalance, VentilationMode, VentilationSetting, VentilationSpeed, VentilationTemperatureProfile, - ComfoCoolMode, ) from aiocomfoconnect.exceptions import ( AioComfoConnectNotConnected, @@ -111,7 +112,6 @@ async def _reconnect_loop(): except AioComfoConnectNotConnected: # Reconnect when connection has been dropped _LOGGER.info("We got disconnected. Reconnecting.") - pass reconnect_task = self._loop.create_task(_reconnect_loop()) self._tasks.add(reconnect_task) @@ -120,6 +120,7 @@ async def _reconnect_loop(): await connected async def disconnect(self): + """Disconnect from the bridge.""" await self._disconnect() async def register_sensor(self, sensor: Sensor): diff --git a/aiocomfoconnect/const.py b/aiocomfoconnect/const.py index 536f3e0..b3c2d51 100644 --- a/aiocomfoconnect/const.py +++ b/aiocomfoconnect/const.py @@ -1,5 +1,6 @@ """ Constants """ + # PDO Types class PdoType: """Defines a PDO type.""" @@ -208,6 +209,7 @@ class VentilationSpeed: MEDIUM = "medium" HIGH = "high" + class ComfoCoolMode: """Enum for ventilation settings.""" diff --git a/aiocomfoconnect/discovery.py b/aiocomfoconnect/discovery.py index 4d5b7a4..c16541b 100644 --- a/aiocomfoconnect/discovery.py +++ b/aiocomfoconnect/discovery.py @@ -1,9 +1,10 @@ """ Bridge discovery """ + from __future__ import annotations import asyncio import logging -from typing import Any, List, Text, Union +from typing import Any, List, Union from .bridge import Bridge from .protobuf import zehnder_pb2 @@ -35,7 +36,7 @@ def connection_made(self, transport: asyncio.transports.DatagramTransport): _LOGGER.debug("Sending discovery request to broadcast:%d", Bridge.PORT) self.transport.sendto(b"\x0a\x00", ("", Bridge.PORT)) - def datagram_received(self, data: Union[bytes, Text], addr: tuple[str | Any, int]): + def datagram_received(self, data: Union[bytes, str], addr: tuple[str | Any, int]): """Called when some datagram is received.""" if data == b"\x0a\x00": _LOGGER.debug("Ignoring discovery request from %s:%d", addr[0], addr[1]) diff --git a/aiocomfoconnect/exceptions.py b/aiocomfoconnect/exceptions.py index 1803c16..e3863c9 100644 --- a/aiocomfoconnect/exceptions.py +++ b/aiocomfoconnect/exceptions.py @@ -1,4 +1,5 @@ """ Error definitions """ + from __future__ import annotations @@ -47,3 +48,11 @@ class AioComfoConnectNotConnected(Exception): class AioComfoConnectTimeout(Exception): """An error occurred because the bridge didn't reply in time.""" + + +class BridgeNotFoundException(Exception): + """Exception raised when no bridge is found.""" + + +class UnknownActionException(Exception): + """Exception raised when an unknown action is provided.""" diff --git a/aiocomfoconnect/properties.py b/aiocomfoconnect/properties.py index 1f96ea3..02b74e3 100644 --- a/aiocomfoconnect/properties.py +++ b/aiocomfoconnect/properties.py @@ -1,4 +1,5 @@ """ Property definitions """ + from __future__ import annotations from dataclasses import dataclass diff --git a/aiocomfoconnect/sensors.py b/aiocomfoconnect/sensors.py index bd22cda..32132de 100644 --- a/aiocomfoconnect/sensors.py +++ b/aiocomfoconnect/sensors.py @@ -1,4 +1,5 @@ """ Sensor definitions. """ + from __future__ import annotations from dataclasses import dataclass diff --git a/aiocomfoconnect/util.py b/aiocomfoconnect/util.py index 8af9104..1c19424 100644 --- a/aiocomfoconnect/util.py +++ b/aiocomfoconnect/util.py @@ -1,4 +1,5 @@ """ Helper methods. """ + from __future__ import annotations from aiocomfoconnect.const import PdoType