Improve decorator type annotations [sensibo] (#104824)
parent
fed8e5e873
commit
ca9d58c442
|
@ -23,22 +23,24 @@ def async_handle_api_call(
|
||||||
) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, Any]]:
|
) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, Any]]:
|
||||||
"""Decorate api calls."""
|
"""Decorate api calls."""
|
||||||
|
|
||||||
async def wrap_api_call(*args: Any, **kwargs: Any) -> None:
|
async def wrap_api_call(entity: _T, *args: _P.args, **kwargs: _P.kwargs) -> None:
|
||||||
"""Wrap services for api calls."""
|
"""Wrap services for api calls."""
|
||||||
res: bool = False
|
res: bool = False
|
||||||
try:
|
try:
|
||||||
async with asyncio.timeout(TIMEOUT):
|
async with asyncio.timeout(TIMEOUT):
|
||||||
res = await function(*args, **kwargs)
|
res = await function(entity, *args, **kwargs)
|
||||||
except SENSIBO_ERRORS as err:
|
except SENSIBO_ERRORS as err:
|
||||||
raise HomeAssistantError from err
|
raise HomeAssistantError from err
|
||||||
|
|
||||||
LOGGER.debug("Result %s for entity %s with arguments %s", res, args[0], kwargs)
|
LOGGER.debug("Result %s for entity %s with arguments %s", res, entity, kwargs)
|
||||||
entity: SensiboDeviceBaseEntity = args[0]
|
|
||||||
if res is not True:
|
if res is not True:
|
||||||
raise HomeAssistantError(f"Could not execute service for {entity.name}")
|
raise HomeAssistantError(f"Could not execute service for {entity.name}")
|
||||||
if kwargs.get("key") is not None and kwargs.get("value") is not None:
|
if (
|
||||||
setattr(entity.device_data, kwargs["key"], kwargs["value"])
|
isinstance(key := kwargs.get("key"), str)
|
||||||
LOGGER.debug("Debug check key %s is now %s", kwargs["key"], kwargs["value"])
|
and (value := kwargs.get("value")) is not None
|
||||||
|
):
|
||||||
|
setattr(entity.device_data, key, value)
|
||||||
|
LOGGER.debug("Debug check key %s is now %s", key, value)
|
||||||
entity.async_write_ha_state()
|
entity.async_write_ha_state()
|
||||||
await entity.coordinator.async_request_refresh()
|
await entity.coordinator.async_request_refresh()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue