Repository: home-assistant/core
File: homeassistant/components/xbox/media_player.py (Python)

Code before:

    @property
    def media_image_url(self) -> str | None:
        """Image url of current playing media."""
        if not (app_details := self.data.app_details) or not (
            image := _find_media_image(app_details.localized_properties[0].images)
        ):
            return None

        url = image.uri
        if url[0] == "/":
            url = f"http:{url}"
        return url

    @exception_handler
    async def async_turn_on(self) -> None:
        """Turn the media player on."""
        try:
            await self.client.smartglass.wake_up(self._console.id)
        except HTTPStatusError as e:
            if e.response.status_code == HTTPStatus.NOT_FOUND:
                raise ServiceValidationError(
                    translation_domain=DOMAIN,
                    translation_key="turn_on_failed",
                ) from e
            raise

    @exception_handler
    async def async_turn_off(self) -> None:
        """Turn the media player off."""
        await self.client.smartglass.turn_off(self._console.id)

    @exception_handler
    async def async_mute_volume(self, mute: bool) -> None:
        """Mute the volume."""

        if mute:
            await self.client.smartglass.mute(self._console.id)
        else:
            await self.client.smartglass.unmute(self._console.id)

        self._attr_is_volume_muted = mute
        self.async_write_ha_state()

    @exception_handler
    async def async_volume_up(self) -> None:
        """Turn volume up for media player."""

        await self.client.smartglass.volume(self._console.id, VolumeDirection.Up)

    @exception_handler

Review comment (suggestion):
`ServiceValidationError` should be raised before a connection attempt (bad input from user)

I don't think it's a good exception here, and I'm not sure why it would apply only to `async_turn_on`.

Code after:

    @property
    def media_image_url(self) -> str | None:
        """Image url of current playing media."""
        if not (app_details := self.data.app_details) or not (
            image := _find_media_image(app_details.localized_properties[0].images)
        ):
            return None

        url = image.uri
        if url[0] == "/":
            url = f"http:{url}"
        return url

    @exception_handler
    async def async_turn_on(self) -> None:
        """Turn the media player on."""
        try:
            await self.client.smartglass.wake_up(self._console.id)
        except HTTPStatusError as e:
            if e.response.status_code == HTTPStatus.NOT_FOUND:
                raise HomeAssistantError(
                    translation_domain=DOMAIN,
                    translation_key="turn_on_failed",
                ) from e
            raise

    @exception_handler
    async def async_turn_off(self) -> None:
        """Turn the media player off."""
        await self.client.smartglass.turn_off(self._console.id)

    @exception_handler
    async def async_mute_volume(self, mute: bool) -> None:
        """Mute the volume."""

        if mute:
            await self.client.smartglass.mute(self._console.id)
        else:
            await self.client.smartglass.unmute(self._console.id)

        self._attr_is_volume_muted = mute
        self.async_write_ha_state()

    @exception_handler
    async def async_volume_up(self) -> None:
        """Turn volume up for media player."""

        await self.client.smartglass.volume(self._console.id, VolumeDirection.Up)

    @exception_handler