Repository: home-assistant/core
File: homeassistant/components/tesla_fleet/helpers.py (Python)

Code before:
from typing import Any

from tesla_fleet_api.exceptions import TeslaFleetError

from homeassistant.exceptions import HomeAssistantError

from .const import DOMAIN, LOGGER, TeslaFleetState
from .models import TeslaFleetVehicleData


async def wake_up_vehicle(vehicle: TeslaFleetVehicleData) -> None:
    """Wake up a vehicle."""
    async with vehicle.wakelock:
        times = 0
        while vehicle.coordinator.data["state"] != TeslaFleetState.ONLINE:
            try:
                if times == 0:
                    cmd = await vehicle.api.wake_up()
                else:
                    cmd = await vehicle.api.vehicle()
                state = cmd["response"]["state"]
            except TeslaFleetError as e:
                raise HomeAssistantError(
                    translation_domain=DOMAIN,
                    translation_key="wake_up_failed",
                    translation_placeholders={"message": e.message},
                ) from e
            vehicle.coordinator.data["state"] = state
            if state != TeslaFleetState.ONLINE:
                times += 1
                if times >= 4:  # Give up after 30 seconds total
                    raise HomeAssistantError(
                        translation_domain=DOMAIN,
                        translation_key="wake_up_timeout",
                    )
                await asyncio.sleep(times * 5)


async def handle_command(command: Awaitable) -> dict[str, Any]:
    """Handle a command."""
    try:
        result = await command
    except TeslaFleetError as e:
        raise HomeAssistantError(
            translation_domain=DOMAIN,
            translation_key="command_failed",
            translation_placeholders={"message": e.message},
        ) from e
    LOGGER.debug("Command result: %s", result)
    return result


Review comment (bug):
```suggestion
                    translation_domain=DOMAIN,
                    translation_key="wake_up_failed",
```
don't do that, or you'll end up with a mixed translated string in the UI

Code after:
from typing import Any

from tesla_fleet_api.exceptions import TeslaFleetError

from homeassistant.exceptions import HomeAssistantError

from .const import DOMAIN, LOGGER, TeslaFleetState
from .models import TeslaFleetVehicleData


async def wake_up_vehicle(vehicle: TeslaFleetVehicleData) -> None:
    """Wake up a vehicle."""
    async with vehicle.wakelock:
        times = 0
        while vehicle.coordinator.data["state"] != TeslaFleetState.ONLINE:
            try:
                if times == 0:
                    cmd = await vehicle.api.wake_up()
                else:
                    cmd = await vehicle.api.vehicle()
                state = cmd["response"]["state"]
            except TeslaFleetError as e:
                raise HomeAssistantError(
                    translation_domain=DOMAIN,
                    translation_key="wake_up_failed",
                ) from e
            vehicle.coordinator.data["state"] = state
            if state != TeslaFleetState.ONLINE:
                times += 1
                if times >= 4:  # Give up after 30 seconds total
                    raise HomeAssistantError(
                        translation_domain=DOMAIN,
                        translation_key="wake_up_timeout",
                    )
                await asyncio.sleep(times * 5)


async def handle_command(command: Awaitable) -> dict[str, Any]:
    """Handle a command."""
    try:
        result = await command
    except TeslaFleetError as e:
        raise HomeAssistantError(
            translation_domain=DOMAIN,
            translation_key="command_failed",
            translation_placeholders={"message": e.message},
        ) from e
    LOGGER.debug("Command result: %s", result)
    return result