|
|
@@ -13,6 +13,8 @@ from custom_components.room_cal_ha.const import (
|
|
|
CONF_CALENDAR_NAME,
|
|
|
CONF_RESERVATION_DEFAULTS,
|
|
|
CONF_SYNC_MODE,
|
|
|
+ CONF_ZWAVE_LOCKS,
|
|
|
+ CONF_ZWAVE_SLOT,
|
|
|
DOMAIN,
|
|
|
STORE_CURRENT,
|
|
|
STORE_NEXT,
|
|
|
@@ -845,8 +847,204 @@ async def test_unifi_diff_key_check_skipped_when_actual_key_none(coordinator):
|
|
|
assert not diff.has_changes
|
|
|
|
|
|
|
|
|
+@pytest.fixture
|
|
|
+async def coordinator_with_locks(hass: HomeAssistant):
|
|
|
+ entry = _make_entry(hass, {CONF_ZWAVE_LOCKS: ["lock.front_door"], CONF_ZWAVE_SLOT: 5})
|
|
|
+ coord = RoomCalCoordinator(hass, entry)
|
|
|
+ await coord.async_setup()
|
|
|
+ return coord
|
|
|
+
|
|
|
+
|
|
|
async def test_unifi_diff_preserves_original_diff_flags(coordinator):
|
|
|
# Even when UniFi state is clean, original checkin_needed from the base diff is kept.
|
|
|
old_inactive = ReservationData(wifi_ssid="GuestNet", wifi_key="correct-key") # no dates → not active
|
|
|
diff = _unifi_diff(coordinator, old_inactive, _ACTIVE_WIFI_RES, _DURING_WINDOW, enabled=True, key="correct-key")
|
|
|
assert diff.checkin_needed # from reservation_diff: old not active, new is active
|
|
|
+
|
|
|
+
|
|
|
+# ---------------------------------------------------------------------------
|
|
|
+# Check-in Z-Wave lock behavior
|
|
|
+# ---------------------------------------------------------------------------
|
|
|
+
|
|
|
+_ZWAVE_RES = ReservationData(
|
|
|
+ guest_name="Bob",
|
|
|
+ check_in_date=date(2026, 7, 1),
|
|
|
+ check_in_time=time(14, 0),
|
|
|
+ check_out_date=date(2026, 7, 5),
|
|
|
+ check_out_time=time(12, 0),
|
|
|
+ door_code="7890",
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+async def test_check_in_sets_lock_codes(hass: HomeAssistant, coordinator_with_locks: RoomCalCoordinator):
|
|
|
+ await coordinator_with_locks.store.async_set(STORE_NEXT, _ZWAVE_RES)
|
|
|
+ mock_switch = MagicMock()
|
|
|
+ mock_switch.async_turn_on = AsyncMock()
|
|
|
+ coordinator_with_locks._room_state_switch = mock_switch
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(coordinator_with_locks, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch.object(coordinator_with_locks, "_is_use_zwave_on", return_value=True),
|
|
|
+ patch("custom_components.room_cal_ha.coordinator.dt_util.now", return_value=_AT_CHECK_IN),
|
|
|
+ patch("custom_components.room_cal_ha.zwave_lock.async_set_lock_usercode", new_callable=AsyncMock) as mock_set,
|
|
|
+ ):
|
|
|
+ await coordinator_with_locks._async_do_check_in()
|
|
|
+ coordinator_with_locks.cancel_mode_sync()
|
|
|
+
|
|
|
+ mock_set.assert_awaited_once_with(hass, "lock.front_door", 5, "7890")
|
|
|
+ mock_switch.async_turn_on.assert_awaited_once()
|
|
|
+
|
|
|
+
|
|
|
+async def test_check_in_sets_codes_on_all_locks(hass: HomeAssistant):
|
|
|
+ entry = _make_entry(hass, {CONF_ZWAVE_LOCKS: ["lock.front", "lock.back"], CONF_ZWAVE_SLOT: 5})
|
|
|
+ coord = RoomCalCoordinator(hass, entry)
|
|
|
+ await coord.async_setup()
|
|
|
+ await coord.store.async_set(STORE_NEXT, _ZWAVE_RES)
|
|
|
+ mock_switch = MagicMock()
|
|
|
+ mock_switch.async_turn_on = AsyncMock()
|
|
|
+ coord._room_state_switch = mock_switch
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(coord, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch.object(coord, "_is_use_zwave_on", return_value=True),
|
|
|
+ patch("custom_components.room_cal_ha.coordinator.dt_util.now", return_value=_AT_CHECK_IN),
|
|
|
+ patch("custom_components.room_cal_ha.zwave_lock.async_set_lock_usercode", new_callable=AsyncMock) as mock_set,
|
|
|
+ ):
|
|
|
+ await coord._async_do_check_in()
|
|
|
+ coord.cancel_mode_sync()
|
|
|
+
|
|
|
+ assert mock_set.await_count == 2
|
|
|
+ mock_set.assert_any_await(hass, "lock.front", 5, "7890")
|
|
|
+ mock_set.assert_any_await(hass, "lock.back", 5, "7890")
|
|
|
+
|
|
|
+
|
|
|
+async def test_check_in_skips_lock_codes_when_door_code_none(hass: HomeAssistant, coordinator_with_locks: RoomCalCoordinator):
|
|
|
+ no_code = _ZWAVE_RES.__class__(
|
|
|
+ guest_name="Bob",
|
|
|
+ check_in_date=_ZWAVE_RES.check_in_date,
|
|
|
+ check_in_time=_ZWAVE_RES.check_in_time,
|
|
|
+ check_out_date=_ZWAVE_RES.check_out_date,
|
|
|
+ check_out_time=_ZWAVE_RES.check_out_time,
|
|
|
+ door_code=None,
|
|
|
+ )
|
|
|
+ await coordinator_with_locks.store.async_set(STORE_NEXT, no_code)
|
|
|
+ coordinator_with_locks._room_state_switch = None
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(coordinator_with_locks, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch.object(coordinator_with_locks, "_is_use_zwave_on", return_value=True),
|
|
|
+ patch("custom_components.room_cal_ha.coordinator.dt_util.now", return_value=_AT_CHECK_IN),
|
|
|
+ patch("custom_components.room_cal_ha.zwave_lock.async_set_lock_usercode", new_callable=AsyncMock) as mock_set,
|
|
|
+ ):
|
|
|
+ await coordinator_with_locks._async_do_check_in()
|
|
|
+ coordinator_with_locks.cancel_mode_sync()
|
|
|
+
|
|
|
+ mock_set.assert_not_awaited()
|
|
|
+
|
|
|
+
|
|
|
+async def test_check_in_skips_lock_codes_when_use_zwave_off(hass: HomeAssistant, coordinator_with_locks: RoomCalCoordinator):
|
|
|
+ await coordinator_with_locks.store.async_set(STORE_NEXT, _ZWAVE_RES)
|
|
|
+ coordinator_with_locks._room_state_switch = None
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(coordinator_with_locks, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch.object(coordinator_with_locks, "_is_use_zwave_on", return_value=False),
|
|
|
+ patch("custom_components.room_cal_ha.coordinator.dt_util.now", return_value=_AT_CHECK_IN),
|
|
|
+ patch("custom_components.room_cal_ha.zwave_lock.async_set_lock_usercode", new_callable=AsyncMock) as mock_set,
|
|
|
+ ):
|
|
|
+ await coordinator_with_locks._async_do_check_in()
|
|
|
+ coordinator_with_locks.cancel_mode_sync()
|
|
|
+
|
|
|
+ mock_set.assert_not_awaited()
|
|
|
+
|
|
|
+
|
|
|
+async def test_check_in_skips_lock_codes_when_no_locks_configured(hass: HomeAssistant, coordinator: RoomCalCoordinator):
|
|
|
+ # coordinator fixture has no CONF_ZWAVE_LOCKS in options
|
|
|
+ await coordinator.store.async_set(STORE_NEXT, _ZWAVE_RES)
|
|
|
+ coordinator._room_state_switch = None
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(coordinator, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch.object(coordinator, "_is_use_zwave_on", return_value=True),
|
|
|
+ patch("custom_components.room_cal_ha.coordinator.dt_util.now", return_value=_AT_CHECK_IN),
|
|
|
+ patch("custom_components.room_cal_ha.zwave_lock.async_set_lock_usercode", new_callable=AsyncMock) as mock_set,
|
|
|
+ ):
|
|
|
+ await coordinator._async_do_check_in()
|
|
|
+ coordinator.cancel_mode_sync()
|
|
|
+
|
|
|
+ mock_set.assert_not_awaited()
|
|
|
+
|
|
|
+
|
|
|
+async def test_check_in_lock_error_still_activates_guest_mode(coordinator_with_locks: RoomCalCoordinator):
|
|
|
+ await coordinator_with_locks.store.async_set(STORE_NEXT, _ZWAVE_RES)
|
|
|
+ mock_switch = MagicMock()
|
|
|
+ mock_switch.async_turn_on = AsyncMock()
|
|
|
+ coordinator_with_locks._room_state_switch = mock_switch
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(coordinator_with_locks, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch.object(coordinator_with_locks, "_is_use_zwave_on", return_value=True),
|
|
|
+ patch("custom_components.room_cal_ha.coordinator.dt_util.now", return_value=_AT_CHECK_IN),
|
|
|
+ patch(
|
|
|
+ "custom_components.room_cal_ha.zwave_lock.async_set_lock_usercode",
|
|
|
+ side_effect=HomeAssistantError("lock offline"),
|
|
|
+ ),
|
|
|
+ ):
|
|
|
+ await coordinator_with_locks._async_do_check_in()
|
|
|
+ coordinator_with_locks.cancel_mode_sync()
|
|
|
+
|
|
|
+ mock_switch.async_turn_on.assert_awaited_once()
|
|
|
+
|
|
|
+
|
|
|
+# ---------------------------------------------------------------------------
|
|
|
+# Check-out Z-Wave lock behavior
|
|
|
+# ---------------------------------------------------------------------------
|
|
|
+
|
|
|
+async def test_check_out_clears_lock_codes(hass: HomeAssistant, coordinator_with_locks: RoomCalCoordinator):
|
|
|
+ await coordinator_with_locks.store.async_set(STORE_CURRENT, _ZWAVE_RES)
|
|
|
+ mock_switch = MagicMock()
|
|
|
+ mock_switch.async_turn_off = AsyncMock()
|
|
|
+ coordinator_with_locks._room_state_switch = mock_switch
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(coordinator_with_locks, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch.object(coordinator_with_locks, "_is_use_zwave_on", return_value=True),
|
|
|
+ patch("custom_components.room_cal_ha.zwave_lock.async_clear_lock_usercode", new_callable=AsyncMock) as mock_clear,
|
|
|
+ ):
|
|
|
+ await coordinator_with_locks._async_do_check_out()
|
|
|
+
|
|
|
+ mock_clear.assert_awaited_once_with(hass, "lock.front_door", 5)
|
|
|
+ mock_switch.async_turn_off.assert_awaited_once()
|
|
|
+
|
|
|
+
|
|
|
+async def test_check_out_skips_lock_codes_when_use_zwave_off(coordinator_with_locks: RoomCalCoordinator):
|
|
|
+ await coordinator_with_locks.store.async_set(STORE_CURRENT, _ZWAVE_RES)
|
|
|
+ coordinator_with_locks._room_state_switch = None
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(coordinator_with_locks, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch.object(coordinator_with_locks, "_is_use_zwave_on", return_value=False),
|
|
|
+ patch("custom_components.room_cal_ha.zwave_lock.async_clear_lock_usercode", new_callable=AsyncMock) as mock_clear,
|
|
|
+ ):
|
|
|
+ await coordinator_with_locks._async_do_check_out()
|
|
|
+
|
|
|
+ mock_clear.assert_not_awaited()
|
|
|
+
|
|
|
+
|
|
|
+async def test_check_out_lock_error_still_deactivates_guest_mode(coordinator_with_locks: RoomCalCoordinator):
|
|
|
+ await coordinator_with_locks.store.async_set(STORE_CURRENT, _ZWAVE_RES)
|
|
|
+ mock_switch = MagicMock()
|
|
|
+ mock_switch.async_turn_off = AsyncMock()
|
|
|
+ coordinator_with_locks._room_state_switch = mock_switch
|
|
|
+
|
|
|
+ with (
|
|
|
+ patch.object(coordinator_with_locks, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch.object(coordinator_with_locks, "_is_use_zwave_on", return_value=True),
|
|
|
+ patch(
|
|
|
+ "custom_components.room_cal_ha.zwave_lock.async_clear_lock_usercode",
|
|
|
+ side_effect=HomeAssistantError("lock offline"),
|
|
|
+ ),
|
|
|
+ ):
|
|
|
+ await coordinator_with_locks._async_do_check_out()
|
|
|
+
|
|
|
+ mock_switch.async_turn_off.assert_awaited_once()
|