Files
core/tests/components/tesla_fleet/conftest.py
T
Brett AdamsandG Johansson a2c2488c8b Add Tesla Fleet integration (#122019)
* Add Tesla Fleet

* Remove debug

* Improvements

* Fix refresh and stage tests

* Working oauth flow test

* Config Flow tests

* Fixes

* fixes

* Remove comment

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Remove TYPE_CHECKING

* Add more tests

* More tests

* More tests

* revert teslemetry change

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
2024-07-19 09:05:27 +02:00

143 lines
3.8 KiB
Python

"""Fixtures for Tessie."""
from __future__ import annotations
from copy import deepcopy
import time
from unittest.mock import patch
import jwt
import pytest
from homeassistant.components.application_credentials import (
ClientCredential,
async_import_client_credential,
)
from homeassistant.components.tesla_fleet.application_credentials import CLIENT_ID
from homeassistant.components.tesla_fleet.const import DOMAIN, SCOPES
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from .const import LIVE_STATUS, PRODUCTS, SITE_INFO, VEHICLE_DATA, VEHICLE_ONLINE
from tests.common import MockConfigEntry
UID = "abc-123"
@pytest.fixture(name="expires_at")
def mock_expires_at() -> int:
"""Fixture to set the oauth token expiration time."""
return time.time() + 3600
@pytest.fixture(name="scopes")
def mock_scopes() -> list[str]:
"""Fixture to set the scopes present in the OAuth token."""
return SCOPES
@pytest.fixture
def normal_config_entry(expires_at: int, scopes: list[str]) -> MockConfigEntry:
"""Create Tesla Fleet entry in Home Assistant."""
access_token = jwt.encode(
{
"sub": UID,
"aud": [],
"scp": scopes,
"ou_code": "NA",
},
key="",
algorithm="none",
)
return MockConfigEntry(
domain=DOMAIN,
title=UID,
unique_id=UID,
data={
"auth_implementation": DOMAIN,
"token": {
"status": 0,
"userid": UID,
"access_token": access_token,
"refresh_token": "mock-refresh-token",
"expires_at": expires_at,
"scope": ",".join(scopes),
},
},
)
@pytest.fixture(autouse=True)
async def setup_credentials(hass: HomeAssistant) -> None:
"""Fixture to setup credentials."""
assert await async_setup_component(hass, "application_credentials", {})
await async_import_client_credential(
hass,
DOMAIN,
ClientCredential(CLIENT_ID, ""),
DOMAIN,
)
@pytest.fixture(autouse=True)
def mock_products():
"""Mock Tesla Fleet Api products method."""
with patch(
"homeassistant.components.tesla_fleet.TeslaFleetApi.products",
return_value=PRODUCTS,
) as mock_products:
yield mock_products
@pytest.fixture(autouse=True)
def mock_vehicle_state():
"""Mock Tesla Fleet API Vehicle Specific vehicle method."""
with patch(
"homeassistant.components.tesla_fleet.VehicleSpecific.vehicle",
return_value=VEHICLE_ONLINE,
) as mock_vehicle:
yield mock_vehicle
@pytest.fixture(autouse=True)
def mock_vehicle_data():
"""Mock Tesla Fleet API Vehicle Specific vehicle_data method."""
with patch(
"homeassistant.components.tesla_fleet.VehicleSpecific.vehicle_data",
return_value=VEHICLE_DATA,
) as mock_vehicle_data:
yield mock_vehicle_data
@pytest.fixture(autouse=True)
def mock_wake_up():
"""Mock Tesla Fleet API Vehicle Specific wake_up method."""
with patch(
"homeassistant.components.tesla_fleet.VehicleSpecific.wake_up",
return_value=VEHICLE_ONLINE,
) as mock_wake_up:
yield mock_wake_up
@pytest.fixture(autouse=True)
def mock_live_status():
"""Mock Teslemetry Energy Specific live_status method."""
with patch(
"homeassistant.components.tesla_fleet.EnergySpecific.live_status",
side_effect=lambda: deepcopy(LIVE_STATUS),
) as mock_live_status:
yield mock_live_status
@pytest.fixture(autouse=True)
def mock_site_info():
"""Mock Teslemetry Energy Specific site_info method."""
with patch(
"homeassistant.components.tesla_fleet.EnergySpecific.site_info",
side_effect=lambda: deepcopy(SITE_INFO),
) as mock_live_status:
yield mock_live_status