From c6c752040afd3e670ebe7471001df100c95765d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Wed, 3 Mar 2021 18:11:53 +0000 Subject: freestyle: use freestyle-hid 1.0.2 encoding parameter. This allows FreeStyle Libre devices to use UTF-8 patient names, while not risking to brick older models that might just support ASCII. --- glucometerutils/support/freestyle.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'glucometerutils/support') diff --git a/glucometerutils/support/freestyle.py b/glucometerutils/support/freestyle.py index 28aec6a..b74d4b8 100644 --- a/glucometerutils/support/freestyle.py +++ b/glucometerutils/support/freestyle.py @@ -50,17 +50,20 @@ class FreeStyleHidDevice(driver.GlucometerDevice): device_path: Optional[str], text_cmd: int = 0x60, text_reply_cmd: int = 0x60, + encoding: str = "ascii", ) -> None: super().__init__(device_path) + self._encoding = encoding try: self._session = freestyle_hid.Session( product_id, pathlib.Path(device_path) if device_path else None, text_cmd, text_reply_cmd, + encoding=encoding, ) except Exception as e: - raise exceptions.ConnectionFailed(str(e)) + raise exceptions.ConnectionFailed(str(e)) from e def connect(self) -> None: """Open connection to the device, starting the knocking sequence.""" @@ -92,9 +95,11 @@ class FreeStyleHidDevice(driver.GlucometerDevice): def set_patient_name(self, name: str) -> None: try: - encoded_name = name.encode("ascii") - except UnicodeDecodeError: - raise ValueError("Only ASCII-safe names are tested working") + encoded_name = name.encode(self._encoding) + except UnicodeDecodeError as error: + raise ValueError( + f"Error encoding patient name to {self._encoding}." + ) from error self._session.send_text_command(b"$ptname," + encoded_name) -- cgit v1.2.3