2021-05-11 17:36:40 +00:00
|
|
|
"""Helper methods for common tasks."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import functools as ft
|
|
|
|
import logging
|
|
|
|
from typing import Any, Callable
|
|
|
|
|
|
|
|
from pysonos.exceptions import SoCoException, SoCoUPnPException
|
|
|
|
|
2021-06-17 09:09:57 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
|
2021-05-11 17:36:40 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def soco_error(errorcodes: list[str] | None = None) -> Callable:
|
2021-06-17 09:09:57 +00:00
|
|
|
"""Filter out specified UPnP errors and raise exceptions for service calls."""
|
2021-05-11 17:36:40 +00:00
|
|
|
|
|
|
|
def decorator(funct: Callable) -> Callable:
|
|
|
|
"""Decorate functions."""
|
|
|
|
|
|
|
|
@ft.wraps(funct)
|
|
|
|
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
|
|
"""Wrap for all soco UPnP exception."""
|
|
|
|
try:
|
|
|
|
return funct(*args, **kwargs)
|
2021-06-17 09:09:57 +00:00
|
|
|
except (OSError, SoCoException, SoCoUPnPException) as err:
|
|
|
|
error_code = getattr(err, "error_code", None)
|
|
|
|
function = funct.__name__
|
|
|
|
if errorcodes and error_code in errorcodes:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Error code %s ignored in call to %s", error_code, function
|
|
|
|
)
|
|
|
|
return
|
|
|
|
raise HomeAssistantError(f"Error calling {function}: {err}") from err
|
2021-05-11 17:36:40 +00:00
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return decorator
|