From c4d5a7992f508923bc8bb7a0398074cf935a0332 Mon Sep 17 00:00:00 2001 From: ArnoutD Date: Sat, 24 May 2025 11:29:27 +0200 Subject: [PATCH] define constants for reused errors --- plugwise_usb/__init__.py | 16 +++++---------- plugwise_usb/messages/properties.py | 31 +++++++++++++++-------------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/plugwise_usb/__init__.py b/plugwise_usb/__init__.py index e375e79a3..c69fce361 100644 --- a/plugwise_usb/__init__.py +++ b/plugwise_usb/__init__.py @@ -10,7 +10,7 @@ from collections.abc import Callable, Coroutine from functools import wraps import logging -from typing import Any, TypeVar, cast +from typing import Any, TypeVar, cast, Final from .api import NodeEvent, PlugwiseNode, StickEvent from .connection import StickController @@ -19,7 +19,7 @@ FuncT = TypeVar("FuncT", bound=Callable[..., Any]) - +NOT_INITIALIZED_STICK_ERROR: Final[StickError] = StickError("Cannot load nodes when network is not initialized") _LOGGER = logging.getLogger(__name__) @@ -319,9 +319,7 @@ async def start_network(self) -> None: async def load_nodes(self) -> bool: """Load all discovered nodes.""" if self._network is None: - raise StickError( - "Cannot load nodes when network is not initialized" - ) + raise NOT_INITIALIZED_STICK_ERROR if not self._network.is_running: raise StickError( "Cannot load nodes when network is not started" @@ -333,9 +331,7 @@ async def load_nodes(self) -> bool: async def discover_coordinator(self, load: bool = False) -> None: """Discover the network coordinator.""" if self._network is None: - raise StickError( - "Cannot load nodes when network is not initialized" - ) + raise NOT_INITIALIZED_STICK_ERROR await self._network.discover_network_coordinator(load=load) @raise_not_connected @@ -343,9 +339,7 @@ async def discover_coordinator(self, load: bool = False) -> None: async def discover_nodes(self, load: bool = False) -> None: """Discover all nodes.""" if self._network is None: - raise StickError( - "Cannot load nodes when network is not initialized" - ) + raise NOT_INITIALIZED_STICK_ERROR await self._network.discover_nodes(load=load) @raise_not_connected diff --git a/plugwise_usb/messages/properties.py b/plugwise_usb/messages/properties.py index a2a065aa4..9cc51e861 100644 --- a/plugwise_usb/messages/properties.py +++ b/plugwise_usb/messages/properties.py @@ -3,12 +3,13 @@ import binascii from datetime import UTC, date, datetime, time, timedelta import struct -from typing import Any +from typing import Any, Final from ..constants import LOGADDR_OFFSET, PLUGWISE_EPOCH, UTF8 from ..exceptions import MessageError from ..helpers.util import int_to_uint +DESERIALIZE_ERROR: Final[MessageError] = MessageError("Unable to return value. Deserialize data first") class BaseType: """Generic single instance property.""" @@ -72,7 +73,7 @@ def deserialize(self, val: bytes) -> None: def value(self) -> bytes: """Return bytes value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -92,7 +93,7 @@ def deserialize(self, val: bytes) -> None: def value(self) -> str: """Return converted int value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -121,7 +122,7 @@ def deserialize(self, val: bytes) -> None: def value(self) -> int: """Return converted int value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -155,7 +156,7 @@ def deserialize(self, val: bytes) -> None: def value(self) -> int: """Return converted datetime value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -183,7 +184,7 @@ def deserialize(self, val: bytes) -> None: def value(self) -> datetime: """Return converted datetime value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -203,7 +204,7 @@ def deserialize(self, val: bytes) -> None: def value(self) -> int: """Return converted int value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -240,14 +241,14 @@ def deserialize(self, val: bytes) -> None: def value_set(self) -> bool: """True when datetime is converted.""" if not self._deserialized: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value is not None @property def value(self) -> datetime: """Return converted datetime value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -272,7 +273,7 @@ def deserialize(self, val: bytes) -> None: def value(self) -> time: """Return converted time value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -297,7 +298,7 @@ def deserialize(self, val: bytes) -> None: def value(self) -> str: """Return converted string value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -326,7 +327,7 @@ def deserialize(self, val: bytes) -> None: def value(self) -> time: """Return converted time value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -355,7 +356,7 @@ def deserialize(self, val: bytes) -> None: def value(self) -> date: """Return converted date value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -376,7 +377,7 @@ def deserialize(self, val: bytes) -> None: def value(self) -> float: """Return converted float value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value @@ -399,5 +400,5 @@ def deserialize(self, val: bytes) -> None: def value(self) -> int: """Return converted time value.""" if self._value is None: - raise MessageError("Unable to return value. Deserialize data first") + raise DESERIALIZE_ERROR return self._value