Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """Utilities for Plugwise."""
2 
3 from collections.abc import Awaitable, Callable, Coroutine
4 from typing import Any, Concatenate
5 
6 from plugwise.exceptions import PlugwiseException
7 
8 from homeassistant.exceptions import HomeAssistantError
9 
10 from .entity import PlugwiseEntity
11 
12 
13 def plugwise_command[_PlugwiseEntityT: PlugwiseEntity, **_P, _R](
14  func: Callable[Concatenate[_PlugwiseEntityT, _P], Awaitable[_R]],
15 ) -> Callable[Concatenate[_PlugwiseEntityT, _P], Coroutine[Any, Any, _R]]:
16  """Decorate Plugwise calls that send commands/make changes to the device.
17 
18  A decorator that wraps the passed in function, catches Plugwise errors,
19  and requests an coordinator update to update status of the devices asap.
20  """
21 
22  async def handler(
23  self: _PlugwiseEntityT, *args: _P.args, **kwargs: _P.kwargs
24  ) -> _R:
25  try:
26  return await func(self, *args, **kwargs)
27  except PlugwiseException as error:
28  raise HomeAssistantError(
29  f"Error communicating with API: {error}"
30  ) from error
31  finally:
32  await self.coordinator.async_request_refresh()
33 
34  return handler