test_buttons.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """Unit tests for ForceRefreshButton and PromoteFromNextButton.
  2. Uses object.__new__ to bypass HA entity setup — valid because async_press
  3. only calls coordinator methods, not HA lifecycle hooks.
  4. """
  5. from unittest.mock import AsyncMock, MagicMock
  6. import pytest
  7. from custom_components.room_cal_ha.button import ForceRefreshButton, PromoteFromNextButton
  8. def _button(cls):
  9. btn = object.__new__(cls)
  10. btn._coordinator = MagicMock()
  11. btn._coordinator.async_refresh = AsyncMock()
  12. btn._coordinator.async_promote_next_to_current = AsyncMock()
  13. return btn
  14. async def test_force_refresh_calls_coordinator_refresh():
  15. btn = _button(ForceRefreshButton)
  16. await btn.async_press()
  17. btn._coordinator.async_refresh.assert_awaited_once()
  18. async def test_promote_from_next_calls_coordinator_promote():
  19. btn = _button(PromoteFromNextButton)
  20. await btn.async_press()
  21. btn._coordinator.async_promote_next_to_current.assert_awaited_once()
  22. async def test_force_refresh_does_not_call_promote():
  23. btn = _button(ForceRefreshButton)
  24. await btn.async_press()
  25. btn._coordinator.async_promote_next_to_current.assert_not_called()
  26. async def test_promote_does_not_call_refresh():
  27. btn = _button(PromoteFromNextButton)
  28. await btn.async_press()
  29. btn._coordinator.async_refresh.assert_not_called()