Rename and deprecate exception classes (#739)

# Public #
SmartDeviceException -> KasaException
UnsupportedDeviceException(SmartDeviceException) -> UnsupportedDeviceError(KasaException)
TimeoutException(SmartDeviceException, asyncio.TimeoutError) -> TimeoutError(KasaException, asyncio.TimeoutError)

Add new exception for error codes -> DeviceError(KasaException)
AuthenticationException(SmartDeviceException) -> AuthenticationError(DeviceError)

# Internal #
RetryableException(SmartDeviceException) -> _RetryableError(DeviceError)
ConnectionException(SmartDeviceException) -> _ConnectionError(KasaException)
This commit is contained in:
Steven B
2024-02-21 15:52:55 +00:00
committed by GitHub
parent 4beff228c9
commit 8c39e81a40
44 changed files with 393 additions and 361 deletions

View File

@@ -4,7 +4,7 @@ from typing import Any, Dict, List, Optional
from ..bulb import Bulb
from ..device_type import DeviceType
from ..deviceconfig import DeviceConfig
from ..exceptions import SmartDeviceException
from ..exceptions import KasaException
from ..iot.iotbulb import HSV, BulbPreset, ColorTempRange
from ..smartprotocol import SmartProtocol
from .smartdevice import SmartDevice
@@ -57,7 +57,7 @@ class SmartBulb(SmartDevice, Bulb):
:return: White temperature range in Kelvin (minimum, maximum)
"""
if not self.is_variable_color_temp:
raise SmartDeviceException("Color temperature not supported")
raise KasaException("Color temperature not supported")
ct_range = self._info.get("color_temp_range", [0, 0])
return ColorTempRange(min=ct_range[0], max=ct_range[1])
@@ -107,7 +107,7 @@ class SmartBulb(SmartDevice, Bulb):
:return: hue, saturation and value (degrees, %, %)
"""
if not self.is_color:
raise SmartDeviceException("Bulb does not support color.")
raise KasaException("Bulb does not support color.")
h, s, v = (
self._info.get("hue", 0),
@@ -121,7 +121,7 @@ class SmartBulb(SmartDevice, Bulb):
def color_temp(self) -> int:
"""Whether the bulb supports color temperature changes."""
if not self.is_variable_color_temp:
raise SmartDeviceException("Bulb does not support colortemp.")
raise KasaException("Bulb does not support colortemp.")
return self._info.get("color_temp", -1)
@@ -129,7 +129,7 @@ class SmartBulb(SmartDevice, Bulb):
def brightness(self) -> int:
"""Return the current brightness in percentage."""
if not self.is_dimmable: # pragma: no cover
raise SmartDeviceException("Bulb is not dimmable.")
raise KasaException("Bulb is not dimmable.")
return self._info.get("brightness", -1)
@@ -151,7 +151,7 @@ class SmartBulb(SmartDevice, Bulb):
:param int transition: transition in milliseconds.
"""
if not self.is_color:
raise SmartDeviceException("Bulb does not support color.")
raise KasaException("Bulb does not support color.")
if not isinstance(hue, int) or not (0 <= hue <= 360):
raise ValueError(f"Invalid hue value: {hue} (valid range: 0-360)")
@@ -188,7 +188,7 @@ class SmartBulb(SmartDevice, Bulb):
# TODO: Note, trying to set brightness at the same time
# with color_temp causes error -1008
if not self.is_variable_color_temp:
raise SmartDeviceException("Bulb does not support colortemp.")
raise KasaException("Bulb does not support colortemp.")
valid_temperature_range = self.valid_temperature_range
if temp < valid_temperature_range[0] or temp > valid_temperature_range[1]:
@@ -211,7 +211,7 @@ class SmartBulb(SmartDevice, Bulb):
:param int transition: transition in milliseconds.
"""
if not self.is_dimmable: # pragma: no cover
raise SmartDeviceException("Bulb is not dimmable.")
raise KasaException("Bulb is not dimmable.")
return await self.protocol.query(
{"set_device_info": {"brightness": brightness}}

View File

@@ -9,7 +9,7 @@ from ..device import Device, WifiNetwork
from ..device_type import DeviceType
from ..deviceconfig import DeviceConfig
from ..emeterstatus import EmeterStatus
from ..exceptions import AuthenticationException, SmartDeviceException, SmartErrorCode
from ..exceptions import AuthenticationError, DeviceError, KasaException, SmartErrorCode
from ..feature import Feature, FeatureType
from ..smartprotocol import SmartProtocol
from .modules import ( # noqa: F401
@@ -85,7 +85,7 @@ class SmartDevice(Device):
return response
if default is not None:
return default
raise SmartDeviceException(
raise KasaException(
f"{request} not found in {responses} for device {self.host}"
)
@@ -100,7 +100,7 @@ class SmartDevice(Device):
async def update(self, update_children: bool = True):
"""Update the device."""
if self.credentials is None and self.credentials_hash is None:
raise AuthenticationException("Tapo plug requires authentication.")
raise AuthenticationError("Tapo plug requires authentication.")
if self._components_raw is None:
await self._negotiate()
@@ -341,7 +341,7 @@ class SmartDevice(Device):
"""Retrieve current energy readings."""
_LOGGER.warning("Deprecated, use `emeter_realtime`.")
if not self.has_emeter:
raise SmartDeviceException("Device has no emeter")
raise KasaException("Device has no emeter")
return self.emeter_realtime
@property
@@ -421,7 +421,7 @@ class SmartDevice(Device):
after some delay.
"""
if not self.credentials:
raise AuthenticationException("Device requires authentication.")
raise AuthenticationError("Device requires authentication.")
payload = {
"account": {
@@ -445,10 +445,9 @@ class SmartDevice(Device):
# Thus, We limit retries and suppress the raised exception as useless.
try:
return await self.protocol.query({"set_qs_info": payload}, retry_count=0)
except SmartDeviceException as ex:
if ex.error_code: # Re-raise on device-reported errors
raise
except DeviceError:
raise # Re-raise on device-reported errors
except KasaException:
_LOGGER.debug("Received an expected for wifi join, but this is expected")
async def update_credentials(self, username: str, password: str):

View File

@@ -2,7 +2,7 @@
import logging
from typing import TYPE_CHECKING, Dict, Type
from ..exceptions import SmartDeviceException
from ..exceptions import KasaException
from ..module import Module
if TYPE_CHECKING:
@@ -59,7 +59,7 @@ class SmartModule(Module):
q_keys = list(q.keys())
# TODO: hacky way to check if update has been called.
if q_keys[0] not in self._device._last_update:
raise SmartDeviceException(
raise KasaException(
f"You need to call update() prior accessing module data"
f" for '{self._module}'"
)