1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-23 17:00:13 +01:00
Files
core/tests/components/forecast_solar/conftest.py
T
Artem Khvastunov b056723b98 Add multi-plane support for Forecast.Solar integration (#160058)
Co-authored-by: Junie <noreply@jb.gg>
Co-authored-by: Junie <junie@jetbrains.com>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-04-01 17:42:17 +02:00

149 lines
4.8 KiB
Python

"""Fixtures for Forecast.Solar integration tests."""
from collections.abc import Generator
from datetime import datetime, timedelta
from typing import Any
from unittest.mock import AsyncMock, MagicMock, patch
from forecast_solar import models
import pytest
from homeassistant.components.forecast_solar.const import (
CONF_AZIMUTH,
CONF_DAMPING_EVENING,
CONF_DAMPING_MORNING,
CONF_DECLINATION,
CONF_INVERTER_SIZE,
CONF_MODULES_POWER,
DOMAIN,
SUBENTRY_TYPE_PLANE,
)
from homeassistant.config_entries import ConfigSubentryData
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import HomeAssistant
from homeassistant.util import dt as dt_util
from tests.common import MockConfigEntry
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Mock setting up a config entry."""
with patch(
"homeassistant.components.forecast_solar.async_setup_entry", return_value=True
) as mock_setup:
yield mock_setup
@pytest.fixture
def api_key_present() -> bool:
"""Return whether an API key should be present in the config entry options."""
return True
@pytest.fixture
def mock_config_entry(api_key_present: bool) -> MockConfigEntry:
"""Return the default mocked config entry."""
options: dict[str, Any] = {
CONF_DAMPING_MORNING: 0.5,
CONF_DAMPING_EVENING: 0.5,
CONF_INVERTER_SIZE: 2000,
}
if api_key_present:
options[CONF_API_KEY] = "abcdef1234567890"
return MockConfigEntry(
title="Green House",
unique_id="unique",
version=3,
domain=DOMAIN,
data={
CONF_LATITUDE: 52.42,
CONF_LONGITUDE: 4.42,
},
options=options,
subentries_data=[
ConfigSubentryData(
data={
CONF_DECLINATION: 30,
CONF_AZIMUTH: 190,
CONF_MODULES_POWER: 5100,
},
subentry_id="mock_plane_id",
subentry_type=SUBENTRY_TYPE_PLANE,
title="30° / 190° / 5100W",
unique_id=None,
),
],
)
@pytest.fixture
def mock_forecast_solar(hass: HomeAssistant) -> Generator[MagicMock]:
"""Return a mocked Forecast.Solar client.
hass fixture included because it sets the time zone.
"""
with patch(
"homeassistant.components.forecast_solar.coordinator.ForecastSolar",
autospec=True,
) as forecast_solar_mock:
forecast_solar = forecast_solar_mock.return_value
now = datetime(2021, 6, 27, 6, 0, tzinfo=dt_util.get_default_time_zone())
estimate = MagicMock(spec=models.Estimate)
estimate.now.return_value = now
estimate.timezone = "Europe/Amsterdam"
estimate.api_rate_limit = 60
estimate.account_type.value = "public"
estimate.energy_production_today = 100000
estimate.energy_production_today_remaining = 50000
estimate.energy_production_tomorrow = 200000
estimate.power_production_now = 300000
estimate.power_highest_peak_time_today = datetime(
2021, 6, 27, 13, 0, tzinfo=dt_util.get_default_time_zone()
)
estimate.power_highest_peak_time_tomorrow = datetime(
2021, 6, 27, 14, 0, tzinfo=dt_util.get_default_time_zone()
)
estimate.energy_current_hour = 800000
estimate.power_production_at_time.side_effect = {
now + timedelta(hours=1): 400000,
now + timedelta(hours=12): 600000,
now + timedelta(hours=24): 700000,
}.get
estimate.sum_energy_production.side_effect = {
1: 900000,
}.get
estimate.watts = {
datetime(2021, 6, 27, 13, 0, tzinfo=dt_util.get_default_time_zone()): 10,
datetime(2022, 6, 27, 13, 0, tzinfo=dt_util.get_default_time_zone()): 100,
}
estimate.wh_days = {
datetime(2021, 6, 27, 13, 0, tzinfo=dt_util.get_default_time_zone()): 20,
datetime(2022, 6, 27, 13, 0, tzinfo=dt_util.get_default_time_zone()): 200,
}
estimate.wh_period = {
datetime(2021, 6, 27, 13, 0, tzinfo=dt_util.get_default_time_zone()): 30,
datetime(2022, 6, 27, 13, 0, tzinfo=dt_util.get_default_time_zone()): 300,
}
forecast_solar.estimate.return_value = estimate
yield forecast_solar
@pytest.fixture
async def init_integration(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_forecast_solar: MagicMock,
) -> MockConfigEntry:
"""Set up the Forecast.Solar integration for testing."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
return mock_config_entry