diff --git a/api.py b/api.py index 95af513..e1f526f 100644 --- a/api.py +++ b/api.py @@ -1,7 +1,8 @@ """Auth sessions for pybotvac.""" +from __future__ import annotations + from functools import wraps import logging -from typing import Optional import pybotvac from pybotvac.exceptions import NeatoRobotException @@ -47,15 +48,14 @@ class VorwerkSession(pybotvac.PasswordlessSession): return self._token -def when_available(f): +def when_available(func): """Prevent calling the method and return None when not available.""" - @wraps(f) + @wraps(func) def wrapper(self, *args, **kw): - if self.available: - return f(self, *args, **kw) - else: + if not self.available: return None + return func(self, *args, **kw) return wrapper @@ -98,11 +98,9 @@ class VorwerkState: self.robot_state = {} return - self._available = True - @property @when_available - def docked(self) -> Optional[bool]: + def docked(self): """Vacuum is docked.""" return ( self.robot_state["state"] == ROBOT_STATE_IDLE @@ -111,7 +109,7 @@ class VorwerkState: @property @when_available - def charging(self) -> Optional[bool]: + def charging(self): """Vacuum is charging.""" return ( self.robot_state.get("state") == ROBOT_STATE_IDLE @@ -120,27 +118,28 @@ class VorwerkState: @property @when_available - def state(self) -> Optional[str]: + def state(self) -> str | None: """Return Home Assistant vacuum state.""" robot_state = self.robot_state.get("state") + state = None if self.charging or self.docked: - return STATE_DOCKED + state = STATE_DOCKED elif robot_state == ROBOT_STATE_IDLE: - return STATE_IDLE + state = STATE_IDLE elif robot_state == ROBOT_STATE_BUSY: if robot_state["action"] != ROBOT_ACTION_DOCKING: - return STATE_RETURNING + state = STATE_RETURNING else: - return STATE_CLEANING + state = STATE_CLEANING elif robot_state == ROBOT_STATE_PAUSE: - return STATE_PAUSED + state = STATE_PAUSED elif robot_state == ROBOT_STATE_ERROR: - return STATE_ERROR - return None + state = STATE_ERROR + return state @property @when_available - def alert(self) -> Optional[str]: + def alert(self) -> str | None: """Return vacuum alert message.""" if "alert" in self.robot_state: return ALERTS.get(self.robot_state["alert"], self.robot_state["alert"]) @@ -148,7 +147,7 @@ class VorwerkState: @property @when_available - def status(self) -> Optional[str]: + def status(self) -> str | None: """Return vacuum status message.""" status = None @@ -210,6 +209,6 @@ class VorwerkState: @property @when_available - def scheduleEnabled(self) -> Optional[bool]: + def schedule_enabled(self): """Return True when schedule is enabled.""" return bool(self.robot_state["details"]["isScheduleEnabled"]) diff --git a/config_flow.py b/config_flow.py index cc1d870..9d721d9 100644 --- a/config_flow.py +++ b/config_flow.py @@ -1,6 +1,8 @@ """Config flow to configure Vorwerk integration.""" +from __future__ import annotations + import logging -from typing import Any, Dict, Optional +from typing import Any from pybotvac.exceptions import NeatoException from requests.models import HTTPError @@ -35,7 +37,7 @@ class VorwerkConfigFlow(config_entries.ConfigFlow, domain=VORWERK_DOMAIN): def __init__(self): """Initialize the config flow.""" - self._email: Optional[str] = None + self._email: str | None = None self._session = api.VorwerkSession() async def async_step_user(self, user_input=None): @@ -59,8 +61,8 @@ class VorwerkConfigFlow(config_entries.ConfigFlow, domain=VORWERK_DOMAIN): ) async def async_step_code( - self, user_input: Dict[str, Any] = None - ) -> Dict[str, Any]: + self, user_input: dict[str, Any] = None + ) -> dict[str, Any]: """Step when user enters OTP Code from email.""" assert self._email is not None # typing errors = {} @@ -96,7 +98,7 @@ class VorwerkConfigFlow(config_entries.ConfigFlow, domain=VORWERK_DOMAIN): errors=errors, ) - async def async_step_import(self, user_input: Dict[str, Any]) -> Dict[str, Any]: + async def async_step_import(self, user_input: dict[str, Any]) -> dict[str, Any]: """Import a config flow from configuration.""" unique_id = "from configuration" data = {VORWERK_ROBOTS: user_input} diff --git a/switch.py b/switch.py index 64f19bf..847bdce 100644 --- a/switch.py +++ b/switch.py @@ -71,7 +71,7 @@ class VorwerkScheduleSwitch(CoordinatorEntity, ToggleEntity): def is_on(self): """Return true if switch is on.""" if self._state.available: - if self._state.scheduleEnabled: + if self._state.schedule_enabled: return STATE_ON else: return STATE_OFF