2023-07-25 08:46:53 +00:00
|
|
|
"""Define fixtures for electric kiwi tests."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2023-07-25 08:46:53 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-02-05 19:01:45 +00:00
|
|
|
from collections.abc import Generator
|
2023-09-10 23:30:25 +00:00
|
|
|
from time import time
|
2023-07-25 08:46:53 +00:00
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
|
2025-02-05 19:01:45 +00:00
|
|
|
from electrickiwi_api.model import (
|
|
|
|
AccountSummary,
|
|
|
|
CustomerConnection,
|
|
|
|
Hop,
|
|
|
|
HopIntervals,
|
|
|
|
Service,
|
|
|
|
Session,
|
|
|
|
)
|
2023-07-25 08:46:53 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.application_credentials import (
|
|
|
|
ClientCredential,
|
|
|
|
async_import_client_credential,
|
|
|
|
)
|
|
|
|
from homeassistant.components.electric_kiwi.const import DOMAIN
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
2023-09-10 23:30:25 +00:00
|
|
|
from tests.common import MockConfigEntry, load_json_value_fixture
|
2023-07-25 08:46:53 +00:00
|
|
|
|
|
|
|
CLIENT_ID = "1234"
|
|
|
|
CLIENT_SECRET = "5678"
|
|
|
|
REDIRECT_URI = "https://example.com/auth/external/callback"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2025-02-05 19:01:45 +00:00
|
|
|
async def setup_credentials(hass: HomeAssistant) -> None:
|
|
|
|
"""Fixture to setup application credentials component."""
|
|
|
|
await async_setup_component(hass, "application_credentials", {})
|
|
|
|
await async_import_client_credential(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
ClientCredential(CLIENT_ID, CLIENT_SECRET),
|
|
|
|
)
|
2023-07-25 08:46:53 +00:00
|
|
|
|
|
|
|
|
2025-02-05 19:01:45 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def electrickiwi_api() -> Generator[AsyncMock]:
|
|
|
|
"""Mock ek api and return values."""
|
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.electric_kiwi.ElectricKiwiApi",
|
|
|
|
autospec=True,
|
|
|
|
) as mock_client,
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.electric_kiwi.config_flow.ElectricKiwiApi",
|
|
|
|
new=mock_client,
|
|
|
|
),
|
|
|
|
):
|
|
|
|
client = mock_client.return_value
|
|
|
|
client.customer_number = 123456
|
|
|
|
client.electricity = Service(
|
|
|
|
identifier="00000000DDA",
|
|
|
|
service="electricity",
|
|
|
|
service_status="Y",
|
|
|
|
is_primary_service=True,
|
2023-09-10 23:30:25 +00:00
|
|
|
)
|
2025-02-05 19:01:45 +00:00
|
|
|
client.get_active_session.return_value = Session.from_dict(
|
|
|
|
load_json_value_fixture("session.json", DOMAIN)
|
|
|
|
)
|
|
|
|
client.get_hop_intervals.return_value = HopIntervals.from_dict(
|
|
|
|
load_json_value_fixture("hop_intervals.json", DOMAIN)
|
|
|
|
)
|
|
|
|
client.get_hop.return_value = Hop.from_dict(
|
|
|
|
load_json_value_fixture("get_hop.json", DOMAIN)
|
|
|
|
)
|
|
|
|
client.get_account_summary.return_value = AccountSummary.from_dict(
|
|
|
|
load_json_value_fixture("account_summary.json", DOMAIN)
|
|
|
|
)
|
|
|
|
client.get_connection_details.return_value = CustomerConnection.from_dict(
|
|
|
|
load_json_value_fixture("connection_details.json", DOMAIN)
|
|
|
|
)
|
|
|
|
yield client
|
2023-07-25 08:46:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="config_entry")
|
|
|
|
def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
|
|
|
|
"""Create mocked config entry."""
|
2024-04-06 09:07:37 +00:00
|
|
|
return MockConfigEntry(
|
2023-07-25 08:46:53 +00:00
|
|
|
title="Electric Kiwi",
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={
|
2025-02-05 19:01:45 +00:00
|
|
|
"id": "123456",
|
2023-07-25 08:46:53 +00:00
|
|
|
"auth_implementation": DOMAIN,
|
2023-09-10 23:30:25 +00:00
|
|
|
"token": {
|
|
|
|
"refresh_token": "mock-refresh-token",
|
|
|
|
"access_token": "mock-access-token",
|
|
|
|
"type": "Bearer",
|
|
|
|
"expires_in": 60,
|
|
|
|
"expires_at": time() + 60,
|
|
|
|
},
|
2023-07-25 08:46:53 +00:00
|
|
|
},
|
|
|
|
unique_id=DOMAIN,
|
2025-02-05 19:01:45 +00:00
|
|
|
version=1,
|
|
|
|
minor_version=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="config_entry2")
|
|
|
|
def mock_config_entry2(hass: HomeAssistant) -> MockConfigEntry:
|
|
|
|
"""Create mocked config entry."""
|
|
|
|
return MockConfigEntry(
|
|
|
|
title="Electric Kiwi",
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={
|
|
|
|
"id": "123457",
|
|
|
|
"auth_implementation": DOMAIN,
|
|
|
|
"token": {
|
|
|
|
"refresh_token": "mock-refresh-token",
|
|
|
|
"access_token": "mock-access-token",
|
|
|
|
"type": "Bearer",
|
|
|
|
"expires_in": 60,
|
|
|
|
"expires_at": time() + 60,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
unique_id="1234567",
|
|
|
|
version=1,
|
|
|
|
minor_version=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="migrated_config_entry")
|
|
|
|
def mock_migrated_config_entry(hass: HomeAssistant) -> MockConfigEntry:
|
|
|
|
"""Create mocked config entry."""
|
|
|
|
return MockConfigEntry(
|
|
|
|
title="Electric Kiwi",
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={
|
|
|
|
"id": "123456",
|
|
|
|
"auth_implementation": DOMAIN,
|
|
|
|
"token": {
|
|
|
|
"refresh_token": "mock-refresh-token",
|
|
|
|
"access_token": "mock-access-token",
|
|
|
|
"type": "Bearer",
|
|
|
|
"expires_in": 60,
|
|
|
|
"expires_at": time() + 60,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
unique_id="123456",
|
|
|
|
version=1,
|
|
|
|
minor_version=2,
|
2023-07-25 08:46:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-06-06 15:24:22 +00:00
|
|
|
def mock_setup_entry() -> Generator[AsyncMock]:
|
2023-07-25 08:46:53 +00:00
|
|
|
"""Mock setting up a config entry."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.electric_kiwi.async_setup_entry", return_value=True
|
|
|
|
) as mock_setup:
|
|
|
|
yield mock_setup
|
2023-09-10 23:30:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="ek_auth")
|
2025-02-05 19:01:45 +00:00
|
|
|
def electric_kiwi_auth() -> Generator[AsyncMock]:
|
2023-09-10 23:30:25 +00:00
|
|
|
"""Patch access to electric kiwi access token."""
|
|
|
|
with patch(
|
2025-02-05 19:01:45 +00:00
|
|
|
"homeassistant.components.electric_kiwi.api.ConfigEntryElectricKiwiAuth"
|
2023-09-10 23:30:25 +00:00
|
|
|
) as mock_auth:
|
|
|
|
mock_auth.return_value.async_get_access_token = AsyncMock("auth_token")
|
|
|
|
yield mock_auth
|