hass-core/homeassistant/components/homematicip_cloud/helpers.py
hahn-th c9dfa15ed6
Add device HmIP-DLD (#83380)
* Add HmIP-DLD

* Remove commented code

* Fix errors

* Format using black

* Fix device count

* Add missing tests

* Apply changes by reviewer

* Change setup entry code

* Remove jammed state

* Add error messages

* Update homeassistant/components/homematicip_cloud/helpers.py

Co-authored-by: Aaron Bach <bachya1208@gmail.com>

* Add decorator

* Add error log output

* Update test_device.py

---------

Co-authored-by: Aaron Bach <bachya1208@gmail.com>
2023-02-26 10:49:25 -07:00

39 lines
1 KiB
Python

"""Helper functions for Homematicip Cloud Integration."""
from functools import wraps
import json
import logging
from homeassistant.exceptions import HomeAssistantError
from . import HomematicipGenericEntity
_LOGGER = logging.getLogger(__name__)
def is_error_response(response) -> bool:
"""Response from async call contains errors or not."""
if isinstance(response, dict):
return response.get("errorCode") not in ("", None)
return False
def handle_errors(func):
"""Handle async errors."""
@wraps(func)
async def inner(self: HomematicipGenericEntity) -> None:
"""Handle errors from async call."""
result = await func(self)
if is_error_response(result):
_LOGGER.error(
"Error while execute function %s: %s",
__name__,
json.dumps(result),
)
raise HomeAssistantError(
f"Error while execute function {func.__name__}: {result.get('errorCode')}. See log for more information."
)
return inner