|
| 1 | +"""Test timer cancellation when binary sensor is disabled.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +from homeassistant.core import HomeAssistant |
| 9 | +from homeassistant.helpers.entity_registry import RegistryEntry |
| 10 | +from pytest_homeassistant_custom_component.common import MockConfigEntry |
| 11 | + |
| 12 | +from custom_components.calendar_event.binary_sensor import CalendarEventBinarySensor |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.asyncio |
| 16 | +async def test_timer_cancelled_when_entity_disabled_directly( |
| 17 | + hass: HomeAssistant, |
| 18 | +) -> None: |
| 19 | + """Test that pending timers are cancelled when entity is disabled.""" |
| 20 | + |
| 21 | + # Create a mock config entry |
| 22 | + config_entry = MockConfigEntry( |
| 23 | + domain="calendar_event", |
| 24 | + options={ |
| 25 | + "calendar_entity": "calendar.test", |
| 26 | + "summary": "Test", |
| 27 | + "name": "Test", |
| 28 | + }, |
| 29 | + ) |
| 30 | + |
| 31 | + # Create the entity |
| 32 | + entity = CalendarEventBinarySensor( |
| 33 | + hass=hass, |
| 34 | + config_entry=config_entry, |
| 35 | + name="Test", |
| 36 | + unique_id="test_id", |
| 37 | + calendar_entity_id="calendar.test", |
| 38 | + summary="Test", |
| 39 | + comparison_method="contains", |
| 40 | + ) |
| 41 | + |
| 42 | + # Create a mock handle for the timer |
| 43 | + mock_handle = MagicMock() |
| 44 | + entity._call_later_handle = mock_handle |
| 45 | + |
| 46 | + # Mock registry entry as disabled |
| 47 | + mock_registry_entry = MagicMock(spec=RegistryEntry) |
| 48 | + mock_registry_entry.disabled_by = "user" |
| 49 | + |
| 50 | + with patch.object(entity, "registry_entry", mock_registry_entry): |
| 51 | + # Simulate the entity registry update callback |
| 52 | + mock_event = MagicMock() |
| 53 | + entity._entity_registry_updated(mock_event) |
| 54 | + |
| 55 | + # Verify the timer was cancelled |
| 56 | + mock_handle.cancel.assert_called_once() |
| 57 | + assert entity._call_later_handle is None |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.asyncio |
| 61 | +async def test_timer_not_scheduled_when_disabled(hass: HomeAssistant) -> None: |
| 62 | + """Test that no timers are scheduled when entity is disabled.""" |
| 63 | + |
| 64 | + # Create a mock config entry |
| 65 | + config_entry = MockConfigEntry( |
| 66 | + domain="calendar_event", |
| 67 | + options={ |
| 68 | + "calendar_entity": "calendar.test", |
| 69 | + "summary": "Test", |
| 70 | + "name": "Test", |
| 71 | + }, |
| 72 | + ) |
| 73 | + |
| 74 | + # Create the entity |
| 75 | + entity = CalendarEventBinarySensor( |
| 76 | + hass=hass, |
| 77 | + config_entry=config_entry, |
| 78 | + name="Test", |
| 79 | + unique_id="test_id", |
| 80 | + calendar_entity_id="calendar.test", |
| 81 | + summary="Test", |
| 82 | + comparison_method="contains", |
| 83 | + ) |
| 84 | + |
| 85 | + # Mock registry entry as disabled |
| 86 | + mock_registry_entry = MagicMock(spec=RegistryEntry) |
| 87 | + mock_registry_entry.disabled_by = "user" |
| 88 | + |
| 89 | + with ( |
| 90 | + patch.object(entity, "registry_entry", mock_registry_entry), |
| 91 | + patch.object(hass.loop, "call_later") as mock_call_later, |
| 92 | + ): |
| 93 | + # Try to update state when disabled |
| 94 | + await entity._update_state() |
| 95 | + |
| 96 | + # Verify no timer was scheduled |
| 97 | + mock_call_later.assert_not_called() |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.asyncio |
| 101 | +async def test_timer_cancelled_in_state_changed_when_disabled( |
| 102 | + hass: HomeAssistant, |
| 103 | +) -> None: |
| 104 | + """Test that timers are cancelled in state changed callback when disabled.""" |
| 105 | + |
| 106 | + # Create a mock config entry |
| 107 | + config_entry = MockConfigEntry( |
| 108 | + domain="calendar_event", |
| 109 | + options={ |
| 110 | + "calendar_entity": "calendar.test", |
| 111 | + "summary": "Test", |
| 112 | + "name": "Test", |
| 113 | + }, |
| 114 | + ) |
| 115 | + |
| 116 | + # Create the entity |
| 117 | + entity = CalendarEventBinarySensor( |
| 118 | + hass=hass, |
| 119 | + config_entry=config_entry, |
| 120 | + name="Test", |
| 121 | + unique_id="test_id", |
| 122 | + calendar_entity_id="calendar.test", |
| 123 | + summary="Test", |
| 124 | + comparison_method="contains", |
| 125 | + ) |
| 126 | + |
| 127 | + # Create a mock handle for the timer |
| 128 | + mock_handle = MagicMock() |
| 129 | + entity._call_later_handle = mock_handle |
| 130 | + |
| 131 | + # Mock registry entry as disabled |
| 132 | + mock_registry_entry = MagicMock(spec=RegistryEntry) |
| 133 | + mock_registry_entry.disabled_by = "user" |
| 134 | + |
| 135 | + with patch.object(entity, "registry_entry", mock_registry_entry): |
| 136 | + # Create a mock event for the calendar entity state change |
| 137 | + mock_event = MagicMock() |
| 138 | + mock_event.data = {"entity_id": "calendar.test"} |
| 139 | + |
| 140 | + # Call the state changed callback |
| 141 | + await entity._state_changed(mock_event) |
| 142 | + |
| 143 | + # Verify the timer was cancelled |
| 144 | + mock_handle.cancel.assert_called_once() |
| 145 | + assert entity._call_later_handle is None |
0 commit comments