"""Unit tests for GuestModeSwitch and ModeSyncSwitch. Uses object.__new__ to bypass HA entity setup. async_write_ha_state is replaced with a MagicMock to avoid needing a real HA instance. """ from unittest.mock import AsyncMock, MagicMock, patch import pytest from custom_components.room_cal_ha.switch import GuestModeSwitch, ModeSyncSwitch def _guest_mode(): s = object.__new__(GuestModeSwitch) s._state = False s._deferred = True s._coordinator = MagicMock() s.async_write_ha_state = MagicMock() return s def _mode_sync(): s = object.__new__(ModeSyncSwitch) s._state = False s._coordinator = MagicMock() s._coordinator.cancel_mode_sync = MagicMock() s.async_write_ha_state = MagicMock() return s # --------------------------------------------------------------------------- # GuestModeSwitch — basic state # --------------------------------------------------------------------------- async def test_guest_mode_initial_state_is_false(): s = _guest_mode() assert s.is_on is False async def test_guest_mode_turn_on_sets_state(): s = _guest_mode() s._deferred = False await s.async_turn_on() assert s.is_on is True async def test_guest_mode_turn_off_sets_state(): s = _guest_mode() s._state = True s._deferred = False await s.async_turn_off() assert s.is_on is False # --------------------------------------------------------------------------- # GuestModeSwitch — deferred write behavior # --------------------------------------------------------------------------- async def test_guest_mode_deferred_suppresses_write(): s = _guest_mode() assert s._deferred is True s.async_write_ha_state() # should be a no-op when deferred # The real async_write_ha_state is replaced; calling it when deferred returns early. # We verify by re-implementing the check: with patch.object(type(s).__bases__[0], "async_write_ha_state") as mock_parent: s.async_write_ha_state = GuestModeSwitch.async_write_ha_state.__get__(s, type(s)) s.async_write_ha_state() mock_parent.assert_not_called() def test_guest_mode_commit_state_unblocks_and_writes(): s = _guest_mode() s._state = True real_write = MagicMock() with patch( "homeassistant.helpers.entity.Entity.async_write_ha_state", real_write, ): s.commit_state() assert s._deferred is False real_write.assert_called_once() # --------------------------------------------------------------------------- # ModeSyncSwitch — turn off cancels mode sync # --------------------------------------------------------------------------- async def test_mode_sync_turn_off_cancels_sync(): s = _mode_sync() s._state = True await s.async_turn_off() assert s.is_on is False s._coordinator.cancel_mode_sync.assert_called_once() async def test_mode_sync_turn_on_does_not_cancel_sync(): s = _mode_sync() await s.async_turn_on() s._coordinator.cancel_mode_sync.assert_not_called() async def test_mode_sync_initial_state_is_false(): s = _mode_sync() assert s.is_on is False