2024-01-25 21:54:47 +10:00
|
|
|
"""Tests for the Teslemetry integration."""
|
2024-01-29 06:04:44 +10:00
|
|
|
|
2025-12-30 01:40:45 +10:00
|
|
|
import time
|
2024-01-29 06:04:44 +10:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
2024-09-27 23:06:09 +10:00
|
|
|
from syrupy.assertion import SnapshotAssertion
|
2024-01-29 06:04:44 +10:00
|
|
|
|
|
|
|
|
from homeassistant.components.teslemetry.const import DOMAIN
|
|
|
|
|
from homeassistant.const import Platform
|
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
|
|
|
|
|
2025-12-30 01:40:45 +10:00
|
|
|
def mock_config_entry() -> MockConfigEntry:
|
|
|
|
|
"""Create a mock config entry."""
|
|
|
|
|
|
|
|
|
|
return MockConfigEntry(
|
|
|
|
|
domain=DOMAIN,
|
|
|
|
|
version=2,
|
|
|
|
|
unique_id="abc-123",
|
|
|
|
|
data={
|
|
|
|
|
"auth_implementation": DOMAIN,
|
|
|
|
|
"token": {
|
|
|
|
|
"access_token": "test_access_token",
|
|
|
|
|
"refresh_token": "test_refresh_token",
|
|
|
|
|
"expires_at": int(time.time()) + 3600,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-01-29 02:44:05 +10:00
|
|
|
async def setup_platform(
|
2025-12-30 01:40:45 +10:00
|
|
|
hass: HomeAssistant,
|
|
|
|
|
platforms: list[Platform] | None = None,
|
2025-01-29 02:44:05 +10:00
|
|
|
) -> MockConfigEntry:
|
2024-01-29 06:04:44 +10:00
|
|
|
"""Set up the Teslemetry platform."""
|
|
|
|
|
|
2025-12-30 01:40:45 +10:00
|
|
|
mock_entry = mock_config_entry()
|
2024-01-29 06:04:44 +10:00
|
|
|
mock_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
|
|
if platforms is None:
|
|
|
|
|
await hass.config_entries.async_setup(mock_entry.entry_id)
|
|
|
|
|
else:
|
|
|
|
|
with patch("homeassistant.components.teslemetry.PLATFORMS", platforms):
|
|
|
|
|
await hass.config_entries.async_setup(mock_entry.entry_id)
|
2024-05-12 22:25:09 +10:00
|
|
|
await hass.async_block_till_done()
|
2024-01-29 06:04:44 +10:00
|
|
|
|
|
|
|
|
return mock_entry
|
|
|
|
|
|
|
|
|
|
|
2025-01-29 02:43:41 +10:00
|
|
|
async def reload_platform(
|
|
|
|
|
hass: HomeAssistant, entry: MockConfigEntry, platforms: list[Platform] | None = None
|
|
|
|
|
):
|
|
|
|
|
"""Reload the Teslemetry platform."""
|
|
|
|
|
|
|
|
|
|
if platforms is None:
|
|
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|
|
|
|
|
else:
|
|
|
|
|
with patch("homeassistant.components.teslemetry.PLATFORMS", platforms):
|
|
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
|
|
|
2024-01-29 06:04:44 +10:00
|
|
|
def assert_entities(
|
|
|
|
|
hass: HomeAssistant,
|
|
|
|
|
entry_id: str,
|
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test that all entities match their snapshot."""
|
2024-05-12 22:25:09 +10:00
|
|
|
|
2024-01-29 06:04:44 +10:00
|
|
|
entity_entries = er.async_entries_for_config_entry(entity_registry, entry_id)
|
|
|
|
|
|
|
|
|
|
assert entity_entries
|
|
|
|
|
for entity_entry in entity_entries:
|
|
|
|
|
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
|
|
|
|
|
assert (state := hass.states.get(entity_entry.entity_id))
|
|
|
|
|
assert state == snapshot(name=f"{entity_entry.entity_id}-state")
|
2024-03-16 20:54:37 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def assert_entities_alt(
|
|
|
|
|
hass: HomeAssistant,
|
|
|
|
|
entry_id: str,
|
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
|
) -> None:
|
2026-02-03 21:57:14 +10:00
|
|
|
"""Test that all entities match their alt snapshot.
|
|
|
|
|
|
|
|
|
|
The `_alt` test variants use VEHICLE_DATA_ALT fixture data to verify
|
|
|
|
|
entity behavior with alternative vehicle state values (different charge
|
|
|
|
|
levels, door states, climate settings, etc.). This ensures entities
|
|
|
|
|
handle varied data correctly.
|
|
|
|
|
"""
|
2024-03-16 20:54:37 +10:00
|
|
|
entity_entries = er.async_entries_for_config_entry(entity_registry, entry_id)
|
|
|
|
|
|
|
|
|
|
assert entity_entries
|
|
|
|
|
for entity_entry in entity_entries:
|
|
|
|
|
assert (state := hass.states.get(entity_entry.entity_id))
|
|
|
|
|
assert state == snapshot(name=f"{entity_entry.entity_id}-statealt")
|