test_switches.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """Unit tests for GuestModeSwitch and ModeSyncSwitch.
  2. Uses object.__new__ to bypass HA entity setup. async_write_ha_state is
  3. replaced with a MagicMock to avoid needing a real HA instance.
  4. """
  5. from unittest.mock import AsyncMock, MagicMock, patch
  6. import pytest
  7. from custom_components.room_cal_ha.switch import GuestModeSwitch, ModeSyncSwitch
  8. def _guest_mode():
  9. s = object.__new__(GuestModeSwitch)
  10. s._state = False
  11. s._deferred = True
  12. s._coordinator = MagicMock()
  13. s.async_write_ha_state = MagicMock()
  14. return s
  15. def _mode_sync():
  16. s = object.__new__(ModeSyncSwitch)
  17. s._state = False
  18. s._coordinator = MagicMock()
  19. s._coordinator.cancel_mode_sync = MagicMock()
  20. s.async_write_ha_state = MagicMock()
  21. return s
  22. # ---------------------------------------------------------------------------
  23. # GuestModeSwitch — basic state
  24. # ---------------------------------------------------------------------------
  25. async def test_guest_mode_initial_state_is_false():
  26. s = _guest_mode()
  27. assert s.is_on is False
  28. async def test_guest_mode_turn_on_sets_state():
  29. s = _guest_mode()
  30. s._deferred = False
  31. await s.async_turn_on()
  32. assert s.is_on is True
  33. async def test_guest_mode_turn_off_sets_state():
  34. s = _guest_mode()
  35. s._state = True
  36. s._deferred = False
  37. await s.async_turn_off()
  38. assert s.is_on is False
  39. # ---------------------------------------------------------------------------
  40. # GuestModeSwitch — deferred write behavior
  41. # ---------------------------------------------------------------------------
  42. async def test_guest_mode_deferred_suppresses_write():
  43. s = _guest_mode()
  44. assert s._deferred is True
  45. s.async_write_ha_state() # should be a no-op when deferred
  46. # The real async_write_ha_state is replaced; calling it when deferred returns early.
  47. # We verify by re-implementing the check:
  48. with patch.object(type(s).__bases__[0], "async_write_ha_state") as mock_parent:
  49. s.async_write_ha_state = GuestModeSwitch.async_write_ha_state.__get__(s, type(s))
  50. s.async_write_ha_state()
  51. mock_parent.assert_not_called()
  52. def test_guest_mode_commit_state_unblocks_and_writes():
  53. s = _guest_mode()
  54. s._state = True
  55. real_write = MagicMock()
  56. with patch(
  57. "homeassistant.helpers.entity.Entity.async_write_ha_state",
  58. real_write,
  59. ):
  60. s.commit_state()
  61. assert s._deferred is False
  62. real_write.assert_called_once()
  63. # ---------------------------------------------------------------------------
  64. # ModeSyncSwitch — turn off cancels mode sync
  65. # ---------------------------------------------------------------------------
  66. async def test_mode_sync_turn_off_cancels_sync():
  67. s = _mode_sync()
  68. s._state = True
  69. await s.async_turn_off()
  70. assert s.is_on is False
  71. s._coordinator.cancel_mode_sync.assert_called_once()
  72. async def test_mode_sync_turn_on_does_not_cancel_sync():
  73. s = _mode_sync()
  74. await s.async_turn_on()
  75. s._coordinator.cancel_mode_sync.assert_not_called()
  76. async def test_mode_sync_initial_state_is_false():
  77. s = _mode_sync()
  78. assert s.is_on is False