mirror of
https://github.com/home-assistant/core.git
synced 2026-10-07 06:50:41 -04:00
Translate Teslemetry command connection errors (#183457)
This commit is contained in:
@@ -4,6 +4,7 @@ import asyncio
|
||||
from collections.abc import Awaitable
|
||||
from typing import Any, cast
|
||||
|
||||
from aiohttp import ClientError
|
||||
from aiopowerwall import PowerwallAuthenticationError, PowerwallClient
|
||||
from tesla_fleet_api.exceptions import TeslaFleetError
|
||||
from tesla_fleet_api.tesla import EnergySiteRouter
|
||||
@@ -103,6 +104,11 @@ async def handle_command(command: Awaitable[dict[str, Any]]) -> dict[str, Any]:
|
||||
translation_key="command_exception",
|
||||
translation_placeholders={"message": e.message},
|
||||
) from e
|
||||
except (ClientError, TimeoutError) as e:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="command_connection_error",
|
||||
) from e
|
||||
LOGGER.debug("Command result: %s", result)
|
||||
return result
|
||||
|
||||
|
||||
@@ -1232,6 +1232,9 @@
|
||||
"auth_failed_subscription_required": {
|
||||
"message": "Teslemetry subscription required"
|
||||
},
|
||||
"command_connection_error": {
|
||||
"message": "Unable to connect to Teslemetry"
|
||||
},
|
||||
"command_error": {
|
||||
"message": "Command returned error: {error}"
|
||||
},
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from aiohttp import ClientConnectionError
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
from tesla_fleet_api.exceptions import InvalidCommand
|
||||
@@ -131,17 +132,29 @@ async def test_lock_command_errors(hass: HomeAssistant, response: dict) -> None:
|
||||
)
|
||||
|
||||
|
||||
async def test_lock_command_exception(hass: HomeAssistant) -> None:
|
||||
"""Tests that a command SDK exception raises HomeAssistantError."""
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect", "translation_key"),
|
||||
[
|
||||
pytest.param(InvalidCommand, "command_exception", id="fleet_error"),
|
||||
pytest.param(
|
||||
ClientConnectionError, "command_connection_error", id="client_error"
|
||||
),
|
||||
pytest.param(TimeoutError, "command_connection_error", id="timeout"),
|
||||
],
|
||||
)
|
||||
async def test_lock_command_exception(
|
||||
hass: HomeAssistant, side_effect: type[BaseException], translation_key: str
|
||||
) -> None:
|
||||
"""Tests that a command exception raises a translated HomeAssistantError."""
|
||||
|
||||
await setup_platform(hass, [Platform.LOCK])
|
||||
|
||||
with (
|
||||
patch(
|
||||
"tesla_fleet_api.teslemetry.Vehicle.door_lock",
|
||||
side_effect=InvalidCommand,
|
||||
side_effect=side_effect,
|
||||
),
|
||||
pytest.raises(HomeAssistantError),
|
||||
pytest.raises(HomeAssistantError) as exc_info,
|
||||
):
|
||||
await hass.services.async_call(
|
||||
LOCK_DOMAIN,
|
||||
@@ -149,6 +162,7 @@ async def test_lock_command_exception(hass: HomeAssistant) -> None:
|
||||
{ATTR_ENTITY_ID: "lock.test_lock"},
|
||||
blocking=True,
|
||||
)
|
||||
assert exc_info.value.translation_key == translation_key
|
||||
|
||||
|
||||
async def test_lock_streaming(
|
||||
|
||||
Reference in New Issue
Block a user