"""Fixtures for the Nord Pool integration.""" from collections.abc import AsyncGenerator from http import HTTPStatus import json from typing import Any from pynordpool import API, NordPoolClient import pytest from homeassistant.components.nordpool.const import DOMAIN, PLATFORMS from homeassistant.config_entries import SOURCE_USER from homeassistant.const import Platform from homeassistant.core import HomeAssistant from . import ENTRY_CONFIG from tests.common import MockConfigEntry, load_fixture, patch from tests.test_util.aiohttp import AiohttpClientMocker @pytest.fixture(name="mock_asyncio_sleep", autouse=True) async def mock_asyncio_sleep() -> AsyncGenerator[None]: """Mock asyncio.sleep to avoid actual sleeping during tests.""" with patch( "pynordpool.asyncio.sleep", ): yield @pytest.fixture(name="load_platforms") async def patch_platform_constant() -> list[Platform]: """Return list of platforms to load.""" return PLATFORMS @pytest.fixture async def load_int( hass: HomeAssistant, get_client: NordPoolClient, load_platforms: list[Platform], ) -> MockConfigEntry: """Set up the Nord Pool integration in Home Assistant.""" config_entry = MockConfigEntry( domain=DOMAIN, source=SOURCE_USER, data=ENTRY_CONFIG, ) config_entry.add_to_hass(hass) with patch("homeassistant.components.nordpool.PLATFORMS", load_platforms): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() return config_entry @pytest.fixture(name="get_client") async def get_data_from_library( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, load_json: list[dict[str, Any]], ) -> AsyncGenerator[NordPoolClient]: """Retrieve data from Nord Pool library.""" aioclient_mock.request( "GET", url=API + "/DayAheadPrices", params={ "date": "2025-10-01", "market": "DayAhead", "deliveryArea": "SE3,SE4", "currency": "SEK", }, json=load_json[0], ) aioclient_mock.request( "GET", url=API + "/DayAheadPrices", params={ "date": "2025-10-01", "market": "DayAhead", "deliveryArea": "SE3", "currency": "EUR", }, json=load_json[0], ) aioclient_mock.request( "GET", url=API + "/DayAheadPrices", params={ "date": "2025-10-01", "market": "DayAhead", "deliveryArea": "SE3,SE4", "currency": "EUR", }, json=load_json[0], ) aioclient_mock.request( "GET", url=API + "/DayAheadPrices", params={ "date": "2025-09-30", "market": "DayAhead", "deliveryArea": "SE3,SE4", "currency": "SEK", }, json=load_json[1], ) aioclient_mock.request( "GET", url=API + "/DayAheadPrices", params={ "date": "2025-10-02", "market": "DayAhead", "deliveryArea": "SE3,SE4", "currency": "SEK", }, json=load_json[2], ) aioclient_mock.request( "GET", url=API + "/DayAheadPrices", params={ "date": "2025-10-03", "market": "DayAhead", "deliveryArea": "SE3,SE4", "currency": "SEK", }, status=HTTPStatus.NO_CONTENT, ) aioclient_mock.request( "GET", url=API + "/DayAheadPrices", params={ "date": "2025-10-04", "market": "DayAhead", "deliveryArea": "SE3,SE4", "currency": "SEK", }, status=HTTPStatus.NO_CONTENT, ) client = NordPoolClient(aioclient_mock.create_session(hass.loop)) yield client await client._session.close() @pytest.fixture(name="load_json") def load_json_from_fixture(load_data: list[str, str, str]) -> list[dict[str, Any]]: """Load fixture with json data and return.""" return [ json.loads(load_data[0]), json.loads(load_data[1]), json.loads(load_data[2]), ] @pytest.fixture(name="load_data", scope="package") def load_data_from_fixture() -> list[str, str, str]: """Load fixture with fixture data and return.""" return [ load_fixture("delivery_period_today.json", DOMAIN), load_fixture("delivery_period_yesterday.json", DOMAIN), load_fixture("delivery_period_tomorrow.json", DOMAIN), ]