Move core Sonos functionality out of entities (#50277)

This commit is contained in:
jjlawren 2021-05-11 12:36:40 -05:00 committed by GitHub
parent 0fdc50408a
commit d6a202bd74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 733 additions and 839 deletions

View file

@ -0,0 +1,32 @@
"""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
_LOGGER = logging.getLogger(__name__)
def soco_error(errorcodes: list[str] | None = None) -> Callable:
"""Filter out specified UPnP errors from logs and avoid exceptions."""
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)
except SoCoUPnPException as err:
if not errorcodes or err.error_code not in errorcodes:
_LOGGER.error("Error on %s with %s", funct.__name__, err)
except SoCoException as err:
_LOGGER.error("Error on %s with %s", funct.__name__, err)
return wrapper
return decorator