Fix error handling

This commit is contained in:
Teemu Rytilahti 2025-01-03 19:46:20 +01:00
parent 92a3082c91
commit cf3f3166bc

View File

@ -10,7 +10,7 @@ from ...feature import Feature
from ..smartmodule import SmartModule from ..smartmodule import SmartModule
if TYPE_CHECKING: if TYPE_CHECKING:
from ..smartdevice import SmartDevice pass
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -29,6 +29,14 @@ class Status(IntEnum):
Unknown = 101 Unknown = 101
class ErrorCode(IntEnum):
"""Error codes for vacuum."""
Ok = 0
DustBinRemoved = 14
Unknown = -1000
class FanSpeed(IntEnum): class FanSpeed(IntEnum):
"""Fan speed level.""" """Fan speed level."""
@ -42,12 +50,13 @@ class Vacuum(SmartModule):
"""Implementation of vacuum support.""" """Implementation of vacuum support."""
REQUIRED_COMPONENT = "clean" REQUIRED_COMPONENT = "clean"
_error_code = ErrorCode.Ok
def __init__(self, device: SmartDevice, module: str) -> None: def _initialize_features(self) -> None:
super().__init__(device, module) """Initialize features."""
self._add_feature( self._add_feature(
Feature( Feature(
device, self._device,
id="vacuum_return_home", id="vacuum_return_home",
name="Return home", name="Return home",
container=self, container=self,
@ -58,7 +67,7 @@ class Vacuum(SmartModule):
) )
self._add_feature( self._add_feature(
Feature( Feature(
device, self._device,
id="vacuum_start", id="vacuum_start",
name="Start cleaning", name="Start cleaning",
container=self, container=self,
@ -69,7 +78,7 @@ class Vacuum(SmartModule):
) )
self._add_feature( self._add_feature(
Feature( Feature(
device, self._device,
id="vacuum_pause", id="vacuum_pause",
name="Pause", name="Pause",
container=self, container=self,
@ -80,7 +89,7 @@ class Vacuum(SmartModule):
) )
self._add_feature( self._add_feature(
Feature( Feature(
device, self._device,
id="vacuum_status", id="vacuum_status",
name="Vacuum status", name="Vacuum status",
container=self, container=self,
@ -89,6 +98,17 @@ class Vacuum(SmartModule):
type=Feature.Type.Sensor, type=Feature.Type.Sensor,
) )
) )
self._add_feature(
Feature(
self._device,
id="vacuum_error",
name="Error",
container=self,
attribute_getter="error",
category=Feature.Category.Info,
type=Feature.Type.Sensor,
)
)
self._add_feature( self._add_feature(
Feature( Feature(
self._device, self._device,
@ -118,6 +138,24 @@ class Vacuum(SmartModule):
) )
) )
async def _post_update_hook(self) -> None:
"""Set error code after update."""
errors = self._vac_status.get("err_status")
if errors is None:
self._error = ErrorCode.Ok
return
if len(errors) > 1:
_LOGGER.warning(
"Multiple error codes, using the first one only: %s", errors
)
error = errors.pop(0)
try:
self._error = ErrorCode(error)
except ValueError:
self._error = ErrorCode.Unknown
def query(self) -> dict: def query(self) -> dict:
"""Query to execute during the update cycle.""" """Query to execute during the update cycle."""
return { return {
@ -165,6 +203,11 @@ class Vacuum(SmartModule):
"""Return home / pause returning.""" """Return home / pause returning."""
return await self.call("setSwitchCharge", {"switch_charge": enabled}) return await self.call("setSwitchCharge", {"switch_charge": enabled})
@property
def error(self) -> ErrorCode:
"""Return error."""
return self._error
@property @property
def fan_speed_preset(self) -> str: def fan_speed_preset(self) -> str:
"""Return fan speed preset.""" """Return fan speed preset."""
@ -192,7 +235,7 @@ class Vacuum(SmartModule):
@property @property
def status(self) -> Status: def status(self) -> Status:
"""Return current status.""" """Return current status."""
if self._vac_status.get("err_status"): if self._error_code is not ErrorCode.Ok:
return Status.Error return Status.Error
status_code = self._vac_status["status"] status_code = self._vac_status["status"]