|
|
@@ -11,6 +11,7 @@ from custom_components.room_cal_ha.const import (
|
|
|
CONF_API_BASE_URL,
|
|
|
CONF_CALDAV_URL,
|
|
|
CONF_CALENDAR_NAME,
|
|
|
+ CONF_RESERVATION_DEFAULTS,
|
|
|
CONF_SYNC_MODE,
|
|
|
DOMAIN,
|
|
|
STORE_CURRENT,
|
|
|
@@ -287,8 +288,12 @@ async def test_check_in_calls_wifi_key(hass: HomeAssistant, coordinator: RoomCal
|
|
|
mock_switch.async_turn_on = AsyncMock()
|
|
|
coordinator._guest_mode_switch = mock_switch
|
|
|
|
|
|
- with patch("custom_components.room_cal_ha.wifi.async_set_wifi_key", new_callable=AsyncMock) as mock_wifi:
|
|
|
+ with (
|
|
|
+ patch.object(coordinator, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch("custom_components.room_cal_ha.wifi.async_set_wifi_key", new_callable=AsyncMock) as mock_wifi,
|
|
|
+ ):
|
|
|
await coordinator._async_do_check_in()
|
|
|
+ coordinator.cancel_mode_sync()
|
|
|
|
|
|
mock_wifi.assert_awaited_once_with(hass, "GuestNet", "securepass")
|
|
|
mock_switch.async_turn_on.assert_awaited_once()
|
|
|
@@ -320,8 +325,12 @@ async def test_check_in_wifi_error_still_activates_guest_mode(coordinator: RoomC
|
|
|
mock_switch.async_turn_on = AsyncMock()
|
|
|
coordinator._guest_mode_switch = mock_switch
|
|
|
|
|
|
- with patch("custom_components.room_cal_ha.wifi.async_set_wifi_key", side_effect=HomeAssistantError("hub down")):
|
|
|
+ with (
|
|
|
+ patch.object(coordinator, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch("custom_components.room_cal_ha.wifi.async_set_wifi_key", side_effect=HomeAssistantError("hub down")),
|
|
|
+ ):
|
|
|
await coordinator._async_do_check_in()
|
|
|
+ coordinator.cancel_mode_sync()
|
|
|
|
|
|
mock_switch.async_turn_on.assert_awaited_once()
|
|
|
|
|
|
@@ -360,18 +369,41 @@ async def test_start_override_turns_on_guest_mode(coordinator: RoomCalCoordinato
|
|
|
mock_switch = MagicMock()
|
|
|
mock_switch.async_turn_on = AsyncMock()
|
|
|
coordinator._guest_mode_switch = mock_switch
|
|
|
- await coordinator.async_start_override(OverrideData(current=_OVERRIDE_RES))
|
|
|
+ with patch.object(coordinator, "_is_mode_sync_on", return_value=True):
|
|
|
+ await coordinator.async_start_override(OverrideData(current=_OVERRIDE_RES))
|
|
|
mock_switch.async_turn_on.assert_awaited_once()
|
|
|
|
|
|
|
|
|
+async def test_start_override_does_not_turn_on_guest_mode_when_mode_sync_off(coordinator: RoomCalCoordinator):
|
|
|
+ mock_switch = MagicMock()
|
|
|
+ mock_switch.async_turn_on = AsyncMock()
|
|
|
+ coordinator._guest_mode_switch = mock_switch
|
|
|
+ with patch.object(coordinator, "_is_mode_sync_on", return_value=False):
|
|
|
+ await coordinator.async_start_override(OverrideData(current=_OVERRIDE_RES))
|
|
|
+ mock_switch.async_turn_on.assert_not_awaited()
|
|
|
+
|
|
|
+
|
|
|
async def test_start_override_calls_wifi_when_credentials_present(hass: HomeAssistant, coordinator: RoomCalCoordinator):
|
|
|
coordinator._guest_mode_switch = MagicMock()
|
|
|
coordinator._guest_mode_switch.async_turn_on = AsyncMock()
|
|
|
- with patch("custom_components.room_cal_ha.wifi.async_set_wifi_key", new_callable=AsyncMock) as mock_wifi:
|
|
|
+ with (
|
|
|
+ patch.object(coordinator, "_is_mode_sync_on", return_value=True),
|
|
|
+ patch("custom_components.room_cal_ha.wifi.async_set_wifi_key", new_callable=AsyncMock) as mock_wifi,
|
|
|
+ ):
|
|
|
await coordinator.async_start_override(OverrideData(current=_WIFI_OVERRIDE))
|
|
|
mock_wifi.assert_awaited_once_with(hass, "GuestNet", "secret")
|
|
|
|
|
|
|
|
|
+async def test_start_override_skips_wifi_when_mode_sync_off(coordinator: RoomCalCoordinator):
|
|
|
+ coordinator._guest_mode_switch = None
|
|
|
+ with (
|
|
|
+ patch.object(coordinator, "_is_mode_sync_on", return_value=False),
|
|
|
+ patch("custom_components.room_cal_ha.wifi.async_set_wifi_key", new_callable=AsyncMock) as mock_wifi,
|
|
|
+ ):
|
|
|
+ await coordinator.async_start_override(OverrideData(current=_WIFI_OVERRIDE))
|
|
|
+ mock_wifi.assert_not_awaited()
|
|
|
+
|
|
|
+
|
|
|
async def test_start_override_skips_wifi_when_ssid_missing(coordinator: RoomCalCoordinator):
|
|
|
coordinator._guest_mode_switch = None
|
|
|
no_ssid = ReservationData(wifi_ssid=None, wifi_key="secret")
|
|
|
@@ -394,6 +426,91 @@ async def test_start_override_uses_override_next_when_provided(coordinator: Room
|
|
|
assert coordinator.data.next.guest_name == "Override Next"
|
|
|
|
|
|
|
|
|
+async def test_start_override_applies_defaults_to_current(hass: HomeAssistant):
|
|
|
+ entry = MockConfigEntry(
|
|
|
+ domain=DOMAIN,
|
|
|
+ data={},
|
|
|
+ options={
|
|
|
+ CONF_SYNC_MODE: SYNC_MODE_MANUAL,
|
|
|
+ CONF_RESERVATION_DEFAULTS: {"wifi_ssid": "DefaultSSID", "check_out_time": "11:00:00"},
|
|
|
+ },
|
|
|
+ )
|
|
|
+ entry.add_to_hass(hass)
|
|
|
+ coord = RoomCalCoordinator(hass, entry)
|
|
|
+ await coord.async_setup()
|
|
|
+ coord._guest_mode_switch = None
|
|
|
+
|
|
|
+ partial_override = ReservationData(guest_name="Override Guest")
|
|
|
+ await coord.async_start_override(OverrideData(current=partial_override))
|
|
|
+
|
|
|
+ assert coord.data.current.guest_name == "Override Guest"
|
|
|
+ assert coord.data.current.wifi_ssid == "DefaultSSID"
|
|
|
+ assert coord.data.current.check_out_time == time(11, 0)
|
|
|
+
|
|
|
+
|
|
|
+async def test_start_override_defaults_do_not_overwrite_explicit_fields(hass: HomeAssistant):
|
|
|
+ entry = MockConfigEntry(
|
|
|
+ domain=DOMAIN,
|
|
|
+ data={},
|
|
|
+ options={
|
|
|
+ CONF_SYNC_MODE: SYNC_MODE_MANUAL,
|
|
|
+ CONF_RESERVATION_DEFAULTS: {"wifi_ssid": "DefaultSSID"},
|
|
|
+ },
|
|
|
+ )
|
|
|
+ entry.add_to_hass(hass)
|
|
|
+ coord = RoomCalCoordinator(hass, entry)
|
|
|
+ await coord.async_setup()
|
|
|
+ coord._guest_mode_switch = None
|
|
|
+
|
|
|
+ explicit_override = ReservationData(guest_name="Override Guest", wifi_ssid="ExplicitSSID")
|
|
|
+ await coord.async_start_override(OverrideData(current=explicit_override))
|
|
|
+
|
|
|
+ assert coord.data.current.wifi_ssid == "ExplicitSSID"
|
|
|
+
|
|
|
+
|
|
|
+async def test_refresh_during_override_applies_defaults_to_current(hass: HomeAssistant):
|
|
|
+ entry = MockConfigEntry(
|
|
|
+ domain=DOMAIN,
|
|
|
+ data={},
|
|
|
+ options={
|
|
|
+ CONF_SYNC_MODE: SYNC_MODE_MANUAL,
|
|
|
+ CONF_RESERVATION_DEFAULTS: {"door_code": "9999"},
|
|
|
+ },
|
|
|
+ )
|
|
|
+ entry.add_to_hass(hass)
|
|
|
+ coord = RoomCalCoordinator(hass, entry)
|
|
|
+ await coord.async_setup()
|
|
|
+ coord._guest_mode_switch = None
|
|
|
+
|
|
|
+ partial_override = ReservationData(guest_name="Override Guest")
|
|
|
+ await coord.async_start_override(OverrideData(current=partial_override))
|
|
|
+ await coord.async_refresh()
|
|
|
+
|
|
|
+ assert coord.data.current.guest_name == "Override Guest"
|
|
|
+ assert coord.data.current.door_code == "9999"
|
|
|
+
|
|
|
+
|
|
|
+async def test_start_override_applies_defaults_to_store_next(hass: HomeAssistant):
|
|
|
+ entry = MockConfigEntry(
|
|
|
+ domain=DOMAIN,
|
|
|
+ data={},
|
|
|
+ options={
|
|
|
+ CONF_SYNC_MODE: SYNC_MODE_MANUAL,
|
|
|
+ CONF_RESERVATION_DEFAULTS: {"wifi_ssid": "DefaultSSID"},
|
|
|
+ },
|
|
|
+ )
|
|
|
+ entry.add_to_hass(hass)
|
|
|
+ coord = RoomCalCoordinator(hass, entry)
|
|
|
+ await coord.async_setup()
|
|
|
+ coord._guest_mode_switch = None
|
|
|
+
|
|
|
+ await coord.store.async_set(STORE_NEXT, ReservationData(guest_name="Next Guest"))
|
|
|
+ await coord.async_start_override(OverrideData(current=_OVERRIDE_RES))
|
|
|
+
|
|
|
+ assert coord.data.next.guest_name == "Next Guest"
|
|
|
+ assert coord.data.next.wifi_ssid == "DefaultSSID"
|
|
|
+
|
|
|
+
|
|
|
async def test_end_override_clears_override_active(coordinator: RoomCalCoordinator):
|
|
|
coordinator._guest_mode_switch = None
|
|
|
coordinator._override = OverrideData(current=_OVERRIDE_RES)
|