From bec88e5e512cc0b77321a10459b1e871f7826981 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 12 Jan 2024 11:31:08 +0100 Subject: [PATCH] Add decorator typing [izone] (#107556) --- homeassistant/components/izone/climate.py | 24 +++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/izone/climate.py b/homeassistant/components/izone/climate.py index 1ff016c3177..75eb19b2978 100644 --- a/homeassistant/components/izone/climate.py +++ b/homeassistant/components/izone/climate.py @@ -1,9 +1,9 @@ """Support for the iZone HVAC.""" from __future__ import annotations -from collections.abc import Mapping +from collections.abc import Callable, Mapping import logging -from typing import Any +from typing import Any, Concatenate, ParamSpec, TypeVar from pizone import Controller, Zone import voluptuous as vol @@ -47,6 +47,12 @@ from .const import ( 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__) _IZONE_FAN_TO_HA = { @@ -112,13 +118,15 @@ async def async_setup_entry( ) -def _return_on_connection_error(ret=None): - def wrap(func): - def wrapped_f(*args, **kwargs): - if not args[0].available: +def _return_on_connection_error( + ret: _T = None, # type: ignore[assignment] +) -> Callable[[_FuncType[_DeviceT, _P, _R]], _FuncType[_DeviceT, _P, _R | _T]]: + 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 try: - return func(*args, **kwargs) + return func(self, *args, **kwargs) except ConnectionError: return ret @@ -498,7 +506,7 @@ class ZoneDevice(ClimateEntity): return self._controller.available @property - @_return_on_connection_error(0) + @_return_on_connection_error(ClimateEntityFeature(0)) def supported_features(self) -> ClimateEntityFeature: """Return the list of supported features.""" if self._zone.mode == Zone.Mode.AUTO: