mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Improve error handling in Teslemetry (#117336)
* Improvement command handle * Add test for ignored reasons
This commit is contained in:
parent
4d5ae57390
commit
af0dd189d9
@ -74,10 +74,9 @@ class TeslemetryEntity(
|
|||||||
"""Handle a command."""
|
"""Handle a command."""
|
||||||
try:
|
try:
|
||||||
result = await command
|
result = await command
|
||||||
LOGGER.debug("Command result: %s", result)
|
|
||||||
except TeslaFleetError as e:
|
except TeslaFleetError as e:
|
||||||
LOGGER.debug("Command error: %s", e.message)
|
|
||||||
raise HomeAssistantError(f"Teslemetry command failed, {e.message}") from e
|
raise HomeAssistantError(f"Teslemetry command failed, {e.message}") from e
|
||||||
|
LOGGER.debug("Command result: %s", result)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
@ -137,21 +136,20 @@ class TeslemetryVehicleEntity(TeslemetryEntity):
|
|||||||
"""Handle a vehicle command."""
|
"""Handle a vehicle command."""
|
||||||
result = await super().handle_command(command)
|
result = await super().handle_command(command)
|
||||||
if (response := result.get("response")) is None:
|
if (response := result.get("response")) is None:
|
||||||
if message := result.get("error"):
|
if error := result.get("error"):
|
||||||
# No response with error
|
# No response with error
|
||||||
LOGGER.info("Command failure: %s", message)
|
raise HomeAssistantError(error)
|
||||||
raise HomeAssistantError(message)
|
|
||||||
# No response without error (unexpected)
|
# No response without error (unexpected)
|
||||||
LOGGER.error("Unknown response: %s", response)
|
raise HomeAssistantError(f"Unknown response: {response}")
|
||||||
raise HomeAssistantError("Unknown response")
|
if (result := response.get("result")) is not True:
|
||||||
if (message := response.get("result")) is not True:
|
if reason := response.get("reason"):
|
||||||
if message := response.get("reason"):
|
if reason in ("already_set", "not_charging", "requested"):
|
||||||
|
# Reason is acceptable
|
||||||
|
return result
|
||||||
# Result of false with reason
|
# Result of false with reason
|
||||||
LOGGER.info("Command failure: %s", message)
|
raise HomeAssistantError(reason)
|
||||||
raise HomeAssistantError(message)
|
|
||||||
# Result of false without reason (unexpected)
|
# Result of false without reason (unexpected)
|
||||||
LOGGER.error("Unknown response: %s", response)
|
raise HomeAssistantError("Command failed with no reason")
|
||||||
raise HomeAssistantError("Unknown response")
|
|
||||||
# Response with result of true
|
# Response with result of true
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ SITE_INFO = load_json_object_fixture("site_info.json", DOMAIN)
|
|||||||
|
|
||||||
COMMAND_OK = {"response": {"result": True, "reason": ""}}
|
COMMAND_OK = {"response": {"result": True, "reason": ""}}
|
||||||
COMMAND_REASON = {"response": {"result": False, "reason": "already closed"}}
|
COMMAND_REASON = {"response": {"result": False, "reason": "already closed"}}
|
||||||
|
COMMAND_IGNORED_REASON = {"response": {"result": False, "reason": "already_set"}}
|
||||||
COMMAND_NOREASON = {"response": {"result": False}} # Unexpected
|
COMMAND_NOREASON = {"response": {"result": False}} # Unexpected
|
||||||
COMMAND_ERROR = {
|
COMMAND_ERROR = {
|
||||||
"response": None,
|
"response": None,
|
||||||
|
@ -27,6 +27,7 @@ from homeassistant.helpers import entity_registry as er
|
|||||||
from . import assert_entities, setup_platform
|
from . import assert_entities, setup_platform
|
||||||
from .const import (
|
from .const import (
|
||||||
COMMAND_ERRORS,
|
COMMAND_ERRORS,
|
||||||
|
COMMAND_IGNORED_REASON,
|
||||||
METADATA_NOSCOPE,
|
METADATA_NOSCOPE,
|
||||||
VEHICLE_DATA_ALT,
|
VEHICLE_DATA_ALT,
|
||||||
WAKE_UP_ASLEEP,
|
WAKE_UP_ASLEEP,
|
||||||
@ -134,8 +135,7 @@ async def test_climate_offline(
|
|||||||
assert_entities(hass, entry.entry_id, entity_registry, snapshot)
|
assert_entities(hass, entry.entry_id, entity_registry, snapshot)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("response", COMMAND_ERRORS)
|
async def test_invalid_error(hass: HomeAssistant) -> None:
|
||||||
async def test_errors(hass: HomeAssistant, response: str) -> None:
|
|
||||||
"""Tests service error is handled."""
|
"""Tests service error is handled."""
|
||||||
|
|
||||||
await setup_platform(hass, platforms=[Platform.CLIMATE])
|
await setup_platform(hass, platforms=[Platform.CLIMATE])
|
||||||
@ -157,12 +157,20 @@ async def test_errors(hass: HomeAssistant, response: str) -> None:
|
|||||||
mock_on.assert_called_once()
|
mock_on.assert_called_once()
|
||||||
assert error.from_exception == InvalidCommand
|
assert error.from_exception == InvalidCommand
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("response", COMMAND_ERRORS)
|
||||||
|
async def test_errors(hass: HomeAssistant, response: str) -> None:
|
||||||
|
"""Tests service reason is handled."""
|
||||||
|
|
||||||
|
await setup_platform(hass, platforms=[Platform.CLIMATE])
|
||||||
|
entity_id = "climate.test_climate"
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.teslemetry.VehicleSpecific.auto_conditioning_start",
|
"homeassistant.components.teslemetry.VehicleSpecific.auto_conditioning_start",
|
||||||
return_value=response,
|
return_value=response,
|
||||||
) as mock_on,
|
) as mock_on,
|
||||||
pytest.raises(HomeAssistantError) as error,
|
pytest.raises(HomeAssistantError),
|
||||||
):
|
):
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
CLIMATE_DOMAIN,
|
CLIMATE_DOMAIN,
|
||||||
@ -173,6 +181,26 @@ async def test_errors(hass: HomeAssistant, response: str) -> None:
|
|||||||
mock_on.assert_called_once()
|
mock_on.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
async def test_ignored_error(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
) -> None:
|
||||||
|
"""Tests ignored error is handled."""
|
||||||
|
|
||||||
|
await setup_platform(hass, [Platform.CLIMATE])
|
||||||
|
entity_id = "climate.test_climate"
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.teslemetry.VehicleSpecific.auto_conditioning_start",
|
||||||
|
return_value=COMMAND_IGNORED_REASON,
|
||||||
|
) as mock_on:
|
||||||
|
await hass.services.async_call(
|
||||||
|
CLIMATE_DOMAIN,
|
||||||
|
SERVICE_TURN_ON,
|
||||||
|
{ATTR_ENTITY_ID: [entity_id]},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
mock_on.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
async def test_asleep_or_offline(
|
async def test_asleep_or_offline(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_vehicle_data,
|
mock_vehicle_data,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user