Add decorator typing [izone] (#107556)

This commit is contained in:
Marc Mueller 2024-01-12 11:31:08 +01:00 committed by GitHub
parent 68ddc1481e
commit bec88e5e51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,9 @@
"""Support for the iZone HVAC.""" """Support for the iZone HVAC."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Mapping from collections.abc import Callable, Mapping
import logging import logging
from typing import Any from typing import Any, Concatenate, ParamSpec, TypeVar
from pizone import Controller, Zone from pizone import Controller, Zone
import voluptuous as vol import voluptuous as vol
@ -47,6 +47,12 @@ from .const import (
IZONE, IZONE,
) )
_DeviceT = TypeVar("_DeviceT", bound="ControllerDevice | ZoneDevice")
_T = TypeVar("_T")
_R = TypeVar("_R")
_P = ParamSpec("_P")
_FuncType = Callable[Concatenate[_T, _P], _R]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_IZONE_FAN_TO_HA = { _IZONE_FAN_TO_HA = {
@ -112,13 +118,15 @@ async def async_setup_entry(
) )
def _return_on_connection_error(ret=None): def _return_on_connection_error(
def wrap(func): ret: _T = None, # type: ignore[assignment]
def wrapped_f(*args, **kwargs): ) -> Callable[[_FuncType[_DeviceT, _P, _R]], _FuncType[_DeviceT, _P, _R | _T]]:
if not args[0].available: def wrap(func: _FuncType[_DeviceT, _P, _R]) -> _FuncType[_DeviceT, _P, _R | _T]:
def wrapped_f(self: _DeviceT, *args: _P.args, **kwargs: _P.kwargs) -> _R | _T:
if not self.available:
return ret return ret
try: try:
return func(*args, **kwargs) return func(self, *args, **kwargs)
except ConnectionError: except ConnectionError:
return ret return ret
@ -498,7 +506,7 @@ class ZoneDevice(ClimateEntity):
return self._controller.available return self._controller.available
@property @property
@_return_on_connection_error(0) @_return_on_connection_error(ClimateEntityFeature(0))
def supported_features(self) -> ClimateEntityFeature: def supported_features(self) -> ClimateEntityFeature:
"""Return the list of supported features.""" """Return the list of supported features."""
if self._zone.mode == Zone.Mode.AUTO: if self._zone.mode == Zone.Mode.AUTO: