Repository: home-assistant/core
File: homeassistant/components/braviatv/coordinator.py (Python)

Code before:
    SourceType,
)

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL: Final = timedelta(seconds=10)

type BraviaTVConfigEntry = ConfigEntry[BraviaTVCoordinator]


def catch_braviatv_errors[_BraviaTVCoordinatorT: BraviaTVCoordinator, **_P](
    func: Callable[Concatenate[_BraviaTVCoordinatorT, _P], Awaitable[None]],
) -> Callable[Concatenate[_BraviaTVCoordinatorT, _P], Coroutine[Any, Any, None]]:
    """Catch Bravia errors."""

    @wraps(func)
    async def wrapper(
        self: _BraviaTVCoordinatorT,
        *args: _P.args,
        **kwargs: _P.kwargs,
    ) -> None:
        """Catch Bravia errors and log message."""
        try:
            await func(self, *args, **kwargs)
        except BraviaNotFound:
            _LOGGER.error("Error sending command: the Bravia API service is reloading")
        except (BraviaConnectionError, BraviaConnectionTimeout, BraviaTurnedOff):
            _LOGGER.error("Error sending command: the TV is turned off")
        except BraviaError as err:
            _LOGGER.error("Error sending command: %s", err)
        await self.async_request_refresh()

    return wrapper


class BraviaTVCoordinator(DataUpdateCoordinator[None]):
    """Representation of a Bravia TV Coordinator."""

    config_entry: BraviaTVConfigEntry

    def __init__(
        self,
        hass: HomeAssistant,
        config_entry: BraviaTVConfigEntry,
        client: BraviaClient,
    ) -> None:
        """Initialize Bravia TV Client."""

        self.client = client
        self.pin = config_entry.data[CONF_PIN]
        self.use_psk = config_entry.data.get(CONF_USE_PSK, False)

Review comment (suggestion):
I think it worth to add string translations in case of errors. What do you think?

```python
        except BraviaNotFound as err:
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="device_not_found",
                translation_placeholders={
                    "device": self.config_entry.title,
                },
            ) from err
```
and in strings.json:
```json
  "exceptions": {
    "device_not_found": {
      "message": "Error sending command to {device}: the Bravia API service is reloading"
    }
  }
```

Code after:
    SourceType,
)

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL: Final = timedelta(seconds=10)

type BraviaTVConfigEntry = ConfigEntry[BraviaTVCoordinator]


def catch_braviatv_errors[_BraviaTVCoordinatorT: BraviaTVCoordinator, **_P](
    func: Callable[Concatenate[_BraviaTVCoordinatorT, _P], Awaitable[None]],
) -> Callable[Concatenate[_BraviaTVCoordinatorT, _P], Coroutine[Any, Any, None]]:
    """Catch Bravia errors."""

    @wraps(func)
    async def wrapper(
        self: _BraviaTVCoordinatorT,
        *args: _P.args,
        **kwargs: _P.kwargs,
    ) -> None:
        """Catch Bravia errors and log message."""
        try:
            await func(self, *args, **kwargs)
        except BraviaNotFound as err:
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="command_error_not_found",
                translation_placeholders={
                    "device": self.config_entry.title,
                },
            ) from err
        except (BraviaConnectionError, BraviaConnectionTimeout, BraviaTurnedOff) as err:
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="command_error_offline",
                translation_placeholders={
                    "device": self.config_entry.title,
                },
            ) from err
        except BraviaError as err:
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="command_error",
                translation_placeholders={
                    "device": self.config_entry.title,
                    "error": repr(err),
                },
            ) from err
        await self.async_request_refresh()