diff --git a/clive/__private/core/world.py b/clive/__private/core/world.py index 6a6d2c10968737992762d11be2e9bf74ef083d93..698e078c7f0594c484936bcc2325dd430c00a8c8 100644 --- a/clive/__private/core/world.py +++ b/clive/__private/core/world.py @@ -26,6 +26,7 @@ from clive.exceptions import ProfileNotLoadedError if TYPE_CHECKING: from collections.abc import Iterable + from datetime import timedelta from types import TracebackType from typing_extensions import Self @@ -203,9 +204,13 @@ class World: await self.switch_profile(profile) await self.commands.sync_state_with_beekeeper() - async def load_profile(self, profile_name: str, password: str) -> None: + async def load_profile( + self, profile_name: str, password: str, *, time: timedelta | None = None, permanent: bool = True + ) -> None: assert not self.app_state.is_unlocked, "Application is already unlocked" - await self.commands.unlock(profile_name=profile_name, password=password, permanent=True) + ( + await self.commands.unlock(profile_name=profile_name, password=password, time=time, permanent=permanent) + ).raise_if_error_occurred() await self.load_profile_based_on_beekepeer() async def switch_profile(self, new_profile: Profile | None) -> None: diff --git a/clive/__private/ui/screens/unlock/unlock.py b/clive/__private/ui/screens/unlock/unlock.py index df00784f987974fd36828d54630e8a860a507f88..09886023e1aa94d60b6c0f1628999c2690ef7365 100644 --- a/clive/__private/ui/screens/unlock/unlock.py +++ b/clive/__private/ui/screens/unlock/unlock.py @@ -4,6 +4,7 @@ import contextlib from datetime import timedelta from typing import TYPE_CHECKING +from beekeepy.exceptions import InvalidPasswordError from textual import on from textual.app import InvalidModeError from textual.containers import Horizontal @@ -12,6 +13,7 @@ from textual.widgets import Button, Checkbox, Static from clive.__private.core.constants.tui.messages import PRESS_HELP_MESSAGE from clive.__private.core.profile import Profile +from clive.__private.logger import logger from clive.__private.ui.clive_widget import CliveWidget from clive.__private.ui.forms.create_profile.create_profile_form import CreateProfileForm from clive.__private.ui.get_css import get_relative_css_path @@ -113,17 +115,20 @@ class Unlock(BaseScreen): if not password_input.validate_passed() or not lock_after_time.is_valid: return - if not ( - await self.commands.unlock( + try: + await self.world.load_profile( profile_name=select_profile.value_ensure, password=password_input.value_or_error, permanent=lock_after_time.should_stay_unlocked, time=lock_after_time.lock_duration, ) - ).success: + except InvalidPasswordError: + logger.error( + f"Profile `{select_profile.value_ensure}` was not unlocked " + "because entered password is invalid, skipping switching modes" + ) return - await self.world.load_profile_based_on_beekepeer() await self.app.switch_mode("dashboard") self._remove_welcome_modes() self.app.update_alarms_data_on_newest_node_data(suppress_cancelled_error=True)