From 297046adbce62c831065bf3a29998292e65bb431 Mon Sep 17 00:00:00 2001 From: Trey Moen Date: Tue, 20 May 2025 20:14:29 -0700 Subject: [PATCH] Profile dataclass --- system/hardware/tici/esim.py | 35 +++++++++++++++---------- system/hardware/tici/tests/test_esim.py | 8 +++--- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/system/hardware/tici/esim.py b/system/hardware/tici/esim.py index 1c37454f5a..abb500a184 100755 --- a/system/hardware/tici/esim.py +++ b/system/hardware/tici/esim.py @@ -5,7 +5,14 @@ import os import shutil import subprocess import time +from dataclasses import dataclass +@dataclass +class Profile: + iccid: str + nickname: str + enabled: bool + provider: str class LPAError(RuntimeError): pass @@ -25,33 +32,33 @@ class LPA: if shutil.which('lpac') is None: raise LPAError('lpac not found, must be installed!') - def list_profiles(self) -> list[dict[str, str]]: + def list_profiles(self) -> list[Profile]: msgs = self._invoke('profile', 'list') self._validate_successful(msgs) - return [{ - 'iccid': p['iccid'], - 'nickname': p['profileNickname'], - 'enabled': p['profileState'] == 'enabled', - 'provider': p['serviceProviderName'] - } for p in msgs[-1]['payload']['data']] + return [Profile( + iccid=p['iccid'], + nickname=p['profileNickname'], + enabled=p['profileState'] == 'enabled', + provider=p['serviceProviderName'] + ) for p in msgs[-1]['payload']['data']] - def get_active_profile(self) -> dict[str, str] | None: - return next((p for p in self.list_profiles() if p['enabled']), None) + def get_active_profile(self) -> Profile | None: + return next((p for p in self.list_profiles() if p.enabled), None) def enable_profile(self, iccid: str) -> None: self._validate_profile_exists(iccid) latest = self.get_active_profile() if latest: - if latest['iccid'] == iccid: + if latest.iccid == iccid: raise LPAError(f'profile {iccid} is already enabled') - self.disable_profile(latest['iccid']) + self.disable_profile(latest.iccid) self._validate_successful(self._invoke('profile', 'enable', iccid)) self.process_notifications() def disable_profile(self, iccid: str) -> None: self._validate_profile_exists(iccid) latest = self.get_active_profile() - if latest is not None and latest['iccid'] != iccid: + if latest is not None and latest.iccid != iccid: return self._validate_successful(self._invoke('profile', 'disable', iccid)) self.process_notifications() @@ -59,7 +66,7 @@ class LPA: def delete_profile(self, iccid: str) -> None: self._validate_profile_exists(iccid) latest = self.get_active_profile() - if latest is not None and latest['iccid'] == iccid: + if latest is not None and latest.iccid == iccid: self.disable_profile(iccid) self._validate_successful(self._invoke('profile', 'delete', iccid)) self.process_notifications() @@ -115,7 +122,7 @@ class LPA: return messages def _validate_profile_exists(self, iccid: str) -> None: - if not any(p['iccid'] == iccid for p in self.list_profiles()): + if not any(p.iccid == iccid for p in self.list_profiles()): raise LPAProfileNotFoundError(f'profile {iccid} does not exist') def _validate_successful(self, msgs: list[dict]) -> None: diff --git a/system/hardware/tici/tests/test_esim.py b/system/hardware/tici/tests/test_esim.py index 960ca9c3cf..d36bdaa27b 100644 --- a/system/hardware/tici/tests/test_esim.py +++ b/system/hardware/tici/tests/test_esim.py @@ -35,17 +35,17 @@ class TestEsim: current_active = lpa.get_active_profile() lpa.download_profile(TEST_ACTIVATION_CODE, TEST_NICKNAME) - assert any(p['iccid'] == TEST_ICCID and p['nickname'] == TEST_NICKNAME for p in lpa.list_profiles()) + assert any(p.iccid == TEST_ICCID and p.nickname == TEST_NICKNAME for p in lpa.list_profiles()) lpa.enable_profile(TEST_ICCID) new_active = lpa.get_active_profile() assert new_active is not None - assert new_active['iccid'] == TEST_ICCID - assert new_active['nickname'] == TEST_NICKNAME + assert new_active.iccid == TEST_ICCID + assert new_active.nickname == TEST_NICKNAME lpa.disable_profile(TEST_ICCID) new_active = lpa.get_active_profile() assert new_active is None if current_active: - lpa.enable_profile(current_active['iccid']) + lpa.enable_profile(current_active.iccid)