1
0
mirror of https://github.com/home-assistant/core.git synced 2026-07-07 14:56:25 +01:00
Files
core/tests/components/teslemetry/test_number.py
T
Brett Adams 15aaddf4ac Add command error-path tests to Teslemetry (#174163)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 17:23:27 +02:00

181 lines
5.7 KiB
Python

"""Test the Teslemetry number platform."""
from unittest.mock import AsyncMock, patch
import pytest
from syrupy.assertion import SnapshotAssertion
from tesla_fleet_api.exceptions import InvalidCommand
from teslemetry_stream import Signal
from homeassistant.components.number import (
ATTR_VALUE,
DOMAIN as NUMBER_DOMAIN,
SERVICE_SET_VALUE,
)
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from . import assert_entities, reload_platform, setup_platform
from .const import COMMAND_ERRORS, COMMAND_OK, VEHICLE_DATA_ALT
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_number(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
mock_legacy: AsyncMock,
) -> None:
"""Tests that the number entities are correct."""
entry = await setup_platform(hass, [Platform.NUMBER])
assert_entities(hass, entry.entry_id, entity_registry, snapshot)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_number_services(
hass: HomeAssistant, mock_vehicle_data: AsyncMock
) -> None:
"""Tests that the number services work."""
mock_vehicle_data.return_value = VEHICLE_DATA_ALT
await setup_platform(hass, [Platform.NUMBER])
entity_id = "number.test_charge_current"
with patch(
"tesla_fleet_api.teslemetry.Vehicle.set_charging_amps",
return_value=COMMAND_OK,
) as call:
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 16},
blocking=True,
)
state = hass.states.get(entity_id)
assert state.state == "16"
call.assert_called_once()
entity_id = "number.test_charge_limit"
with patch(
"tesla_fleet_api.teslemetry.Vehicle.set_charge_limit",
return_value=COMMAND_OK,
) as call:
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 60},
blocking=True,
)
state = hass.states.get(entity_id)
assert state.state == "60"
call.assert_called_once()
entity_id = "number.energy_site_backup_reserve"
with patch(
"tesla_fleet_api.teslemetry.EnergySite.backup",
return_value=COMMAND_OK,
) as call:
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: entity_id,
ATTR_VALUE: 80,
},
blocking=True,
)
state = hass.states.get(entity_id)
assert state.state == "80"
call.assert_called_once()
entity_id = "number.energy_site_off_grid_reserve"
with patch(
"tesla_fleet_api.teslemetry.EnergySite.off_grid_vehicle_charging_reserve",
return_value=COMMAND_OK,
) as call:
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 88},
blocking=True,
)
state = hass.states.get(entity_id)
assert state.state == "88"
call.assert_called_once()
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
@pytest.mark.parametrize("response", COMMAND_ERRORS)
async def test_number_command_errors(
hass: HomeAssistant, mock_vehicle_data: AsyncMock, response: dict
) -> None:
"""Tests that vehicle command failures raise HomeAssistantError."""
mock_vehicle_data.return_value = VEHICLE_DATA_ALT
await setup_platform(hass, [Platform.NUMBER])
with (
patch(
"tesla_fleet_api.teslemetry.Vehicle.set_charging_amps",
return_value=response,
),
pytest.raises(HomeAssistantError),
):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{ATTR_ENTITY_ID: "number.test_charge_current", ATTR_VALUE: 16},
blocking=True,
)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_number_command_exception(hass: HomeAssistant) -> None:
"""Tests that an energy command SDK exception raises HomeAssistantError."""
await setup_platform(hass, [Platform.NUMBER])
with (
patch(
"tesla_fleet_api.teslemetry.EnergySite.backup",
side_effect=InvalidCommand,
),
pytest.raises(HomeAssistantError),
):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{ATTR_ENTITY_ID: "number.energy_site_backup_reserve", ATTR_VALUE: 80},
blocking=True,
)
async def test_number_streaming(
hass: HomeAssistant,
mock_vehicle_data: AsyncMock,
mock_add_listener: AsyncMock,
) -> None:
"""Tests that the number entities with streaming are correct."""
entry = await setup_platform(hass, [Platform.NUMBER])
# Stream update
mock_add_listener.send(
{
"vin": VEHICLE_DATA_ALT["response"]["vin"],
"data": {
Signal.CHARGE_CURRENT_REQUEST: 24,
Signal.CHARGE_CURRENT_REQUEST_MAX: 32,
Signal.CHARGE_LIMIT_SOC: 99,
},
"createdAt": "2024-10-04T10:45:17.537Z",
}
)
await hass.async_block_till_done()
await reload_platform(hass, entry, [Platform.NUMBER])
# Assert the entities restored their values with concrete assertions
assert hass.states.get("number.test_charge_current").state == "24"
assert hass.states.get("number.test_charge_limit").state == "99"