mirror of
https://github.com/jpawlowski/hass.tibber_prices.git
synced 2026-03-30 05:13:40 +00:00
- Added new configuration options for minimum distance from average price for best and peak prices. - Updated default values for best and peak price flexibility. - Improved coordinator to handle midnight turnover and data rotation more effectively. - Refactored entity initialization to streamline device information retrieval. - Updated sensor attributes to use more descriptive names for price values. - Enhanced translations for new configuration options in English and German. - Improved unit tests for coordinator functionality, ensuring proper cleanup and async handling.
100 lines
3.9 KiB
Python
100 lines
3.9 KiB
Python
"""Test basic coordinator functions."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio # noqa: TC003
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import AsyncMock, Mock, patch
|
|
|
|
import pytest
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Generator
|
|
|
|
from custom_components.tibber_prices.coordinator import TibberPricesDataUpdateCoordinator
|
|
|
|
|
|
class TestBasicCoordinator:
|
|
"""Test basic coordinator operations."""
|
|
|
|
@pytest.fixture
|
|
def mock_hass(self, event_loop: asyncio.AbstractEventLoop) -> Mock:
|
|
"""Create a mock Home Assistant instance."""
|
|
hass = Mock()
|
|
hass.data = {}
|
|
hass.loop = event_loop
|
|
return hass
|
|
|
|
@pytest.fixture
|
|
def mock_config_entry(self) -> Mock:
|
|
"""Create a mock config entry."""
|
|
config_entry = Mock()
|
|
config_entry.unique_id = "test_home_123"
|
|
config_entry.entry_id = "test_entry"
|
|
config_entry.data = {"access_token": "test_token"}
|
|
config_entry.title = "Test Home"
|
|
return config_entry
|
|
|
|
@pytest.fixture
|
|
def mock_session(self) -> Mock:
|
|
"""Create a mock session."""
|
|
return Mock()
|
|
|
|
@pytest.fixture
|
|
def coordinator(
|
|
self, mock_hass: Mock, mock_config_entry: Mock, mock_session: Mock
|
|
) -> Generator[TibberPricesDataUpdateCoordinator]:
|
|
"""Create a coordinator instance."""
|
|
with (
|
|
patch(
|
|
"custom_components.tibber_prices.coordinator.aiohttp_client.async_get_clientsession",
|
|
return_value=mock_session,
|
|
),
|
|
patch("custom_components.tibber_prices.coordinator.Store") as mock_store_class,
|
|
):
|
|
mock_store = Mock()
|
|
mock_store.async_load = AsyncMock(return_value=None)
|
|
mock_store.async_save = AsyncMock()
|
|
mock_store_class.return_value = mock_store
|
|
|
|
coord = TibberPricesDataUpdateCoordinator(mock_hass, mock_config_entry)
|
|
|
|
# Ensure cleanup after test
|
|
yield coord
|
|
|
|
# Clean up the timer
|
|
if coord._quarter_hour_timer_cancel: # noqa: SLF001
|
|
coord._quarter_hour_timer_cancel() # noqa: SLF001
|
|
coord._quarter_hour_timer_cancel = None # noqa: SLF001
|
|
|
|
def test_coordinator_creation(self, coordinator: TibberPricesDataUpdateCoordinator) -> None:
|
|
"""Test that coordinator can be created."""
|
|
assert coordinator is not None # noqa: S101
|
|
assert hasattr(coordinator, "get_current_interval") # noqa: S101
|
|
assert hasattr(coordinator, "get_all_intervals") # noqa: S101
|
|
assert hasattr(coordinator, "get_user_profile") # noqa: S101
|
|
|
|
def test_is_main_entry(self, coordinator: TibberPricesDataUpdateCoordinator) -> None:
|
|
"""Test main entry detection."""
|
|
# First coordinator should be main entry
|
|
assert coordinator.is_main_entry() is True # noqa: S101
|
|
|
|
def test_get_user_profile_no_data(self, coordinator: TibberPricesDataUpdateCoordinator) -> None:
|
|
"""Test getting user profile when no data is cached."""
|
|
profile = coordinator.get_user_profile()
|
|
assert profile == {"last_updated": None, "cached_user_data": False} # noqa: S101
|
|
|
|
def test_get_user_homes_no_data(self, coordinator: TibberPricesDataUpdateCoordinator) -> None:
|
|
"""Test getting user homes when no data is cached."""
|
|
homes = coordinator.get_user_homes()
|
|
assert homes == [] # noqa: S101
|
|
|
|
def test_get_current_interval_data_no_data(self, coordinator: TibberPricesDataUpdateCoordinator) -> None:
|
|
"""Test getting current interval data when no data is available."""
|
|
current_data = coordinator.get_current_interval()
|
|
assert current_data is None # noqa: S101
|
|
|
|
def test_get_all_intervals_no_data(self, coordinator: TibberPricesDataUpdateCoordinator) -> None:
|
|
"""Test getting all intervals when no data is available."""
|
|
intervals = coordinator.get_all_intervals()
|
|
assert intervals == [] # noqa: S101
|